Exporting data from c# to word document

ぃ、小莉子 提交于 2019-12-12 05:31:20

问题


I have a grid in c# filled with data. One of the columns in that grid contains letters (followed by numbers) sorted alphabetically, like this:

A124
A256
A756
B463
B978
D322
etc.

I need to export this data in a word document (.doc or .docx format). This is what i did to export a signle grid:

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();

But now I have to follow this logic: - For every letter from grid a new table with title should be created like this:

Table A:
 A124
 A256
 A756

  • Each new table should start from a new page, like this:

    Table A: A124, A256, A756, //new page

    Table B: B463, B978, //new page

    Table D: D322, etc.

  • Pages in that word document need to be numbered.

Is there any way to write a code in c# to do this or is there some library/plugin that can accomplish this task ?

Some examples would be appreciated.


回答1:


You should be able to use OpenXML SDK from Microsoft to achieve this.

Reference: http://msdn.microsoft.com/en-us/library/bb448854.aspx

Reference Sample:

  1. http://blogs.msdn.com/b/acoat/archive/2010/06/19/document-creation-and-conversion-with-the-openxml-sdk-and-sharepoint-2010-word-automation-services.aspx
  2. http://blogs.msdn.com/b/acoat/archive/2011/04/06/document-creation-and-conversion-with-the-openxml-sdk-and-sharepoint-2010-word-automation-services-part-2.aspx



回答2:


What version of Word do you need to work with? If it's a version that can handle the .docx file format, i.e. 2007 and later, then you can generate those files directly. In fact, the easiest way to do that is create a .docx file in Word that you use as a kind of template, and then programmatically manipulate the xml in that file.

For more information, see

  • http://msdn.microsoft.com/en-us/magazine/cc163526.aspx
  • http://msdn.microsoft.com/en-us/library/system.io.packaging.aspx


来源:https://stackoverflow.com/questions/10913985/exporting-data-from-c-sharp-to-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!