NpsExcel用于读写Excel表格操作。
读取Excel表格样例代码:
var excel = new NpsExcel(new NpsFile("a.xls"),'r');
out.Info(excel.sheets);
excel.currentsheet=0;
out.Info(excel.sheetname);
out.Info(excel.rows);
out.Info(excel.columns);
out.Info(excel.GetCell(0,0));
out.Info(excel.GetString(1,1));
out.Info(excel.GetDouble(2,2));
out.Info(excel.GetDate(3,3));
out.Info(excel.GetBoolean(4,4));
excel.Close();
生成新的Excel表格样例代码:
var format1 = new NpsExcelFormat("","");
format1.SetFont("宋体",12,true,false,false,"black");
format1.SetAlignment("centre");
format1.SetVerticalAlignment("centre");
format1.SetBorder("all","thin");
format1.SetBackground("yellow");
format1.SetOrientation("horizon");
var excel = new NpsExcel(new NpsFile("new.xls"),'n');
excel.CreateSheet("My sheet",0);
excel.MergeCell(0,0,0,10);
excel.AddString(0,0,"TITLE TEST",format1);
excel.AddNumber(1,0,2.5,format1);
excel.AddDate(2,0,new Date(),format1);
excel.AddBoolean(3,0,true,format1);
excel.AddFormula(4,0,"5*2",format1);
excel.Write();
excel.Close();
修改已有Excel表格样例代码:
var excel = new NpsExcel(new NpsFile("new.xls"),'rw');
excel.SetString(0,0,"MODIFY");
excel.SetNumber(1,0,3.8);
excel.SetString(1,0,"STRING");
excel.SetDate(2,0,new Date(2005,7,20));
excel.SetBoolean(3,0,false);
excel.SetFormula(4,0,"10*2");
excel.SetString(0,5,"STRING2");
excel.Write();
excel.Close();
| int |
sheets 只读,当前Excel表格的工作量数量 |
| int |
currentsheet 设置当前工作表。从0开始计数。 |
| String |
sheetname 只读,当前工作表名称 |
| int |
rows 只读,当前工作表的行数 |
| int |
columns 只读,当前工作表的列数 |
| NpsExcel |
new(NpsFile file,String mode) 构造函数。file表示要打开的文件句柄;mode是打开方式,可以有以下取值: 1.mode=r,表示只读模式; 2.mode=n,表示新建模式。如果当前文件存在,将覆盖该文件; 3.mode=rw,表示打开现有文件并进入改写模式; |
| void |
Open(NpsFile file,String mode) 打开指定的文件。file表示要打开的文件句柄;mode是打开方式,可以有以下取值: 1.mode=r,表示只读模式; 2.mode=n,表示新建模式。如果当前文件存在,将覆盖该文件; 3.mode=rw,表示打开现有文件并进入改写模式; |
| String |
GetCell(int col, int row) 得到某个单元格的值,返回值用于是String类型。 |
| String |
GetString(int col, int row) 得到指定单元格文本。 |
| double |
GetDouble(int col, int row) 得到指定数值类型单元格的值,返回值为数字。 |
| Date |
GetDate(int col, int row 得到指定日期类型单元格值,返回值为日期。 |
| boolean |
GetBoolean(int col, int row) 得到指定布尔类型单元格值,返回值为true/false。 |
| void |
CreateSheet(String name,int index) 创建新的工作表。 |
| void |
SetName(String name) 设置当前工作表表名。 |
| void |
SetProtected(boolean b) 设置Excel表格权限。如果为true,则需要密码才能修改。 |
| void |
SetPassword(String pass) 设置修改密码。 |
| void |
SetDefaultColumnWidth(int width) 设置缺省列宽。 |
| void |
SetDefaultRowHeight(int height) 设置缺省行高。 |
| void |
SetRowView(int row,int height,boolean collapsed) 设置行高 |
| void |
SetColumnView(int col,int width) 设置列宽 |
| void |
InsertColumn(int col) 插入列 |
| void |
InsertRow(int row) 插入行 |
| void |
RemoveColumn(int col) 删除列 |
| void |
RemoveRow(int row) 删除行 |
| void |
MergeCell(int c1,int r1,int c2,int r2) 合并单元格 |
| void |
AddString(int col,int row,String value,NpsExcelFormat format) 设置指定单元格(col,row)的值为指定字符串 |
| void |
AddNumber(int col,int row,double value,NpsExcelFormat format) 设置指定单元格(col,row)的值为指定数字 |
| void |
AddDate(int col,int row,Object d,NpsExcelFormat format) 设置指定单元格(col,row)的值为指定日期 |
| void |
AddBoolean(int col,int row,boolean value,NpsExcelFormat format) 设置指定单元格(col,row)的值为指定布尔值 |
| void |
AddFormula(int col,int row,String formula,NpsExcelFormat format) 为设置指定单元格(col,row)设置公式 |
| void |
SetString(int col,int row,String value) 修改单元格(col,row)的值 |
| void |
SetNumber(int col,int row,double value) 修改单元格(col,row)的值 |
| void |
SetDate(int col,int row,Object d) 修改单元格(col,row)的值 |
| void |
SetBoolean(int col,int row,boolean value) 修改单元格(col,row)的值 |
| void |
SetFormula(int col,int row,String formula) 修改单元格(col,row)的公式 |
| void |
Write() 保存Excel文件 |
| void |
Close() 关闭Excel对象 |