aspose.Cells 导出Excel

2022-11-25,,,

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[];

第三步,写入数据

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

输出图片

var pictures = sheet.Pictures;
/*插入图片*/
pictures.Add(upperLeftRow, upperLeftColumn, new System.IO.MemoryStream(byte[]));
/*设置图片格式,与单元格宽度和高度*/
pictures[pictures.Count - ].Placement = PlacementType.FreeFloating;
sheet.Cells.SetRowHeightPixel(rowIndex, pictures[pictures.Count - ].Height);
sheet.Cells.SetColumnWidthPixel(column, width);

aspose.Cells 导出Excel的相关教程结束。

《aspose.Cells 导出Excel.doc》

下载本文的Word格式文档,以方便收藏与打印。