Skip to main content
 首页 » 编程设计

c#之ASP.NET Web 应用程序导出的 excel 文件上的警告弹出窗口

2024年01月11日36虾米姐

我注意到每次打开由我的网络应用程序导出的 excel 文件时,都会弹出此警告消息

filename.xls, is in a different format than specified by the file extension. Verify that the file is not corrupted.....

这里是使用的代码:

            HttpContext.Current.Response.Clear(); 
            HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Single_Raw.xls")); 
            HttpContext.Current.Response.ContentType = "application/ms-excel"; 
 
        using (StringWriter sw = new StringWriter()) 
        { 
            using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
            { 
                Table tb = new Table(); 
                TableRow tr = new TableRow(); 
 
 
                tr.Cells.Add((rawRow((lblPOR1.Text.Substring(0, 4)), (lblPOR1.Text.Substring(5, 3)), (lblPOR1.Text.Substring(9, 3)), lblPNL.Text.ToString()))); 
 
                TableCell cell3 = new TableCell(); 
                cell3.Text = " "; 
 
                TableRow tr2 = new TableRow(); 
                tr2.Cells.Add((rawRow((lblPOR2.Text.Substring(0, 4)), (lblPOR2.Text.Substring(5, 3)), (lblPOR2.Text.Substring(9, 3)), lblPNL.Text.ToString()))); 
 
 
 
 
                tb.Rows.Add(tr); 
                tb.Rows.Add(tr2); 
 
                tb.RenderControl(htw); 
 
                HttpContext.Current.Response.Write(sw.ToString()); 
                HttpContext.Current.Response.End(); 
            } 
        } 

请您参考如下方法:

我认为它需要不同的内容类型:

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";