Create a excelfile in C# - save with version
-
Hey guys =), I want to create a ".xls" and ".xlsx" file in C#2008. Creating && saving is no problem, but how can I close it? I think I've a bug in my code... :doh:
//this is how to create and save
Microsoft.Office.Interop.Excel.Application excelApp = new ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Sheets excelSheets = newWorkbook.Worksheets;
string currentSheet = "Tabelle1"; //german name
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);worksheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\SaveAsTest.xlsx", t, t, t, t, t, t, t, t, t);
but now the question is... "how can I close this worksheet?" :confused: a ".close" property doesn't exist... I read, that I can close the workbook but the workbook uses a already existing file. Is it possible to assign the new created to a workbook? greets Mschauder :-D
-
Hey guys =), I want to create a ".xls" and ".xlsx" file in C#2008. Creating && saving is no problem, but how can I close it? I think I've a bug in my code... :doh:
//this is how to create and save
Microsoft.Office.Interop.Excel.Application excelApp = new ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook newWorkbook = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Sheets excelSheets = newWorkbook.Worksheets;
string currentSheet = "Tabelle1"; //german name
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);worksheet.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\SaveAsTest.xlsx", t, t, t, t, t, t, t, t, t);
but now the question is... "how can I close this worksheet?" :confused: a ".close" property doesn't exist... I read, that I can close the workbook but the workbook uses a already existing file. Is it possible to assign the new created to a workbook? greets Mschauder :-D
-
It looks like you need the Application.Quit()[^] method; don't you just love Microsoft?
MVP 2010 - are they mad?
Hey, worked fine! Now the next problem... my computer uses office 2007, I can open the ".xlsx" && ".xls" file smoothly... ;P But if I use the laptop with office 2003... sure I can't open the ".xlsx", but why can't I open the ".xls"???:confused: Is there a possibility to save the file, with the natural format for office 2003 (.xls) :confused: greets Mschauder :-O
-
Hey, worked fine! Now the next problem... my computer uses office 2007, I can open the ".xlsx" && ".xls" file smoothly... ;P But if I use the laptop with office 2003... sure I can't open the ".xlsx", but why can't I open the ".xls"???:confused: Is there a possibility to save the file, with the natural format for office 2003 (.xls) :confused: greets Mschauder :-O
I answer now my own question (sorry!): just use: t means
object t = Type.Missing;
//XlFileFormat.xlExcel8 suits to .xls format
worksheet.SaveAs("Test.xls", XlFileFormat.xlExcel8, t, t, t, t, t, t, t, t);and saving as office2007:
worksheet.SaveAs("Test.xls", t, t, t, t, t, t, t, t, t);