Click here to Skip to main content
15,671,149 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
I have a program which picks up values from an XML sheet and writes them into an excel sheet, using TestWriter.
FileStream ostrm;
                    StreamWriter strm;
                    TextWriter txtWrt = Console.Out;
                    ostrm = new FileStream(path + "\\Output.xls", FileMode.Truncate, FileAccess.Write);

Now the output file, Output.xls has multiple tabs, but I only have to truncate and write into the first sheet, leaving the other tabs untouched. I was using the below code, but it deleted all other tabs and edited the first tab.
public Excel.Application app = null;
        public Excel.Workbook workBook = null;
        public Excel.Worksheet workSheet = null;
        app = new Excel.Application();
                workBook = app.Workbooks.Add();
                workSheet = (Worksheet)workBook.Worksheets.get_Item(1);
                workBook.SaveAs(path+ "\\Output.xls");

I need to find a way by which this can be avoided.

Thanks in anticipation!

1 solution

Use Workbooks.Open[^] instead Workbooks.Add[^] ;)
Share this answer
Member 10426182 6-Dec-13 5:21am    
Duh!! Thanks fr pointing that out!! :D
Maciej Los 6-Dec-13 8:19am    
You're welcome ;)

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