Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to export datagrid to word file in .net
Posted

 
Share this answer
 
Comments
Herman<T>.Instance 15-Sep-11 6:14am    
this is to excel, not to word!
CodingLover 15-Sep-11 7:29am    
Did you read click on the link I've post?

Seems to me not. There is a title 'Exporting the DataGrid to a Word file' in huge font size.
syed tanveer hussain 15-Sep-11 8:05am    
thanks for your reply but its asp i am searching for windows application
CodingLover 15-Sep-11 8:28am    
Then read about Microsoft.Office.Interop. Sorry I don't have a complete example to send to you.
syed tanveer hussain 15-Sep-11 10:57am    
thank you
i will go through it and get back to you with my doubts
 
Share this answer
 
Comments
CodingLover 15-Sep-11 7:30am    
The same I have post above. ;)
syed tanveer hussain 15-Sep-11 8:06am    
need for windows application
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
GridView1.AllowPaging = false;
//GridView1.DataBind();
show_grid();
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
 
Share this answer
 
Hi,

you can export to Word in ASP.NET or Windows application with this C# / VB.NET Word library.

Here is a sample C# code how to export DataGridView to Word file with .NET mail merge functionality:
C#
// Use the component in free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");

// Create and save a template document. 
// You don't have to do this if you already have a template document.
// This code is only provided as a reference how template document should look like.
var document = new DocumentModel();
document.Sections.Add(
  new Section(document,
    new Table(document,
      new TableRow(document,
        new TableCell(document,
          new Paragraph(document, "Name")),
        new TableCell(document,
          new Paragraph(document, "Surname"))),
      new TableRow(document,
        new TableCell(document,
          new Paragraph(document,
            new Field(document, FieldType.MergeField, "RangeStart:People"),
            new Field(document, FieldType.MergeField, "Name"))),
        new TableCell(document,
          new Paragraph(document,
            new Field(document, FieldType.MergeField, "Surname"),
            new Field(document, FieldType.MergeField, "RangeEnd:People")))))));
document.Save("TemplateDocument.docx", SaveOptions.DocxDefault);

// Load a template document.
document = DocumentModel.Load("TemplateDocument.docx", LoadOptions.DocxDefault);

// Mail merge template document with DataGridView.DataSource.
// DataGridView.DataSource is a DataTable with two columns: 'Name' and 'Surname'.
// Important: rangeName parameter and RangeStart/RangeEnd merge field names must match.
document.MailMerge.ExecuteRange("People", dataGridView.DataSource);

// Save the mail merged document.
document.Save("Document.docx", SaveOptions.DocxDefault);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900