Programmatically fill/generate Word doc from C#

J

Joe Ross

I am an experienced developer, but when it comes to developing for Office,
I'm a complete novice. My last experience with doing anything Office
related dates back 4 or 5 years. Things have changed a bit since then.

What I'm attempting to do seems like it should be relatively
straightforward. From an ASP.NET (C#) web application, I want to generate a
Word document and stream it to the user. The document can either be
generated from scratch or loaded and filled in from some sort of template.

The Word document has a standard format to it, my code needs to "fill in the
blanks" based on data from our database. I've Google'd a bunch but have
been unable to get headed in the right direction.

From a high level, what are the options? I'd like something efficient (i.e.
I don't want to be kicking off a Word process every time this happens as the
server would bog down quickly).

Thanks for the assistance.
-joe
 
A

Alvin Bruney

why don't you just change the content type to application/ms-word and stream
it out to the browser. That seems to be the most simple, efficient way to go
Response.ContentType = "application/ms-word"
It would render whatever your file is in word format in the browser.

if you want to do something more sophisticated, then you can respond back.
 
J

Joe Ross

Thank you for the reply.

The content of the file itself is dependent on the records in the database.
So it's not simple streaming a file back from disk. Are you suggesting
simply using the Word object model to completely generate the document on
the fly and stream it back? If so, that sounds great. Is there a
beginner's guide/tutorial for using the Word object model in this manner?
My apologies for the simple question, but I am completely inexperienced when
it comes to programming any of the Office apps.

Thanks
-joe
 
A

Alvin Bruney

It's not an OWC solution but I don't know exactly what you are wanting to do
so try this. If it doesnt work for you then you may need so more heavy duty
stuff like the OWC

For example, if you had your data in a datagrid, then you could do something
like this
ExcelGrid.DataSource = ds;

ExcelGrid.DataBind();

Response.ContentType="application/vnd.ms-excel"; //word would be
'application/vnd.ms-word'

StringWriter tw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);

ExcelGrid.RenderControl(hw);

Response.Write(tw.ToString());

Response.End();

tw = null;

hw = null;

wf = null;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top