Protected Sub ExportToWord(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportWord.Click
'Get the data from database into datatable
Dim strQuery As String = "select CustomerID, ContactName, City, " & _
"PostalCode from customers"
Dim cmd As New SqlCommand(strQuery)
Dim dt As DataTable = GetData(cmd)
'Create a dummy GridView
Dim GridView1 As New GridView()
GridView1.AllowPaging = False
GridView1.DataSource = dt
GridView1.DataBind()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", _
"attachment;filename=DataTable.doc")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-word "
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.RenderControl(hw)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Sub
A.Venkatesan
Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=" & filename & ".doc")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.word"
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
Dim dg As New System.Web.UI.WebControls.GridView()
dg.DataSource = dt
dg.DataBind()
dg.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.[End]()
A.Venkatesan
Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
raj kumar ms...
Member
78 Points
23 Posts
Read Excel Data and Write To Word
Feb 21, 2012 06:04 AM|LINK
Hi friends
I have a requirement to read data from excel table and write it to ms word using C#.net
Iam able to read data from excel table and wite into Datasets.
Now i need to write that dataset to a table in the ms word.
can any one help me ...please
srinivaskotr...
Star
11228 Points
1792 Posts
Re: Read Excel Data and Write To Word
Feb 21, 2012 06:26 AM|LINK
Hi,
refer this link
http://stackoverflow.com/questions/2519026/how-do-you-stream-an-excel-2007-or-word-2007-file-using-asp-net-and-c-sharp (answred forum)
Thanks
Srinivas Kotra.
venkatmca008
Participant
1810 Points
341 Posts
Re: Read Excel Data and Write To Word
Feb 21, 2012 06:34 AM|LINK
hi....try this....
Protected Sub ExportToWord(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportWord.Click 'Get the data from database into datatable Dim strQuery As String = "select CustomerID, ContactName, City, " & _ "PostalCode from customers" Dim cmd As New SqlCommand(strQuery) Dim dt As DataTable = GetData(cmd) 'Create a dummy GridView Dim GridView1 As New GridView() GridView1.AllowPaging = False GridView1.DataSource = dt GridView1.DataBind() Response.Clear() Response.Buffer = True Response.AddHeader("content-disposition", _ "attachment;filename=DataTable.doc") Response.Charset = "" Response.ContentType = "application/vnd.ms-word " Dim sw As New StringWriter() Dim hw As New HtmlTextWriter(sw) GridView1.RenderControl(hw) Response.Output.Write(sw.ToString()) Response.Flush() Response.End() End SubMicrosoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com
salman beher...
All-Star
30547 Points
5841 Posts
Re: Read Excel Data and Write To Word
Feb 21, 2012 06:40 AM|LINK
Hi,
check it....
http://forums.asp.net/t/1320121.aspx/1
http://forums.asp.net/t/1069346.aspx/1
Thanks..
Sincerely,
Salman
raj kumar ms...
Member
78 Points
23 Posts
Re: Read Excel Data and Write To Word
Feb 21, 2012 05:37 PM|LINK
Using console application .. not using grid...
what is the namespace for Word.DocumentClass...iam not getting the name space or what reference to add...
can u help clearly...
venkatmca008
Participant
1810 Points
341 Posts
Re: Read Excel Data and Write To Word
Feb 21, 2012 09:16 PM|LINK
hi...u use this...
Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=" & filename & ".doc") Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.word" Dim stringWrite As New System.IO.StringWriter() Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite) Dim dg As New System.Web.UI.WebControls.GridView() dg.DataSource = dt dg.DataBind() dg.RenderControl(htmlWrite) Response.Write(stringWrite.ToString()) Response.[End]()Microsoft Certified Professional
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact venkatmca008@gmail.com