默认情况下,在asp.net中导出到Excel,文件类型应该在Excel工作簿97-2003中 [英] Export to Excel in asp.net by default the file type should be in Excel workbook 97-2003

查看:75
本文介绍了默认情况下,在asp.net中导出到Excel,文件类型应该在Excel工作簿97-2003中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将数据表导出到Microsoft Excel。



I use the following code for exporting a data table into Microsoft Excel.

public static void ExportToExcel(HttpResponse Response, string sData, string sFileName)
    {
    Response.ClearContent();
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("content-disposition", "attachment; filename=" + sFileName +  ".xls");
        Response.Write(sData);
    }





生成excel文件。当我打开excel文件并单击另存为对话框窗口时,默认选择保存类型为(网页(.htm或html)。)。这应该在Excel工作簿97-2003中默认。当我尝试通过特定的邮件服务器发送.xls文件时,接收器可以打开附件文件。当我尝试发送使用上述代码生成的文件时,接收方无法打开附件文件。错误消息是此附件已被删除,因为它包含可能带来安全风险的数据。我需要解决这个问题的任何替代解决方案。请帮我这方面



The excel file gets generated. When I open the excel file and click on save as dialog window the default selected save as type is (Web Page(.htm or html).) . This should in Excel workbook 97-2003 by default. When I try to send .xls file through a particular mail server the receiver is able to open the attachment file. When I try to send the file which is generated by using the above code the receiver is unable to open the attachment file. The Error message is "This attachment was removed because it contains data that could pose a security risk." I need to resolve this issue any alternative solution. please help me on this regard

推荐答案

问题是你没有导出Excel文件。 Excel 97-2003文件的内容根本不是字符串,而是二进制内容。您导出的文件无效。



您需要使用专门设计的库来生成实际的Excel文件。在Web服务器中,您不能使用Excel本身,但必须使用专用于此目的的库或使用OleDb方法在工作表中生成数据。 OleDb方法的问题在于您根本没有格式化功能。您只是将数据插入单元格。
The problem is that you're not exporting an Excel file. The content of a Excel 97-2003 file is not a string at all but binary content. The "file" you're exporting is invalid.

You need to generate an actual Excel file using some library designed to do so. In a web server you cannot use Excel itself, but must use either a library dedicated to the purpose or use OleDb methods to generate the data in a sheet. The problem with the OleDb methods is that you get no formatting capabilities at all. You're just plugging data into cells.


这篇关于默认情况下,在asp.net中导出到Excel,文件类型应该在Excel工作簿97-2003中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆