我在c#中有一個網格,裏面填充了數據。一個在網格列包含字母(後面數字)字母順序排序,像這樣:從c#導出數據到word文檔
A124
A256
A756
B463
B978
D322
etc.
我需要這個數據的word文檔(.doc或.DOCX格式)的出口。 這是我做過什麼導出一個勁兒地格:
var dt = FuntionThatReturnsDatatables();
var gd = new GridView
{
DataSource = dt
};
gd.DataBind();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;
filename=" + "List" + DateTime.Now.ToString("dd.MM.yyyy") + ".doc");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
var oStringWriter = new StringWriter();
var oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
gd.RenderControl(oHtmlTextWriter);
HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
但現在我必須遵循這樣的邏輯: - 對於從網格中的每個字母與標題的新表應該像這樣創建:
Table A:
A124
A256
A756
每個新表應該從一個新的頁面開始,像這樣:
表A: A124, A256, A756, //新的一頁
表B: B463,B978 , //新的一頁
表d: D322, 等
該字文檔中的頁面需要編號。
有什麼辦法在c#中編寫代碼來做到這一點,或者是否有一些庫/插件可以完成這項任務?
一些例子,將不勝感激。
如果你能和/或允許把錢花在一個庫,你可以看看使用Aspose.Words寫Word文檔:http://www.aspose.com/categories/.net-components/aspose.words-for-.net/default.aspx –
你根本沒有創建一個word文檔。您的代碼創建一個HTML文檔並強制客戶端使用Word打開它。 –