0
我已經導出了我的數據表,如下面的代碼所示。它導出大數據時工作正常,但當我嘗試打開xls文件時,它提示我此錯誤:將DataTable導出爲工作簿
「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();