編輯幫我蔭:嘗試.csv格式。這個對我有用。
protected void btnExport_Click(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
string filename = "testExportToCSV";
DataTable table1 = new DataTable("Customers");
DataRow row1, row2, row3;
DataColumn colName = new DataColumn("CustomerName", System.Type.GetType("System.String"));
DataColumn colCity = new DataColumn("City", System.Type.GetType("System.String"));
table1.Columns.Add(colName);
table1.Columns.Add(colCity);
row1 = table1.NewRow();
row1["CustomerName"] = "aa";
row1["City"] = "ss";
table1.Rows.Add(row1);
row2 = table1.NewRow();
row2["CustomerName"] = "bb";
row2["City"] = "sdd";
table1.Rows.Add(row2);
row3 = table1.NewRow();
row3["CustomerName"] = "cc";
row3["City"] = "dfgfgf";
table1.Rows.Add(row3);
//foreach (System.Data.DataColumn column in table1.Columns)
//{
for (int i = 0; i < table1.Columns.Count; i++)
{
context.Response.Write(table1.Columns[i].ColumnName.ToString().Replace(",", string.Empty) + ",");
}
context.Response.Write(Environment.NewLine);
//}
foreach (System.Data.DataRow row in table1.Rows)
{
for (int i = 0; i < table1.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
context.Response.End();
}
編輯1:
我對你的問題的工作,但我發現,它的security issue
和它成爲你的system's security if we modify it
不良影響。
Key: HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security
Value: (DWORD)"ExtensionHardening" =
[0 = Disable check;
1 = Enable check and prompt;
2 = Enable check, no prompt deny open]
Default setting if value not present is 1 (enable and prompt).
你可以參照從這個鏈接整篇文章:http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx
編輯2:Steps to Modify Registry...
- 點擊開始菜單
- 在搜索程序和文件類型運行
- 然後鍵入REGEDIT ,按OK
- HKEY_CURRENT_USER \軟件\微軟\辦公室\ 12.0 \ EXCEL \安全
希望,它會幫助你。 !謝謝。
如果它解決了您的問題,那麼將其標記爲答案,以便其他人也可以參考它。
我想嘗試一個CSV代替:'HttpContext.Current.Response.ContentType =「text/csv」' –
或者使用epplus包導出到本地xlsx文件 –
我試過「text/csv」它正在用html標籤導出aspx頁面,我想要在這裏導出一個excel文件。 @Daniel Cook – Cathode