Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple piece of code which opens an existing excel sheet, and writes the some values in separate cells.

C#
FileStream ostrm;
                    StreamWriter strm;
                    TextWriter txtWrt = Console.Out;
                    ostrm = new FileStream(path + "\\Output.xls", FileMode.Truncate, FileAccess.ReadWrite);
                    strm = new StreamWriter(ostrm);
                    Console.SetOut(strm);
                    Console.Write("Method" + "\n\t");
                    Console.Write("Sheet" + "\t");
                    Console.Write("Time" + "\t");
                    Console.Write("Type" + "\t");
                    Console.Write("Tab" + "\t");
                    Console.Write("Environment" + "\t");
                    Console.Write("Iteration" + "\t");
                    Console.Write("User" + "\t");
                    Console.Write("Module" + "\t");
                    Console.Write("Sheet" + "\t");
                    Console.WriteLine();
                    Console.SetOut(txtWrt);


However, this code prints all the values in the same cell, using '\t' as a space function rather than switching to a new cell. Please point out where I am going wrong.

Thanks in anticipation!!
Posted
Comments
Richard MacCutchan 6-Dec-13 6:31am    
What you are writing is not an Excel file. You need to use the Excel.Interop class to write a properly structured .xls file.

As Richard pointed out you aren't writing an excel file. You are making a text file that has a .xls extension. So when you open it up, excel opens it and displays it as text probably all in one cell.

To work with real Excel files you will need to use the Excel.Interop. It certainly isn't as easy as what you are attempting.
 
Share this answer
 
Comments
Member 10426182 11-Dec-13 3:40am    
It would be really helpful if you can elaborate more on a possible solution.
bowlturner 11-Dec-13 9:20am    
It depends. Creating a .csv file correctly would allow it open up in Excel but as you probably already noticed the link in solution 3 would be a good place to start for creating an actual Excel document.
Alternatively, what you are writing looks more familiar to a .csv file.
You could write it with the .csv extension rather than .xls, and MS Excel will open it.
Just remember that Excel uses ";" as a default separator, or if you necessarily need to use "\t"
you can write "sep=\t" in the first line .
 
Share this answer
 
Follow this MSDN POST and as all members above said use Excel.Interop. You can get a help here also.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900