I am building an application that I want to do the following in its simple form.
1. Website loads up and there is a button that says create word document.
2. Click the button and the word document is created and stored on the hard drive in the web application folder.
I found some code on the net that will generate a word document but when you click the button and run the code it opens a prompt box asking you where to save the file. I do not want to be prompted but to have it write the file to disk automatically. I have
done this with text files before. Of course later on i can set all these values as to where the file should be saved in the web.config file.
Here is the sample code that prompts you where to save the file.
I am basically going to eventually pull a record from a database and then using the source from a word document in the code behind and place those fields in there and generate the document. This document would then be in a folder where peopel can reach them
and open when they wish to.
hey you just need to create WordDocument object, put your data into that and just save it, i mean you can use Filestream and write it on your desk wherever location you want, but just make sure you have write permissio in the directory
I managed to get this to work but having wierd issues. .
Issue#1 is this line of code :
File.WriteAllText(FilePathSave, strFileData); // is supposed to overwrite the file if it is there but doesn't.
Instead of overwriting the file it tosses a permissions denied. The asp.net account has full control as doesnt regular internet users account.
Issue #2:
I can run it somtimes and it will not display a Open, Save or Cancel Dialog box and other times it will. I can't figure out why the prompt comes up in the first place. And why it is intermittent.
protected void ResumeAutoTest_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/msword";
string strFileName = "GenerateTestDocument" + ".doc";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
StringBuilder strHTMLContent = new StringBuilder();
strHTMLContent.Append(" <h1 title='Heading' align='Center' style='font-family:verdana;font-size:80%;color:black'><u>Document Heading</u> </h1>".ToString());
strHTMLContent.Append("<br>".ToString());
strHTMLContent.Append("<table align='Center'>".ToString());
strHTMLContent.Append("<tr>".ToString());
strHTMLContent.Append("<td style='width: 100px;background:#99CC00'><b>Column 1</b></td>".ToString());
strHTMLContent.Append("<td style='width: 100px;background:#99CC00'><b>Column 2</b></td>".ToString());
strHTMLContent.Append("<td style='width: 100px;background:#99CC00'><b>Column 3</b></td>".ToString());
strHTMLContent.Append("</tr>".ToString());
strHTMLContent.Append("<tr>".ToString());
strHTMLContent.Append("<td style='width: 100px'>a</td>".ToString());
strHTMLContent.Append("<td style='width: 100px'>b</td>".ToString());
strHTMLContent.Append("<td style='width: 100px'>c</td>".ToString());
strHTMLContent.Append("</tr>".ToString());
strHTMLContent.Append("<tr>".ToString());
strHTMLContent.Append("<td style='width: 100px'>d</td>".ToString());
strHTMLContent.Append("<td style='width: 100px'>e</td>".ToString());
strHTMLContent.Append("<td style='width: 100px'>f</td>".ToString());
strHTMLContent.Append("</tr>".ToString());
strHTMLContent.Append("</table>".ToString());
strHTMLContent.Append("<br><br>".ToString());
strHTMLContent.Append("<p align='Center'> Note : This is dynamically generated word document </p>".ToString());
//StreamWriter sw = File.CreateText(MapPath("myFile.txt"));
//sw.WriteLine( txtFile.Text );
//sw.Close();
// Convert to string
string strFileData = strHTMLContent.ToString();
string strFolderValue = @"D:\CWEB\ResumeDatabase\Resumes\";
//System.IO.
DirectoryInfo Folder = new DirectoryInfo(strFolderValue);
if (Folder.Exists)
{
if (strFileData != string.Empty)
{
// build the path
string FilePathSave = Folder.ToString() + strFileName;
// if file exist delete and then save again
// else save file
File.WriteAllText(FilePathSave, strFileData); // is supposed to overwrite the file if it is there but doesn't
}
else
{
Response.Write("(Nothing to save)");
}
// If we can't find the folder
}
else
{
Response.Write("Folder [" + strFolderValue + "not found]");
}
//HttpContext.Current.Response.End();
//HttpContext.Current.Response.Flush();
// would display a resume complete panel etc.. but for now just goes back to the page.
//Response.Redirect("ResumeTests.aspx");
Response.Write("File saved to [" + strFolderValue + "strFileName]");
Response.End();
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Run Time Word Document Creation and Save";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
//Insert a paragraph at the end of the document.
Word.Paragraph oPara2;
object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara2.Range.Text = "path c:";
oPara2.Format.SpaceAfter = 6;
oPara2.Range.InsertParagraphAfter();
//Insert another paragraph.
Word.Paragraph oPara3;
oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";
oPara3.Range.Font.Bold = 0;
oPara3.Format.SpaceAfter = 24;
oPara3.Range.InsertParagraphAfter();
The best way to manipulate Word document is to create a class and call that from your ASP.NET application. The code to generate tables, images, font styles, etc can be recorded. The resultng macro code can be pasted in your class. BTW, Word will produce
VB code.
You need to have the .NET Programmability Support installed. Then click Customize Quick Access Toolbar and select "More Commands...", then select "Record Macro / Stop Recorder". Click "Record Macro" and enter macro name. Make the necessary update in Microsoft
Word such as type some text, add a picture / image, apply the necessary settings into the picture / image, create a table, add text header and row/column values, etc. The point is, whatever you do in Microsoft Word can be recorded and the resulting code can
used in your .NET class with some minor tweaks. You need to reference Microsoft Word interop or library in your class and the Word object derived from Word.Application must be appended to each Microsoft Word function / method call.
Lastly, please take note that I am using Microsoft Office 2010.
Member
9 Points
175 Posts
Using ASP.NET to create a word document to disk
Jun 09, 2010 11:04 AM|belcherman|LINK
Hi all,
I am building an application that I want to do the following in its simple form.
1. Website loads up and there is a button that says create word document.
2. Click the button and the word document is created and stored on the hard drive in the web application folder.
I found some code on the net that will generate a word document but when you click the button and run the code it opens a prompt box asking you where to save the file. I do not want to be prompted but to have it write the file to disk automatically. I have done this with text files before. Of course later on i can set all these values as to where the file should be saved in the web.config file.
Here is the sample code that prompts you where to save the file.
I am basically going to eventually pull a record from a database and then using the source from a word document in the code behind and place those fields in there and generate the document. This document would then be in a folder where peopel can reach them and open when they wish to.
doc docx
Contributor
2312 Points
633 Posts
Re: Using ASP.NET to create a word document to disk
Jun 09, 2010 11:33 AM|nizam133|LINK
hey you just need to create WordDocument object, put your data into that and just save it, i mean you can use Filestream and write it on your desk wherever location you want, but just make sure you have write permissio in the directory
Member
9 Points
175 Posts
Re: Using ASP.NET to create a word document to disk
Jun 09, 2010 06:14 PM|belcherman|LINK
Is there any example of this out there? Basically all i need it to have it automatically write to disk. I have done it wit text files a while back.
Thanks for the replies.
Member
9 Points
175 Posts
Re: Using ASP.NET to create a word document to disk
Jun 11, 2010 05:03 PM|belcherman|LINK
I managed to get this to work but having wierd issues. .
Issue#1 is this line of code :
File.WriteAllText(FilePathSave, strFileData); // is supposed to overwrite the file if it is there but doesn't.
Instead of overwriting the file it tosses a permissions denied. The asp.net account has full control as doesnt regular internet users account.
Issue #2:
I can run it somtimes and it will not display a Open, Save or Cancel Dialog box and other times it will. I can't figure out why the prompt comes up in the first place. And why it is intermittent.
any ideas?
Thanks,
Member
191 Points
104 Posts
Re: Using ASP.NET to create a word document to disk
Jun 19, 2010 02:32 AM|Article|LINK
Hi..
On Fly Word Document This code May Help You
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;
public partial class WordAutomation : System.Web.UI.Page
{
// private Word.ApplicationClass oWord;
protected void Page_Load(object sender, EventArgs e)
{
string fileName1 = @"c:\a.doc";
wordwin.Attributes["src"] = fileName1.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
object nullobj = System.Reflection.Missing.Value;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Run Time Word Document Creation and Save";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
//Insert a paragraph at the end of the document.
Word.Paragraph oPara2;
object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara2.Range.Text = "path c:";
oPara2.Format.SpaceAfter = 6;
oPara2.Range.InsertParagraphAfter();
//Insert another paragraph.
Word.Paragraph oPara3;
oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:";
oPara3.Range.Font.Bold = 0;
oPara3.Format.SpaceAfter = 24;
oPara3.Range.InsertParagraphAfter();
object fileName1 =@"c:\a.doc";
oDoc.SaveAs(ref fileName1, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
oDoc.Close(ref nullobj, ref nullobj, ref nullobj);
oWord.Quit(ref nullobj, ref nullobj, ref nullobj);
//wordwin.Attributes["src"] = fileName1.ToString();
}
}
prompt box asking you where to save the file. I do not want to be prompted but to have it write the file to disk automatically
This Is In IE this dilog Box have Check Box Check it.(read the dilog Box message.)
Thank's
Prasanta Kumar Pradhan.
http://globaltouch.in
http://www.csc.com
None
0 Points
4 Posts
Re: Using ASP.NET to create a word document to disk
Oct 24, 2010 12:57 AM|trying2code|LINK
Hey I want to populate table contents from DB and have created the header of the table using the method you gave above....
Can you tell me how to work that part out...
Sandeep
Member
127 Points
79 Posts
Re: Using ASP.NET to create a word document to disk
Sep 07, 2011 12:55 AM|ramakrishnan27|LINK
hi ,
the code which u have given working fine but i want put image in the document .how can i do it..
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Member
70 Points
26 Posts
Re: Using ASP.NET to create a word document to disk
Sep 10, 2011 02:22 PM|kneville|LINK
The best way to manipulate Word document is to create a class and call that from your ASP.NET application. The code to generate tables, images, font styles, etc can be recorded. The resultng macro code can be pasted in your class. BTW, Word will produce VB code.
You need to have the .NET Programmability Support installed. Then click Customize Quick Access Toolbar and select "More Commands...", then select "Record Macro / Stop Recorder". Click "Record Macro" and enter macro name. Make the necessary update in Microsoft Word such as type some text, add a picture / image, apply the necessary settings into the picture / image, create a table, add text header and row/column values, etc. The point is, whatever you do in Microsoft Word can be recorded and the resulting code can used in your .NET class with some minor tweaks. You need to reference Microsoft Word interop or library in your class and the Word object derived from Word.Application must be appended to each Microsoft Word function / method call.
Lastly, please take note that I am using Microsoft Office 2010.
None
0 Points
3 Posts
Re: Using ASP.NET to create a word document to disk
May 16, 2013 05:23 AM|jallad.abdullah|LINK
How i can add an Image to this genarated document in the table for example?
thanks in advance
Regards,
Abdullah Jallad