aspose.Cells 导出Excel

aspose

aspse.Cells可以操作Excel,且不依赖于系统环境。

使用模板,通过绑定输出数据源

这种适合于对格式没有特别要求的,直接绑定数据源即可。和数据绑定控件差不多。

WorkbookDesigner designer = new WorkbookDesigner();
designer.Open(templetpath);
dt.TableName = tablename;
designer.SetDataSource(dt);
designer.SetDataSource("title", title);
designer.Process();
designer.Workbook.Save(filename);

如上,使用DataTable作为填充数据源。

 通过代码逐行输出

对个数有特别要求的,灵活性强。可以完成单元格的合并计算等。

第一步,Workbook

 Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();

如果使用模板,可以进一步打开模板。

wb.Open(templetepath);

第二步,Worksheet

Aspose.Cells.Worksheet sheet = wb.Worksheets[0];

第三步,写入数据

 sheet.Cells[row, column].PutValue(value);

输出图片

var pictures = sheet.Pictures;
/*插入图片*/ pictures.Add(upperLeftRow, upperLeftColumn,
new System.IO.MemoryStream(byte[])); /*设置图片格式,与单元格宽度和高度*/
pictures[pictures.Count
- 1].Placement = PlacementType.FreeFloating; sheet.Cells.SetRowHeightPixel(rowIndex, pictures[pictures.Count - 1].Height); sheet.Cells.SetColumnWidthPixel(column, width);
原文地址:https://www.cnblogs.com/lucika/p/4740030.html