2017-03-20 126 views
0

下面的代碼現在可以使用了,但是我想在打開Excel後沒有任何損壞消息的XLSX格式。以(XLSX)格式將數據導出到Excel數據

protected void BTNExportExcel_Click(object sender, EventArgs e) 
{ 
    DataTable dtexp= ExportTrends(); //const 

    try 
    { 
     Response.Clear(); 
     Response.ClearContent();    
     Response.ContentType = "application/vnd.ms-excel"; 
     Response.AddHeader("Content-Disposition", "attachment; filename=Export.xls"); 
     TextWriter tw = new StringWriter(); 
     HtmlTextWriter h = new HtmlTextWriter(tw); 

     string[] images = TXTImages.Text.Split(new string[]{"<img"},StringSplitOptions.None); 

     if (images.Length > 0) 
     { 
      Response.Write(images[0].ToString()); 
      for (int i = 1; i < images.Length; i++) 
      { 
       System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image(); 
       string secondpart = images[i].Substring(images[i].IndexOf(',')+1); 
       img.ImageUrl = LoadImage(images[i].Substring(images[i].IndexOf(',') + 1, secondpart.LastIndexOf('\'')));       
       img.RenderControl(new HtmlTextWriter(Response.Output)); 
       Response.Write("<BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/><BR/>"); 
      }      
     } 
     Response.Write("<BR/>"); 
     ////// Create a dynamic control, populate and render it 
     GridView excel = new GridView(); 
     excel.DataSource = dtexp; 
     excel.DataBind(); 

     excel.RenderControl(new HtmlTextWriter(Response.Output)); 

     Response.Flush(); 
     Response.End(); 

    } 
    catch (Exception ex) 
    { 
     Response.Write(string.Empty); 
     Response.Flush(); 
     Response.End(); 
    } 
} 
+1

然後,您需要找到一個能夠生成真正的XLSX文件的庫。你有沒有試過谷歌搜索「C#XLSX庫」? – mason

+0

是的,我嘗試過,但他們使用外部庫。我不想將外部庫添加到我的項目中。 – VMS

+0

那對你來說會很艱難。你看,XLSX並不是真正的簡單格式,只需在你自己的代碼中生成即可。這就是圖書館存在的原因。所以你不必重新發明輪子。你有什麼反對使用外部庫? – mason

回答

0

確保您設置了對Excel的引用,然後像下面的代碼示例這樣的東西應該爲您完成工作。

private void button1_Click(object sender, EventArgs e) 
     { 
      //connect with database 
      OleDbConnection connection = new OleDbConnection(); 
      connection.ConnectionString = @"Provider=""Microsoft.Jet.OLEDB.4.0"";Data Source=""demo.mdb"";User Id=;Password="; 
      OleDbCommand command = new OleDbCommand(); 
      command.CommandText = "select * from parts where Cost<1000 and ListPrice>500"; 
      DataSet dataSet = new System.Data.DataSet(); 
      OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command.CommandText, connection); 
      dataAdapter.Fill(dataSet); 
      DataTable dt = dataSet.Tables[0]; 
      this.dataGridView1.DataSource = dt; 
      //export specific data to Excel 
      Workbook book = new Workbook(); 
      Worksheet sheet = book.Worksheets[0]; 
      book.Worksheets[0].InsertDataTable(this.dataGridView1.DataSource as DataTable, true, 1, 1); 
      book.SaveToFile("sample.xlsx", ExcelVersion.Version2010); 
      System.Diagnostics.Process.Start("sample.xlsx"); 
     } 
+0

在ASP.NET應用程序中使用Excel Interop並不是一個好主意,它非常容易出錯並且甚至不被Microsoft支持。請參閱以下鏈接:[有關Office的服務器端自動化的注意事項](https://support.microsoft.com/zh-cn/help/257757/considerations-for-server-side-automation-of-office) – bassfader