Hi daglar,
Check this example. Now please take its reference and correct your code.
XML
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer>
<ID>1</ID>
<Name>John Hammond</Name>
<Country>United States</Country>
</Customer>
<Customer>
<ID>2</ID>
<Name>Mudassar Khan</Name>
<Country>India</Country>
</Customer>
<Customer>
<ID>3</ID>
<Name>Suzanne Mathews</Name>
<Country>France</Country>
</Customer>
<Customer>
<ID>4</ID>
<Name>Robert Schidner</Name>
<Country>Russia</Country>
</Customer>
</Customers>
HTML
<asp:Button Text="Excel" runat="server" OnClick="ExportToExcel" />
<asp:Button Text="Word" runat="server" OnClick="ExportToWord" />
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
protected void ExportToWord(object sender, EventArgs e)
{
// Read Xml to DataSet.
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Customers.xml"));
// Create dummy GridView.
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Customers.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
protected void ExportToExcel(object sender, EventArgs e)
{
// Read Xml to DataSet.
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Customers.xml"));
// Create dummy GridView.
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Customers.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//Apply text style to each Row
GridView1.Rows[i].Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
VB.Net
Protected Sub ExportToWord(ByVal sender As Object, ByVal e As EventArgs)
' Read Xml to DataSet.
Dim ds As DataSet = New DataSet()
ds.ReadXml(Server.MapPath("~/Customers.xml"))
' Create dummy GridView.
Dim GridView1 As GridView = New GridView()
GridView1.AllowPaging = False
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=Customers.doc")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word "
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.RenderControl(hw)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Sub
Protected Sub ExportToExcel(ByVal sender As Object, ByVal e As EventArgs)
' Read Xml to DataSet.
Dim ds As DataSet = New DataSet()
ds.ReadXml(Server.MapPath("~/Customers.xml"))
' Create dummy GridView.
Dim GridView1 As GridView = New GridView()
GridView1.AllowPaging = False
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=Customers.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
For i As Integer = 0 To GridView1.Rows.Count - 1
GridView1.Rows(i).Attributes.Add("class", "textmode")
Next
GridView1.RenderControl(hw)
Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Sub
Screenshots
Excel

Word
