数据表导出到Excel ASP [英] Export DataTable to Excel asp
问题描述
我有一个网页,将数据导出到Excel文件。我遇到的唯一问题是,当我尝试打开Excel文件,我得到一个消息,说你正在试图打开该文件是不是由文件扩展名指定的格式不同。验证文件没有损坏,是从打开文件之前信任的来源。我怎样才能摆脱这种消息。要做到我使用,我在另一篇文章中发现一个函数的出口。这里是code ...
I have a web page that exports data to an excel file. The only problem I am having is when I try and open the excel file I get a message saying "The file you are trying to open is in a different format than specified by the file extension. Verify the file is not corrupted and is from a trusted source before opening the file.". How can I get rid of this message. To do the export I am using a function that I found in another article. Here is the code...
private void ExporttoExcel(DataTable table)
{
//Response.Clear();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/excel";
HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=TestingReports");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1252");
// Sets font
HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
HttpContext.Current.Response.Write("<BR><BR><BR>");
// Sets the table border, cell spacing, border color, font of the text, background, foreground, font height
HttpContext.Current.Response.Write("<Table bgColor='#ffffff' cellSpacing='0' cellPadding='0' style='font-size:10.0pt; font-family:Calibri; background:white;'> <TR>");
// Am getting my grid's column headers
int columnscount = table.Columns.Count;
// Write in new column
for (int j = 0; j < columnscount; j++)
{
HttpContext.Current.Response.Write("<Td style='font-size:15.0pt; text-align:center; width:80.0pt; border-width:1.0pt; border-color:#000000; border-style:solid; height:22.0pt;'>");
// Get column headers and make it as bold in excel columns
HttpContext.Current.Response.Write("<B>");
HttpContext.Current.Response.Write(table.Columns[j].ToString());
HttpContext.Current.Response.Write("</B>");
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
// Write in new row
foreach (DataRow row in table.Rows)
{
HttpContext.Current.Response.Write("<TR>");
for (int i = 0; i < table.Columns.Count; i++)
{
HttpContext.Current.Response.Write("<Td style='width:80.0pt; text-align:center; border-width:0.5pt; border-color:#000000; border-style:solid; height:22.0pt;'>");
HttpContext.Current.Response.Write(row[i].ToString());
HttpContext.Current.Response.Write("</Td>");
}
HttpContext.Current.Response.Write("</TR>");
}
HttpContext.Current.Response.Write("</Table>");
HttpContext.Current.Response.Write("</font>");
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
//Response.Write("TestingReports.xls");
//Response.Flush();
//Response.End();
}
任何帮助将非常AP preciated。谢谢你在前进。
Any help will be much appreciated. Thank you in advance.
推荐答案
这是使用应用程序/ x-msexcel的或应用程序/ vnd.ms - Excel的MIME类型来打开Microsoft Excel的内部网页内容的Web站点当文件试图在Excel 2007中打开可能会遇到以下警告提示:
Web sites that use the "application/x-msexcel" or "application/vnd.ms-excel" MIME type to open web page content inside of Microsoft Excel may encounter the following warning prompt when the file attempts to open in Excel 2007:
你试图打开'[文件名]'的文件,是不是由文件扩展名指定的格式不同。验证该文件没有损坏,是来自可信来源打开文件之前,你现在要打开文件吗? (是|否|帮助)
"The file you are trying to open, '[filename]', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?" (Yes | No | Help)
如果用户点击是,该文件将如预期打开。如果用户点击没有,该文件可能无论如何打开,或者可以提示的第二时间,然后,如果用户没有重新选择不开。
If the user clicks Yes, the file will open as expected. If the user clicks No, the file may open anyway, or may prompt a second time, and then not open if the user selects No again.
在<一个更多信息href=\"http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx
这篇关于数据表导出到Excel ASP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!