943,747 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 4258
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 10th, 2009
0

append in word document using c#

Expand Post »
hello everyone
i have a problem in writing in word document the proble is that i want to append a string after a table i made in that document but it overides it the code is:-
C# Syntax (Toggle Plain Text)
  1. //Text to word file
  2. StringBuilder strBuilder = newStringBuilder();
  3. strBuilder.Append("<h1 title='Header' align='Center'>Writing To Word Using ASP.NET</h1> ".ToString());
  4. strBuilder.Append("<br>".ToString());
  5. strBuilder.Append("<table align='Center'>".ToString());
  6. strBuilder.Append("<tr>".ToString());
  7. strBuilder.Append("<td style='width:100px;color:green'><b>amiT</b></td>".ToString());
  8. strBuilder.Append("<td style='width:100px;color:red'>India</td>".ToString());
  9. strBuilder.Append("</tr>".ToString());
  10. strBuilder.Append("</table>".ToString());
  11. // string strPath = Request.PhysicalApplicationPath + "\\document\\Test.doc";
  12. string strPath = filename;
  13. //string strTextToWrite = TextBox1.Text;
  14. FileStream fStream = newFileStream (strPath,FileMode.Append);
  15. fStream.Close();
  16. StreamWriter sWriter = newStreamWriter(strPath);
  17. sWriter.Write(strBuilder);
  18. sWriter.Close();

any help would be appreciated
Last edited by John A; Aug 10th, 2009 at 10:18 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
fafi_ali is offline Offline
30 posts
since Jul 2009
Aug 13th, 2009
0

Re: append in word document using c#

Click to Expand / Collapse  Quote originally posted by fafi_ali ...
C# Syntax (Toggle Plain Text)
  1. FileStream fStream = newFileStream (strPath,FileMode.Append);
  2. fStream.Close();
  3. StreamWriter sWriter = newStreamWriter(strPath);
  4. sWriter.Write(strBuilder);
  5. sWriter.Close();

any help would be appreciated

You probably need to seek to the end of the file? It seems like that would be the way to go. The Streamwriter starts at position 0, I think, and just clobbers whatever is there...
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Aug 15th, 2009
0

Re: append in word document using c#

[QUOTE=alc6379;948080]You probably need to seek to the end of the file?

would you just tell me how to do that?
thnaks alot for your reply
appreciated
Reputation Points: 10
Solved Threads: 0
Light Poster
fafi_ali is offline Offline
30 posts
since Jul 2009
Aug 15th, 2009
0

Re: append in word document using c#

Click to Expand / Collapse  Quote originally posted by fafi_ali ...
C# Syntax (Toggle Plain Text)
  1. FileStream fStream = newFileStream (strPath,FileMode.Append);
  2. fStream.Close();
  3. StreamWriter sWriter = newStreamWriter(strPath);
  4. sWriter.Write(strBuilder);
  5. sWriter.Close();
C# Syntax (Toggle Plain Text)
  1. FileStream fStream = newFileStream (strPath,FileMode.Append);
  2. //fStream.Close();
  3. //StreamWriter sWriter = newStreamWriter(strPath);
  4. StreamWriter = new StreamWriter(fStream);
  5. sWriter.Write(strBuilder);
  6. sWriter.Flush();
  7. sWriter.Close();
The changes should fix your append at least. Not sure if you are even getting it to compile though.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 21st, 2009
0

Re: append in word document using c#

Click to Expand / Collapse  Quote originally posted by DdoubleD ...
C# Syntax (Toggle Plain Text)
  1. FileStream fStream = newFileStream (strPath,FileMode.Append);
  2. //fStream.Close();
  3. //StreamWriter sWriter = newStreamWriter(strPath);
  4. StreamWriter = new StreamWriter(fStream);
  5. sWriter.Write(strBuilder);
  6. sWriter.Flush();
  7. sWriter.Close();
The changes should fix your append at least. Not sure if you are even getting it to compile though.
actually they did nothing i don,t know why but any way appreciated
sorry for replying lately
Reputation Points: 10
Solved Threads: 0
Light Poster
fafi_ali is offline Offline
30 posts
since Jul 2009
Aug 21st, 2009
0

Re: append in word document using c#

Click to Expand / Collapse  Quote originally posted by fafi_ali ...
actually they did nothing i don,t know why but any way appreciated
sorry for replying lately
No problem. When you say "did nothing", I really don't know what that means.

I'm not sure if know, but you can append to any file you want. However, that doesn't mean that the reader (MS Word in this case) of the file will understand the data appended.

Anyway, if you will post your code so I can run it on my end, I will help you figure it out.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 22nd, 2009
0

Re: append in word document using c#

Click to Expand / Collapse  Quote originally posted by DdoubleD ...
Anyway, if you will post your code so I can run it on my end, I will help you figure it out.
thanks alot
that's my project and it's databases
my code is supposed to make crystal report ,save it as a word document then append some words in that word document
would you just press "save report" in form1.cs then "save " in form2.cs the append code in save menustripitem handler in form2.cs

thanks alot for ur help it's really appreciated
Attached Files
File Type: zip data.zip (23.7 KB, 73 views)
File Type: zip work.zip (823.8 KB, 118 views)
Reputation Points: 10
Solved Threads: 0
Light Poster
fafi_ali is offline Offline
30 posts
since Jul 2009
Aug 22nd, 2009
0

Re: append in word document using c#

Click to Expand / Collapse  Quote originally posted by fafi_ali ...
thanks alot
that's my project and it's databases
my code is supposed to make crystal report ,save it as a word document then append some words in that word document
would you just press "save report" in form1.cs then "save " in form2.cs the append code in save menustripitem handler in form2.cs

thanks alot for ur help it's really appreciated
I've compiled the project and am looking at the warnings, particularly the missing reference: Microsoft.Office.Interop.Word

I am installing the class library download and will get back to you...
Last edited by DdoubleD; Aug 22nd, 2009 at 1:51 pm. Reason: Found the download
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 22nd, 2009
0

Re: append in word document using c#

Click to Expand / Collapse  Quote originally posted by fafi_ali ...
i have a problem in writing in word document the proble is that i want to append a string after a table i made in that document but it overides it...
Ok, your text is being appended to the word document, as you have told it to do. The problem is how Word is interpreting the document. When debugging, I saw that the string "hank" was indeed getting in the document, so I played with some formatting changes in the code and used BinaryWriter instead of StreamWriter so I could position the stream 1 position away from the EOF:
C# Syntax (Toggle Plain Text)
  1. //StreamWriter sWriter = new StreamWriter(fStream);
  2. BinaryWriter sWriter = new BinaryWriter(fStream);
  3. sWriter.Seek(1, SeekOrigin.End);
  4. sWriter.Write("hany}");
Now, Word will display the appended text on a new page. Notice the addition of the closing brace in the text: "hany}". That is because I overwrote the closing brace already existing in the file (with Seek) and I wanted word to see "hany" before the final closing brace.

What you are dealing with at this point is Word scripting and you need to figure out how you can modify word documents externally or use some library that facilitates doing so, instead of using standard stream writers.

If the word "hany" is all you need appear at the end of the doc, and you wanted it to appear on the same page, you might be able to modify the export options to not fill the entire page with the table, then simply use the quick fix I just gave you.

Does this make sense?
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Aug 22nd, 2009
0

Re: append in word document using c#

As an after thought, have you considered just adding a field to the end of your report that you can set to text "hany", or whatever, so that when it is exported it will properly format it in the word document? Not sure exactly what you need to do for your project, but a text field in the report or page footer section seems like it might be the way to go.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: DataGridView AutoSizeRowsMode Fill ?
Next Thread in C# Forum Timeline: how do i use HttpRequest/HttpContext





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC