我已经导出了我的数据表,如下面的代码所示。导出大数据时它工作正常,但是当我尝试打开 xls 文件时它提示我这个错误:
“Excel 在 filename.xls 中发现了不可读的内容。是否要恢复此工作簿的内容?如果您信任此工作簿的来源,请单击“是”。
DataTable exportTable = Record.GetAll();
Workbook workbook = new Workbook();
Worksheet sheet = new Worksheet("Test");
for (int x = 0; x < exportTable.Columns.Count; x++)
{
sheet.Cells[0, x] = new Cell(exportTable.Columns[x].ColumnName.ToString());
}
for (int i = 0; i < exportTable.Rows.Count; i++)
{
for (int j = 0; j < exportTable.Columns.Count; j++)
{
sheet.Cells[(i + 1), j] = new Cell(exportTable.Rows[i][j].ToString());
}
}
workbook.Worksheets.Add(sheet);
workbook.Save(@"C:\temp1\Test.xls");
System.IO.FileInfo file = new System.IO.FileInfo(@"C:\temp1\Test.xls");
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
Response.TransmitFile(file.FullName);
Response.End();