[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2011-03-15
6
Medium Priority
?
1,101 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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