Solved

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

Posted on 2011-03-15
6
1,085 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

25 Experts available now in Live!

Get 1:1 Help Now