Solved

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

Posted on 2011-03-15
6
1,086 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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