How to on word doc output (page setup, streaming html and datagrid, open file)

Discussion in 'ASP .Net' started by Andrew, Oct 4, 2005.

  1. Andrew

    Andrew Guest

    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.
    Andrew, Oct 4, 2005
    #1
    1. Advertising

  2. Andrew

    Andrew Guest

    on step 2 = save the file to the web application folder as newdoc.html

    forget step 3 as we can create it and delete it in the same routine

    change Export routine to below:
    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"),True)
    '<--- above will create output.doc file


    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()
    fs.Close() '<---- I forgot this in the prev version
    File.Delete(Server.MapPath("output.doc"))


    End Sub
    Andrew, Oct 5, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wallace
    Replies:
    0
    Views:
    460
    John Wallace
    Jul 22, 2003
  2. Stu
    Replies:
    2
    Views:
    7,342
  3. Franck
    Replies:
    1
    Views:
    4,815
  4. Matt
    Replies:
    3
    Views:
    484
    Tor Iver Wilhelmsen
    Sep 17, 2004
  5. Replies:
    2
    Views:
    1,086
Loading...

Share This Page