Solved

ASP.net to word using filestream. unabe to get carriage return

Posted on 2011-03-15
6
1,082 Views
Last Modified: 2012-06-27
Hi,

I am trying to write to a word document from ASP.net c# application. The idea is to copy a word template into a new document and then insert values of textboxes and gridviews into th document. I am trying to do this with Filestream but the problem is I am unable to get carriage return in word document after doing various things and trying for last 2 days.,
Please see the code below and advise if possible,

Thanks
Tan


protected void function2()
        {
            String strvalue, Srefno;
            Object td;

            StringWriter temp = new StringWriter();
            temp.Write(TextBox1.Text);

            td = Server.CreateObject("Scripting.FileSystemObject");
            System.IO.File.Copy(Server.MapPath("~/profile_images/test2.rtf"), @"\\" + QCostGlobals.CustomerQuoteFilePath);

            String strFilePath = @"\\" + QCostGlobals.CustomerQuoteFilePath;


            FileStream fs2 = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);


            StreamReader d = new StreamReader(fs2);

            d.BaseStream.Seek(0, SeekOrigin.Begin);

            string swrtarget = d.ReadToEnd(); //Read the file from Start to End in one go.

            d.Close(); // Close the Stream Reader


            //Set the path to open file2.rtf

            strFilePath = @"\\" + QCostGlobals.CustomerQuoteFilePath;

            //Open file file2.rtf in Read Write Mode

            FileStream fs1 = new FileStream(strFilePath, FileMode.Open, FileAccess.ReadWrite);

            StreamWriter s = new StreamWriter(fs1); //Declare a Stream Writer.


            //String str = Regex.Replace(swrtarget, "##Address##", newString, RegexOptions.Multiline);

            string str = swrtarget;
            str = str.Replace("<<Address>>", "^");
          
            foreach (string str3 in str.Split('^'))
            {
                s.WriteLine(str3);

                foreach (string str4 in TextBox1.Text.Split('\n'))
                {
                    s.WriteLine(str4 + ((char)13));
                    

                }
                //s.WriteLine('\n');
            }
           // sw.Close();

            s.Flush();

            //Close the stream writer.

            s.Close();

            fs1.Close();

            fs2.Close();

        }

Open in new window

0
Comment
Question by:tan_dev
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35139485
Hi,

Have you try to use Environment.NewLine? like this

str4 +  Environment.NewLine
0
 
LVL 2

Author Comment

by:tan_dev
ID: 35139498
I have tried that. And '\n' and also <br/> nothing seems to work
0
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35139544
ok try this

str4 + "\r\n";
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Author Comment

by:tan_dev
ID: 35140316
I have already tried this as well. Apparently \r\n does not render carriage return on word. therefore I have used the above mothod of splitting and writing using writeline. But that doesnt work either!
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 250 total points
ID: 35140576
ok, since you are working with a RTF file you can add the RTF indicador of a new line like this,

//RTF new line indicator
s.WriteLine(str4 + "\\par");

I have tested and works, please try it
0
 
LVL 2

Author Comment

by:tan_dev
ID: 35146289
this works like magic! thanks a lot!!!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now