Export data from C # to a text document

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

A124 A256 A756 B463 B978 D322 etc. 

I need to export this data to a text document (.doc or .docx format). This is what I did to export the 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 each letter from the grid, a new table must be created with the heading:

 Table A: A124 A256 A756 

enter image description here

enter image description here

  • Each new table should start from a new page, for example:

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

    Table B: B463, B978, // new page

    Table D: D322, etc.

  • The pages in this document should be numbered.

Is there a way to write code in C # for this, or is there some kind of library / plugin that can accomplish this task?

Some examples will be appreciated.

0
source share
2 answers

What version of Word do you need to work with? If it is a version that can handle the .docx file format, i.e. 2007 and later, you can directly generate these files. In fact, the easiest way to do this is to 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

0
source

All Articles