?
Solved

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

Posted on 2011-03-15
6
Medium Priority
?
1,099 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1000 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

741 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