设定Excel列的格式
MBSCN网友Jedison提了一个关于设定Excel列格式的问题,俺写了个例子贴到了MBSCN上,在这里再贴一下,便于以后查找。
static void StyleTest(Args _args)
{
#Excel
#define.AlignModeText('Align_Text')
#define.AlignModeNumber('Align_Number')
#define.FileName('d:\\StyleTest.xls')
#define.Text('@') //文本类型
#define.Number('#,##0.00') //数字格式
SysExcelApplication application;
SysExcelWorkBooks workBooks;
SysExcelWorkBook workBook;
SysExcelWorkSheet workSheet;
SysExcelRange firstyCol;
SysExcelRange secondCol;
SysExcelStyles styles;
SysExcelStyle style;
SysExcelFont font;
SysExcelCells cell;
;
//实例化Excel对象
application = SysExcelApplication::construct();
workBooks = application.workbooks();
workBook = workBooks.add();
workSheet = workBook.worksheets().itemFromNum(1);
firstyCol = workSheet.columns().item(1); //第一列
secondCol = workSheet.columns().item(2); //第二列
styles = workBook.styles();
//添加Style 居中对齐
style = styles.add(#AlignModeText);
style.horizontalAlignment(#xlCenter);
//添加Style 右对齐
style = styles.add(#AlignModeNumber);
style.horizontalAlignment(#xlRight);
//设定第一列的对齐方式为居中对齐,且为文本类型
firstyCol.style(#AlignModeText);
firstyCol.numberFormat(#Text);
//设定第二列的对齐方式为右对齐,且为数字类型
secondCol.style(#AlignModeNumber);
secondCol.numberFormat(#Number);
cell = workSheet.cells();
//设定第一个cell的值
cell.item(1,1).value('000111');
//设定第二个cell的值
cell.item(1,2).value(000111.12);
workBook.saveAs(#FileName);
application.workbooks().close();
}
{
#Excel
#define.AlignModeText('Align_Text')
#define.AlignModeNumber('Align_Number')
#define.FileName('d:\\StyleTest.xls')
#define.Text('@') //文本类型
#define.Number('#,##0.00') //数字格式
SysExcelApplication application;
SysExcelWorkBooks workBooks;
SysExcelWorkBook workBook;
SysExcelWorkSheet workSheet;
SysExcelRange firstyCol;
SysExcelRange secondCol;
SysExcelStyles styles;
SysExcelStyle style;
SysExcelFont font;
SysExcelCells cell;
;
//实例化Excel对象
application = SysExcelApplication::construct();
workBooks = application.workbooks();
workBook = workBooks.add();
workSheet = workBook.worksheets().itemFromNum(1);
firstyCol = workSheet.columns().item(1); //第一列
secondCol = workSheet.columns().item(2); //第二列
styles = workBook.styles();
//添加Style 居中对齐
style = styles.add(#AlignModeText);
style.horizontalAlignment(#xlCenter);
//添加Style 右对齐
style = styles.add(#AlignModeNumber);
style.horizontalAlignment(#xlRight);
//设定第一列的对齐方式为居中对齐,且为文本类型
firstyCol.style(#AlignModeText);
firstyCol.numberFormat(#Text);
//设定第二列的对齐方式为右对齐,且为数字类型
secondCol.style(#AlignModeNumber);
secondCol.numberFormat(#Number);
cell = workSheet.cells();
//设定第一个cell的值
cell.item(1,1).value('000111');
//设定第二个cell的值
cell.item(1,2).value(000111.12);
workBook.saveAs(#FileName);
application.workbooks().close();
}