上周在博客中写了一篇《Asp.net高效导出Excel篇》在发布之后收到很多热心网友的建议——使用Excel第三方引擎如NOPI、Aspose.cell等导出Excel,优点:效率高、不需要在客户端安装Excel组件,功能强大,可制作高难度Excel形式报表。在众多优点的吸引下我选择了Aspose.cell做为研究对象,并最终实现我项目的需求,现将我做的一个试验例子列出来以供学习。
1、先在项目中引用Aspose.Cells.dll
2、using Aspose.Cells;
Aspose.Cells.Workbook wb=new Aspose.Cells.Workbook();//实例化Workbook对象
Aspose.Cells.Worksheet sheet = wb.Worksheets[0];
Aspose.Cells.Cells cells = sheet.Cells;
int styleIndex = wb.Styles.Add();//设置样式,如字体大小等
Aspose.Cells.Style style = wb.Styles[styleIndex];
style.Font.Size = 12;
style.Font.IsBold = true;
style.Font.Name = "仿宋";
Range range = sheet.Cells.CreateRange(0, 0, 1, 6); //获取excel第0行第0列,一行6列范围
range.Style = style;
range.Merge();//合并单元格
cells[0, 0].PutValue("xxxxxxxxxxxxxxxxxx");//给单元格赋值
//excel单元格注释
Comments comments = wb.Worksheets[0].Comments;
int commentIndex = comments.Add(0,0);
Comment comment = comments[commentIndex];
comment.Note = "ooooooo";
comment.Font.Name = "lalalala";
int row = 100;
int col = 3;
for (int i = 3; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cells[i, j].PutValue(i);
}
}
sheet.AutoFilter.Range = "A8:P"+index;//筛选
sheet.AutoFitColumns();
int styleIndex2 = wb.Styles.Add();
Aspose.Cells.Style style2 = wb.Styles[styleIndex2];
style2.IsTextWrapped = true;//设置单元格自动换行
Range range10= sheet.Cells.CreateRange(8, 1, dt.Rows.Count, 1);
range10.Style = style2;
range10.ColumnWidth = 30;
range10.RowHeight = 50;
wb.Save(ExcelFullName);