简体   繁体   English

Excel无法打开文件,因为文件格式或文件扩展名无效。 确认文件未损坏

[英]Excel cannot open the file because the file format or file extension is not valid. Verify that file has not been corrupted

I am trying to export into excel with xlsx and xls format but getting this error when trying to open the file. 我正在尝试以xlsxxls格式导出到excel,但是在尝试打开文件时遇到此错误。 Excel cannot open the file 'tms.xlsx' because the file format or file extension is not valid. Excel无法打开文件 'tms.xlsx'因为文件格式或文件扩展名无效。 Verify that file has not been corrupted and that the file extension matches the format of the file. 确认文件未损坏,并且文件扩展名与文件格式匹配。

 public static void ExportToExcel(object allLists, string fileName, string driverName) { try { grid.DataSource = allLists; grid.DataBind(); RowCreated(); Header(fileName, driverName); HttpContext.Current.Response.ClearContent(); string FileName = String.Format(fileName + "-{0}", DateTime.Now.ToString("dd/MMM/yyyy")); HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + FileName + ".xls"); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); //Applying style to grid view header cells for (int i = 0; i < grid.Rows.Count; i++) { for (int J = 0; J < grid.HeaderRow.Cells.Count; J++) { if ((grid.Rows[i].Cells[J].Text.Contains("-y@llow"))) { grid.Rows[i].Cells[J].BackColor = System.Drawing.Color.FromArgb(255, 255, 179); var val = Convert.ToString(grid.Rows[i].Cells[J].Text); if (val != null && val != "") { if (val.Contains("-y@llow")) { val = val.Replace("-y@llow", ""); grid.Rows[i].Cells[J].Text =Convert.ToString(val); } } } else if((grid.Rows[i].Cells[J].Text.Contains("-gr@en"))) { grid.Rows[i].Cells[J].BackColor = System.Drawing.Color.FromArgb(159, 223, 159); var val =Convert.ToString(grid.Rows[i].Cells[J].Text); if (val != null && val != "") { if (val.Contains("-gr@en")) { val = val.Replace("-gr@en", ""); grid.Rows[i].Cells[J].Text = Convert.ToString(val); } } } } } for (int i = 0; i < grid.HeaderRow.Cells.Count; i++) { grid.HeaderRow.Cells[i].Style.Add("background-color", "#337ab7"); grid.HeaderRow.Cells[i].Style.Add("color", "white"); } grid.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } catch (Exception) { //Response.End() will generate exception so, do not throw the exception here. //Response.Write(Ex.StackTrace); } } 

Please check content type and extension. 请检查内容类型和扩展名。 You currently have .xls and application/vnd.ms-excel . 您目前拥有.xlsapplication/vnd.ms-excel The stated xlsx requires the respective extension and 所述的xlsx需要相应的扩展名和

ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"`

To add a bit of context I use this as follows in an controller method that returns an ActionResult 为了添加一些上下文,我在返回ActionResult的控制器方法中按以下方式使用它

ActionResult ExportAsExcel(object input)
{
    var fileData = ExcelExport.CreateExcelContent(input);

    if (fileData == null || fileData.Length == 0)
    {
        ViewData["Message"] = "Could not create export file";
        View("Error");
    }

    // File is of type Controller.File
    return File(fileData, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "export.xlsx");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成 Excel C#:Excel 无法打开文件,因为文件格式或文件扩展名无效 - Generate Excel C#: Excel cannot open the file because the file format or file extension is not valid xlsx excel无法打开文件,因为文件格式或文件扩展名无效-C# - xlsx excel cannot open the file because the file format or file extension is not valid - c# excel无法打开文件,因为文件格式或文件扩展名无效c# - excel cannot open the file because the file format or file extension is not valid c# 使用 NPOI 损坏 .xlsx 文件的问题 - Excel 无法打开文件“file.xlsx”,因为文件格式或文件扩展名无效 - Problematic corruption of .xlsx files with NPOI - Excel cannot open the file 'file.xlsx" because the file format or file extension is not valid C#生成的Excel文件:文件格式或文件扩展名无效 - C# Generated Excel File: File Format or File Extension is not valid 如何验证文件是否是有效的Excel电子表格? - How to verify if a file is a valid excel spreadsheet? 打开损坏的.xls 文件 - Open corrupted .xls file NPOI Excel 文件无效的文件格式或扩展名 - NPOI Excel File invalid file format or extension 无法在C#中打开Excel文件,因为格式有不同的扩展名错误 - can not open excel file in c# for different format extension error Adobe Reader无法打开“ StreamTest5.pdf”,因为它不是受支持的文件或文件已损坏错误 - Adobe Reader could not open 'StreamTest5.pdf' because it is either not a supported file or file has been damaged error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM