代码之家  ›  专栏  ›  技术社区  ›  Pankaj Kumar

由于文件格式或文件扩展名无效,Excel无法打开该文件。验证文件是否未损坏

  •  0
  • Pankaj Kumar  · 技术社区  · 6 年前

    我正试图用 xlsx xls 格式化,但在尝试打开文件时出现此错误。 Excel无法打开文件 'tms.xlsx' 因为文件格式或文件扩展名无效。确认文件未损坏,并且文件扩展名与文件格式匹配。

    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);
                }
            }
    1 回复  |  直到 6 年前
        1
  •  0
  •   Markus Deibel    6 年前

    请检查内容类型和扩展名。你现在有 .xls application/vnd.ms-excel . 陈述的 xlsx 需要相应的扩展和

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

    要添加一点上下文,我在返回 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");
    }