A
Andrew
I'm adding this as it to me a while to figure out all the pieces to be
able to do this without using Microsoft.Office.Interop which caused me
problems on the web-server.
Streaming is the easy part, but I couldn't initially work out how to
manipulate the page setup to change page margins and orientation,
that's why I was looking at Microsoft.Office.Interop.
But with Microsoft.Office.Interop I couldn't fiure out how to stream
HTML Source to the word doc it would only output text.
So here's what I figured out:
1. Open a new word doc and alter the page setup to how you want it to
look. Then in the body of the document type the following:
#INSERTDATAHERE#
If you read the html source of this you should easily be able to pick
out the page setup section:
@page Section1
{size:11.0in 8.5in;
mso-page-orientation:landscape;
margin:.5in 1.0in .5in 1.0in;
mso-header-margin:.5in;
mso-footer-margin:.5in;
mso-paper-source:0;}
2. Then save that word doc as html in your web application folder
3. create a blank word doc in your web application folder called
output.doc
4. In your asp.net page add an asp button and an onclick event
e.g:
<asp:Button id="CmdExport" onclick="Export_Click" Text="Click to
Export"></asp:Button>
In the codebehind for the onclick event = Export_Click this example
will render a datagrid and some html (example is in VB)
Sub Export_Click(Sender As Object, E As EventArgs)
Dim sw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(sw)
'Get the HTML for the control.
hw.Write(("<center><H2>THIS IS MY TITLE</H2>"))
hw.Write(("<p>This is a subtitle</p>"))
datagrid.RenderControl(hw)
hw.Write(("</center>"))
'This section will read the input and look for
'#INSERTDATAHERE#" and then input your data in its place
' and will then save the output and open it on as a new
'file on the client
Dim input As New
StreamReader(Server.MapPath("newdoc.html"))
Dim output As New
StreamWriter(Server.MapPath("output.doc"))
Dim findstring As String = "#INSERTDATAHERE#"
Dim line As String = input.ReadLine()
Dim pos As Integer
Do Until line Is Nothing
If InStr(1,line,findstring,1) > 0 Then
output.WriteLine(Replace(line,findstring,sw.ToString()))
Else
output.WriteLine(line)
End If
line = input.ReadLine()
Loop
output.Close()
input.Close()
End If
' this section will get output and open the file as an
' attachment on the client
Dim FilePath As String = Server.MapPath("output.doc")
Dim fs As New
FileStream(FilePath,FileMode.Open,FileAccess.Read)
Dim bw As New System.IO.BinaryReader(fs)
Dim byt() As Byte, i As Integer
byt = bw.ReadBytes(CInt(fs.Length))
i = byt.Length()
Response.ContentType = "application/msword"
' change attachment to inline if you want it to open
'in browser window
Response.AddHeader
("content-disposition","attachment;filename=myoutput.doc")
Response.OutputStream.Write(byt, 0, i)
Response.OutputStream.Close()
End Sub
hope this helps someone and saves you some time. Someone else may have
other suggestions and better code, but this is my 2cents.
able to do this without using Microsoft.Office.Interop which caused me
problems on the web-server.
Streaming is the easy part, but I couldn't initially work out how to
manipulate the page setup to change page margins and orientation,
that's why I was looking at Microsoft.Office.Interop.
But with Microsoft.Office.Interop I couldn't fiure out how to stream
HTML Source to the word doc it would only output text.
So here's what I figured out:
1. Open a new word doc and alter the page setup to how you want it to
look. Then in the body of the document type the following:
#INSERTDATAHERE#
If you read the html source of this you should easily be able to pick
out the page setup section:
@page Section1
{size:11.0in 8.5in;
mso-page-orientation:landscape;
margin:.5in 1.0in .5in 1.0in;
mso-header-margin:.5in;
mso-footer-margin:.5in;
mso-paper-source:0;}
2. Then save that word doc as html in your web application folder
3. create a blank word doc in your web application folder called
output.doc
4. In your asp.net page add an asp button and an onclick event
e.g:
<asp:Button id="CmdExport" onclick="Export_Click" Text="Click to
Export"></asp:Button>
In the codebehind for the onclick event = Export_Click this example
will render a datagrid and some html (example is in VB)
Sub Export_Click(Sender As Object, E As EventArgs)
Dim sw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(sw)
'Get the HTML for the control.
hw.Write(("<center><H2>THIS IS MY TITLE</H2>"))
hw.Write(("<p>This is a subtitle</p>"))
datagrid.RenderControl(hw)
hw.Write(("</center>"))
'This section will read the input and look for
'#INSERTDATAHERE#" and then input your data in its place
' and will then save the output and open it on as a new
'file on the client
Dim input As New
StreamReader(Server.MapPath("newdoc.html"))
Dim output As New
StreamWriter(Server.MapPath("output.doc"))
Dim findstring As String = "#INSERTDATAHERE#"
Dim line As String = input.ReadLine()
Dim pos As Integer
Do Until line Is Nothing
If InStr(1,line,findstring,1) > 0 Then
output.WriteLine(Replace(line,findstring,sw.ToString()))
Else
output.WriteLine(line)
End If
line = input.ReadLine()
Loop
output.Close()
input.Close()
End If
' this section will get output and open the file as an
' attachment on the client
Dim FilePath As String = Server.MapPath("output.doc")
Dim fs As New
FileStream(FilePath,FileMode.Open,FileAccess.Read)
Dim bw As New System.IO.BinaryReader(fs)
Dim byt() As Byte, i As Integer
byt = bw.ReadBytes(CInt(fs.Length))
i = byt.Length()
Response.ContentType = "application/msword"
' change attachment to inline if you want it to open
'in browser window
Response.AddHeader
("content-disposition","attachment;filename=myoutput.doc")
Response.OutputStream.Write(byt, 0, i)
Response.OutputStream.Close()
End Sub
hope this helps someone and saves you some time. Someone else may have
other suggestions and better code, but this is my 2cents.