导出excel错误:您尝试打开FileName的文件的格式与指定的扩展名不同

我已经使用以下代码将数据集中的数据导出到excel文件中:

public void ExportRecords(System.Data.DataTable dt, String fileName) { try { if (dt.Rows.Count > 0) { string path = AllinAudio_Temp; string timestamp = Convert.ToString(DateTime.Now); timestamp = timestamp.Replace(" ", ""); timestamp = timestamp.Replace("/", ""); timestamp = timestamp.Replace(":", ""); fileName = fileName.Replace(" ", ""); string filename = path + @"\" + timestamp + "_" + fileName + ".xls"; CarlosAg.ExcelXmlWriter.Workbook book = new CarlosAg.ExcelXmlWriter.Workbook(); //// Add a Worksheet with some data CarlosAg.ExcelXmlWriter.Worksheet sheet = book.Worksheets.Add("Sheet1"); WorksheetStyle style = book.Styles.Add("HeaderStyle"); style.Font.Bold = true; WorksheetStyle style1 = book.Styles.Add("HeaderStyle1"); style1.Font.Bold = true; style1.Font.Color = "Red"; WorksheetRow row;// = sheet.Table.Rows.Add(); row = sheet.Table.Rows.Add(); foreach (DataColumn column in dt.Columns) { row.Cells.Add(new WorksheetCell(column.ColumnName, "HeaderStyle")); } for (int i = 0; i < dt.Rows.Count; i++) { row = sheet.Table.Rows.Add(); for (int l = 0; l < dt.Columns.Count; l++) { row.Cells.Add(dt.Rows[i][l].ToString()); } } book.Save(filename); Response.ContentType = "text/excel"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + timestamp + "_" + fileName + ".xls"); Response.TransmitFile(filename); Response.End(); } } catch (Exception) { throw; } } 

当文件被导出,我们试图打开它,它给我留言:

在这里输入图像说明

我只需要xls格式的文件,而且我也是用xls格式的文件。

但是,当我尝试打开它时,为什么会出现这种错误。

当我尝试使用XLSX

在这里输入图像描述

最大的提示是用来创build文件的组件的名称。 ExcelXmlWriter似乎以Excel XML格式写入文件,该格式应具有文件扩展名XML 。 当您使用XLS扩展名时,Excel正在期望内容使用旧格式。 当它无法打开文件作为一个经典的Excel文件,它尝试的替代品,发现Excel的XML工程,并警告你的问题。

解决scheme:更改您的输出代码以使用文件扩展名XML代替XLS

(如前所述,不是XLSX。)

试试这个function

  public static void ExportToExcelOnece(DataSet dsExcel, string ExcelFilePath) { if (dsExcel == null || dsExcel.Tables.Count == 0) throw new Exception("ExportToExcel: Null or empty input table!\n"); Excel.Application excelApp = new Excel.Application(); excelApp.Workbooks.Add(); Excel.Worksheet newWorksheet; Excel.Worksheet objSheet = (Excel.Worksheet)excelApp.ActiveWorkbook.Sheets["Sheet1"]; for (int m = 0; m <= dsExcel.Tables.Count - 1; m++) { newWorksheet = (Excel.Worksheet)excelApp.Worksheets.Add(objSheet, Missing.Value, Missing.Value, Missing.Value); newWorksheet.Name = dsExcel.Tables[m].TableName; // column headings for (int i = 0; i < dsExcel.Tables[m].Columns.Count; i++) { newWorksheet.Cells[1, (i + 1)] = dsExcel.Tables[m].Columns[i].ColumnName; } // rows for (int i = 0; i < dsExcel.Tables[m].Rows.Count; i++) { for (int j = 0; j < dsExcel.Tables[m].Columns.Count; j++) { newWorksheet.Cells[(i + 2), (j + 1)] = dsExcel.Tables[m].Rows[i][j]; } } } ((Excel.Worksheet)excelApp.ActiveWorkbook.Sheets[1]).Activate(); objSheet.Delete(); objSheet = (Excel.Worksheet)excelApp.ActiveWorkbook.Sheets["Sheet2"]; objSheet.Delete(); objSheet = (Excel.Worksheet)excelApp.ActiveWorkbook.Sheets["Sheet3"]; objSheet.Delete(); // check fielpath if (ExcelFilePath != null && ExcelFilePath != "") { try { excelApp.ActiveWorkbook.SaveAs(ExcelFilePath); excelApp.Quit(); MessageBox.Show("Excel file saved!"); } catch (Exception ex) { throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n" + ex.Message); } } else // no filepath is given { excelApp.Visible = true; } //Quit the Application. excelApp.Quit(); }