我正在努力创建和保存数据透视表,我运行下面的代码没有错误,但未能打开excel文件。下面是我的代码:
public void Execute()
{
IWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("sample");
IRow row0 = sheet.CreateRow(0);
row0.CreateCell(0).SetCellValue("test");
row0.CreateCell(1).SetCellValue("a");
row0.CreateCell(2).SetCellValue("b");
IRow row1 = sheet.CreateRow(1);
row1.CreateCell(0).SetCellValue("asd");
row1.CreateCell(1).SetCellValue("das");
row1.CreateCell(2).SetCellValue("fgh");
var tl = row0.GetCell(0);
var br = row1.GetCell(2);
CellReference topLeft = new CellReference(tl);
CellReference botRight = new CellReference(br);
AreaReference aref = new AreaReference(topLeft, botRight);
CellReference pos = new CellReference(3, 3);
XSSFPivotTable pivotTable = sheet.CreatePivotTable(aref, pos);
using (FileStream fileWritter = new FileStream("test.xlsx", FileMode.Create, FileAccess.Write))
{
workbook.Write(fileWritter);
}
}
打开文件的错误:
删除部分:/xl/pivotCache/pivotachedefinition1 . XML部分,XML错误。(数据透视表缓存)HRESULT 0x8000ffff第一行,第0列删除的部分:/xl/pivotTables/pivotTable1.xml部分,XML错误。(数据透视表视图)HRESULT 0x8000ffff第1行,第0列
请帮忙!
我自己找到了答案:使用Microsoft.Office.Interop.Excel
在Excel中做无聊的步骤。
我使用Excel中的宏记录器生成源代码,然后很容易地用我想要的任何语言编辑代码。