2012-10-31 32 views
0

Iam將html數據導出爲ex​​cel。只要我點擊導出按鈕,我就會得到一個excel文件,但是當我嘗試打開excel文件時,彈出一個提示,說文件格式與指定的擴展名不同。我怎樣才能擺脫這種警報,請使用以下代碼如何在打開導出的excel文件時避免出現excel文件格式警報?

HttpContext.Current.Response.Buffer = True 
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel" 
HttpContext.Current.Server.ScriptTimeout = 600 
+0

我想嘗試一個CSV代替:'HttpContext.Current.Response.ContentType =「text/csv」' –

+0

或者使用epplus包導出到本地xlsx文件 –

+0

我試過「text/csv」它正在用html標籤導出aspx頁面,我想要在這裏導出一個excel文件。 @Daniel Cook – Cathode

回答

0

編輯幫我蔭:嘗試.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...

  1. 點擊開始菜單
  2. 在搜索程序和文件類型運行
  3. 然後鍵入REGEDIT ,按OK
  4. HKEY_CURRENT_USER \軟件\微軟\辦公室\ 12.0 \ EXCEL \安全

希望,它會幫助你。 !謝謝。

如果它解決了您的問題,那麼將其標記爲答案,以便其他人也可以參考它。

+0

我在你提供的鏈接中找不到答案@ Mack28 – Cathode

+0

陰極 - 你首先參考我在答案中提供的2個鏈接。 –

+0

我已經通過他們,並嘗試了一些幫助我的解決方案。我沒有在我的代碼中使用任何gridview。我只是創建一個表,並將其導出到excel – Cathode

相關問題