Creating Word documents on page

  • Thread starter Thread starter enak
  • Start date Start date
E

enak

I have found some code that shows how to convert an html
form to Word. The message said to simply put the following
in the Page_load:

Response.ContentType = "application/ms-word"
Response.AddHeader("content-disposition", "inline;
filename=BP_CaseStatus.doc")

Now all I should have to do is create the page just like I
would if I were going to display it in the browser.

I create the document in the browser using code similar to
the following:


Response.Write("<table width=""100%"">")
Response.Write(" <tr height=1><td align=right><img
src=""images\bp_white2_icon.ico""></tr>")
Response.Write(" <tr><td align=center><font size=+3>My
Report</font></tr>")
Response.Write(" <tr><td align=center><font size=+3>BP
Cases Status Report</font></tr>")

'==============================================
'MAIN TABLE
'==============================================
Response.Write(" <tr><td>")
Response.Write(" <table border=0 width=""100%""
cellspacing=3 cellpadding=0>")
Response.Write(" <tr bgcolor=#bbbbbb><td>Lead
Plaintiff<td>Jurisdiction<td>Court<td>In-House
Counsel<td># of Pltfs<td>Status<td>BP Billing
Breakdown</tr>")
Response.Write(" </table>")
Response.Write(" </tr>")
'==============================================
'MAIN TABLE
'==============================================
Response.Write("</table>")


The page loads fine until I try to load it as a Word
document. I know that someone out there has done this.

Can anyone help?

Thanks
enak
 
What do you mean by "get fine until....". What eroor do
you get ?

and why do you use Response.Write ? Are you doing ASP.NET
or ASP ?
 
Thanks for your response.

I am using asp.net. If I try to display the page with just
the HTML the page displays as I would expect.

However, if I add the following two lines the page is
blank:

Response.ContentType = "application/ms-word"
Response.AddHeader("content-disposition", "inline;
filename=BP_CaseStatus.doc")

Currently, I am using HTML but should I be doing it
differently? Can I use a datagrid rather than <table>?

Thanks,
enak
 
I just tried your code and it works (not if you do this in
a page load. Only at a postback from an button_click event
for example).

This can work only if you have at least office 2000 and IE
5.5.
 
I need this to work from the Page_Load. What I do is on
the Page_Load I call a sub that creates the page in HTML.
I have the two lines for the Word object in the Page_Load.

Do you know how I can make this work?

I have ie 5.5 and office 2000
 
Hi Enak,

I am sorry if there is any misunderstanding.

As I understand, what you want is exporting data to MS word. I have found
some articles regarding how to Export data to Excel. I hope it helps.

Export to Excel
http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so
_l.asp
"...
Use ASP.NET to send functionally rich reports to your users.
..."

HOW TO: Export Data in a DataGrid on an ASP . NET WebForm to Microsoft Excel
http://support.microsoft.com/default.aspx?scid=kb;en-us;317719

In addition, please upgrade to IE6 and test this issue again.

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Jacob, thanks for doing the research.

My original post talked about something a little different.

I can create the page using HTML and tables. It displays
just fine but then I read on ASP.Net forum that once I
have done that all I need to do is add the following to
the Page_Load function and the HTML will be converted to
Word.

Response.ContentType = "application/vnd.ms-word"
Response.AddHeader("content-disposition", "inline;
filename=BP_CaseStatus.doc")

This brings up a page that does not look anything like
mine. Do you have any experience with this?

Thanks
enak
 
Hi Enak,

Thank you for your update.

I have already tested your code on my side. Please try the following steps
on your side. (I am using Windows XP Pro/VS.NET 2003/IE 6)

1. Create a default web application.

2. Add the following code in Page_Load.

Response.Write("<table width=""100%"">")
Response.Write(" <tr><td align=center><font size=+3>My
Report</font></tr>")
Response.Write(" <tr><td align=center><font size=+3>BP Cases Status
Report</font></tr>")

'==============================================
'MAIN TABLE
'==============================================
Response.Write(" <tr><td>")
Response.Write(" <table border=0 width=""100%"" cellspacing=3
cellpadding=0>")
Response.Write(" <tr bgcolor=#bbbbbb><td>Lead
Plaintiff<td>Jurisdiction<td>Court<td>In-House Counsel<td># of
Pltfs<td>Status<td>BP Billing Breakdown</tr>")
Response.Write(" </table>")
Response.Write(" </tr>")
'==============================================
'MAIN TABLE
'==============================================
Response.Write("</table>")

3. Build and Run the application.

4. We can see the table we creat.

5. Right click the page and select "View Source". We can see the following
HTML code.
----------------------------------------------
<table width="100%"> <tr><td align=center><font size=+3>My
Report</font></tr> <tr><td align=center><font size=+3>BP Cases Status
Report</font></tr> <tr><td> <table border=0 width="100%" cellspacing=3
cellpadding=0> <tr bgcolor=#bbbbbb><td>Lead
Plaintiff<td>Jurisdiction<td>Court<td>In-House Counsel<td># of
Pltfs<td>Status<td>BP Billing Breakdown</tr> </table> </tr></table>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE"
value="dDwtMTI3OTMzNDM4NDs7PkHVmr4UQqVvjWev6bWIe9oveFlT" />

<FONT face="Arial"></FONT>
</form>
</body>
</HTML>
----------------------------------------------

6. Then please add the following two statements in Page_Load.

Response.ContentType = "application/ms-word"
Response.AddHeader("content-disposition", "inline;
filename=BP_CaseStatus.doc")

7. Build and run the application again. After click the "Open" button, we
can find that only the content between "<HTML>" and "</HTML>" are converted
to word. That is the reason of this issue.

8. Then please change the code in Page_Load as the following:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Response.ContentType = "application/ms-word"
Response.AddHeader("content-disposition", "inline;
filename=BP_CaseStatus.doc")
End Sub

9. Change the HTML code of the web form as the following:
--------------------------------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="TestWord.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="Arial"></FONT>
</form>
<table width="100%">
<tr>
<td align="center"><font size="+3">My Report</font></td>
<tr>
<td align="center"><font size="+3">BP Cases Status Report</font></td>
<tr>
<td>
<table border="0" width="100%" cellspacing="3" cellpadding="0">
<tr bgcolor="#bbbbbb">
<td>
Lead Plaintiff<td>
Jurisdiction<td>
Court<td>
In-House Counsel<td>
# of Pltfs<td>
Status<td>BP Billing Breakdown</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</HTML>
--------------------------------------------------------------------

10. Build and run the application. Now we can see the table in MS word.

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thank you for clearing that up.

I see that you are creating the report in HTML and then
converting it to Word. However, I get all of the data for
the report from an SQL 2000 database. That is why I was
trying to do all of the coding from the code_behind page.

In order to continue to do this I have tried to write the
entire table to a string and pass that string to the aspx
page. This does not work. I am not getting the function
called when the form loads. Here is what I am doing:

<HTML>
<HEAD>
<title>WordForm</title>
<%
'Response.ContentType = "application/vnd.ms-word"
'Response.AddHeader("content-
disposition", "inline; filename=BP_CaseStatus.doc")
%>
<meta content="Microsoft Visual
Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1"
name="CODE_LANGUAGE">
<meta content="JavaScript"
name="vs_defaultClientScript">
<meta
content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<asp:Table Runat=server>
<asp:TableRow>
<asp:TableCell><%# getReportTable
() %></asp:TableCell>
</asp:TableRow>
</asp:Table>
</body>
</HTML>


CODE BEHIND

Public Function getReportTable() As String
Dim sUser As String = Mid(Page.User.Identity.Name,
InStr(Page.User.Identity.Name, "\") + 1)
Dim sPath As String =
Request.PhysicalApplicationPath 'gives the path where this
file exists

If Request.QueryString("report") = "Case Status
Report w/ BP Billing Data" Then
Return BPCaseStatusWithBillingData(sUser,
sPath)
End If

End Function

I have a breakpoint on the "If" statement but the debugger
never stops there. The function is never called.

Any ideas why?

Thanks
enak
-----Original Message-----
Hi Enak,

Thank you for your update.

I have already tested your code on my side. Please try the following steps
on your side. (I am using Windows XP Pro/VS.NET 2003/IE 6)

1. Create a default web application.

2. Add the following code in Page_Load.

Response.Write("<table width=""100%"">")
Response.Write(" <tr><td align=center><font size=+3>My
Report</font></tr>")
Response.Write(" <tr><td align=center><font size=+3>BP Cases Status
Report</font></tr>")

'==============================================
'MAIN TABLE
'==============================================
Response.Write(" <tr><td>")
Response.Write(" <table border=0 width=""100%"" cellspacing=3
cellpadding=0>")
Response.Write(" <tr bgcolor=#bbbbbb><td>Lead
Plaintiff<td>Jurisdiction<td>Court<td>In-House
Counsel said:
Pltfs<td>Status<td>BP Billing Breakdown</tr>")
Response.Write(" </table>")
Response.Write(" </tr>")
'==============================================
'MAIN TABLE
'==============================================
Response.Write("</table>")

3. Build and Run the application.

4. We can see the table we creat.

5. Right click the page and select "View Source". We can see the following
HTML code.
----------------------------------------------
<table width="100%"> <tr><td align=center><font size=+3>My
Report</font></tr> <tr><td align=center><font size=+3>BP Cases Status
Report</font></tr> <tr><td> <table border=0 width="100%" cellspacing=3
cellpadding=0> <tr bgcolor=#bbbbbb><td>Lead
Plaintiff<td>Jurisdiction<td>Court<td>In-House
Counsel said:
Pltfs<td>Status<td>BP Billing Breakdown</tr> </table>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form name="Form1" method="post"
action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE"
value="dDwtMTI3OTMzNDM4NDs7PkHVmr4UQqVvjWev6bWIe9oveFlT" /


<FONT face="Arial"></FONT>
</form>
</body>
</HTML>
----------------------------------------------

6. Then please add the following two statements in Page_Load.

Response.ContentType = "application/ms-word"
Response.AddHeader("content-disposition", "inline;
filename=BP_CaseStatus.doc")

7. Build and run the application again. After click the "Open" button, we
can find that only the content between "<HTML>"
and said:
to word. That is the reason of this issue.

8. Then please change the code in Page_Load as the following:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Response.ContentType = "application/ms-word"
Response.AddHeader("content- disposition", "inline;
filename=BP_CaseStatus.doc")
End Sub

9. Change the HTML code of the web form as the following:
---------------------------------------------------------- ----------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="TestWord.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="Arial"></FONT>
</form>
<table width="100%">
<tr>
<td align="center"><font
size="+3">My Report said:
<tr>
<td align="center"><font
size="+3">BP Cases Status Report said:
<tr>
<td>
<table border="0"
width="100%" cellspacing="3" cellpadding="0">
<tr bgcolor="#bbbbbb">
Court said:
 
Hi Enak,

Thank you for your update.

In your case, you need not to create the whole web page from scratch. You
just need to create the tables appropriately by using the
System.Web.UI.WebControls.Table class. After the tables are created, you
then can add them to the controls inheritance tree of the web page by using
the Page.Controls.Add() function.

I hope it helps.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jacob,
I have 2 tasks in my application regarding Word doc.

1. Could you please tell me how can I open a word template
at client's system with data(blob) from server database?
2. Clicking on OK button in the form it should save the
modified word doc data into the server?


My email id: (e-mail address removed)
Thanks in advance,
Raghu.
-----Original Message-----
Hi Enak,

Thank you for your update.

I have already tested your code on my side. Please try the following steps
on your side. (I am using Windows XP Pro/VS.NET 2003/IE 6)

1. Create a default web application.

2. Add the following code in Page_Load.

Response.Write("<table width=""100%"">")
Response.Write(" <tr><td align=center><font size=+3>My
Report</font></tr>")
Response.Write(" <tr><td align=center><font size=+3>BP Cases Status
Report</font></tr>")

'==============================================
'MAIN TABLE
'==============================================
Response.Write(" <tr><td>")
Response.Write(" <table border=0 width=""100%"" cellspacing=3
cellpadding=0>")
Response.Write(" <tr bgcolor=#bbbbbb><td>Lead
Plaintiff<td>Jurisdiction<td>Court<td>In-House
Counsel said:
Pltfs<td>Status<td>BP Billing Breakdown</tr>")
Response.Write(" </table>")
Response.Write(" </tr>")
'==============================================
'MAIN TABLE
'==============================================
Response.Write("</table>")

3. Build and Run the application.

4. We can see the table we creat.

5. Right click the page and select "View Source". We can see the following
HTML code.
----------------------------------------------
<table width="100%"> <tr><td align=center><font size=+3>My
Report</font></tr> <tr><td align=center><font size=+3>BP Cases Status
Report</font></tr> <tr><td> <table border=0 width="100%" cellspacing=3
cellpadding=0> <tr bgcolor=#bbbbbb><td>Lead
Plaintiff<td>Jurisdiction<td>Court<td>In-House
Counsel said:
Pltfs<td>Status<td>BP Billing Breakdown</tr> </table>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form name="Form1" method="post"
action="WebForm1.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE"
value="dDwtMTI3OTMzNDM4NDs7PkHVmr4UQqVvjWev6bWIe9oveFlT" /


<FONT face="Arial"></FONT>
</form>
</body>
</HTML>
----------------------------------------------

6. Then please add the following two statements in Page_Load.

Response.ContentType = "application/ms-word"
Response.AddHeader("content-disposition", "inline;
filename=BP_CaseStatus.doc")

7. Build and run the application again. After click the "Open" button, we
can find that only the content between "<HTML>"
and said:
to word. That is the reason of this issue.

8. Then please change the code in Page_Load as the following:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Response.ContentType = "application/ms-word"
Response.AddHeader("content- disposition", "inline;
filename=BP_CaseStatus.doc")
End Sub

9. Change the HTML code of the web form as the following:
---------------------------------------------------------- ----------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="TestWord.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="Arial"></FONT>
</form>
<table width="100%">
<tr>
<td align="center"><font
size="+3">My Report said:
<tr>
<td align="center"><font
size="+3">BP Cases Status Report said:
<tr>
<td>
<table border="0"
width="100%" cellspacing="3" cellpadding="0">
<tr bgcolor="#bbbbbb">
Court said:
 
Back
Top