2012-08-14 113 views
0

我正在尋找一種方式導出一個Excel電子表格的方式,使每個單元格中有多個值。ASP.NET Excel導出

我目前使用HtmlTextWriter類將Html寫入Excel內容類型格式,但它會爲相應的Row內的每個值創建一個新的Cell。

例如,目前寫出 行1:RowTitle值1 行2:值2 行3:值3

我想是在一行中寫的一切。 ROW1:RowTitle值1 值2 值3 行2:

這是可能的 - 沒有任何人有任何指針?

我當前的代碼如下:提前

Response.Buffer = true; 
        Response.ContentType = "application/vnd.ms-excel"; 
        Response.Charset = ""; 
        Response.AddHeader("content-disposition", "filename=Compare" + fileName + ".xls"); 
        this.EnableViewState = false; 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
        this.ClearControls(tblComparison); 
        tblComparison.RenderControl(oHtmlTextWriter); 

        // Embed style rules here so that when opening the file in Excel separately, the styling will stay in place 
        // and not be dependent on an external css file. 
        Response.Write(@" 
     <head> 
     <title>Compare</title> 
     <style> 
     #tblComparison { empty-cells:show;border-collapse:collapse;border:none; } 
     #tblComparison tr td { vertical-align:top;text-align:center;padding:0px 5px 2px 8px;border-right:1px solid #094A8C;border-bottom:1px dotted #6699FF;font-size:.7em } 
     #tblComparison tr td.header { text-align:right;font-weight:bold } 
     #tblComparison tr td.header span.note { text-align:right;font-weight:normal;color:#094A8C; } 
     #tblComparison tr td div.itrs { display:none } 
     #tblComparison tr td textarea { width:20em } 
     #tblComparison tr td textarea.objective { height:5em;font-size:.9em } 
     #tblComparison tr td select { width:200px;font-size:1em }  
     #tblComparison tr td textarea.comments { height:10em;font-size:.9em } 
     #tblComparison tr td textarea.multiline { height:10em;font-size:.9em } 
     #tblComparison tr td.requested textarea.comments, 
     #tblComparison tr td.requested textarea.objective { background-color:#F2F7FB;border:1px solid gray } 
     #tblComparison tr td.requested input { font-size:1em;background-color:#F2F7FB;border:1px solid gray } 
     #tblComparison tr .existRequested { background-color:#52697B; color:White;} 
     div { font-size: 1em } 
     </style> 
     </head> 
"); 
        Response.Write(oStringWriter.ToString()); 
        Response.End(); 

感謝。

+0

你試過用OleDb嗎? – 2012-08-14 04:35:02

回答

0

希望這個代碼將會幫助你,

Response.ContentType = "text/csv"; 
    Response.Cache.SetNoStore(); 
    Response.Buffer = true; 
    Response.BufferOutput = true; 
    Response.Charset = "UTF-8"; 
    Response.ContentEncoding = System.Text.Encoding.Unicode; 
    Response.Write("Company, City, Country"); 
    //IF you want each item in each cell of a row 
    byte[] buffer = System.Text.Encoding.Unicode.GetBytes(Separater(Name) + Separater(City) +Separater(Country) + "\r\n") 
    Response.OutputStream.Write(buffer, 0, buffer.Length); 
    Response.OutputStream.Flush(); 


    private string Separater(string text) 
    { 
     if (text == null) 
     { 
      return "\"\","; 
     } 
     return "\"" + text.Replace("\"", "'") + "\"" + ","; 
    } 
+0

謝謝 - 我會明天看看這個,並告訴你它是否有用! – Garrett 2012-08-15 06:34:17

0

如果從一個集合或數據集獲得該表的數據,你可以使用電子表格類即時創建您的Excel和代碼格式化你想要的方式背後。

有些事情是這樣的。 OWC

0

當你說「導出」時,我假定你的意思是你希望能夠爲用戶創建一個電子表格來下載。如果這是你的意思,那麼你可能想看看「CarlogAg Excel Xml Writer」 - http://www.carlosag.net/Tools/ExcelXmlWriter/

如果你的意思是你希望能夠在頁面上創建一個網格來包含數據即Excel電子表格中,那麼您可能需要考慮使用「Excel數據讀取器」 - http://exceldatareader.codeplex.com/讀取數據。這將給你一個DataTable,你可以綁定一個ASP.Net,像這樣... http://www.ezineasp.net/post/ASP-Net-Bind-GridView-to-DataTable.aspx