C# - Fill a grid to Excelfile
-
Hey, I want to create a .xlsx file based on data from a grid. My problem: I have values like 01234 (starts with zero) exporting them make 1234 X| ... so, how can I export it with right format? :confused: I fill the file with:
if (Excelpfad == "") return false; Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.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"; Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get\_Item(currentSheet); for (int i = 0; i < UsingGrid.Cols; i++) { for (int j = 0; j < UsingGrid.Rows; j++) { worksheet.Cells\[j + 1, i + 1\] = Convert.ToString(UsingGrid\[j, i\]); } } // Autosize cols for (int iExcel = 0; iExcel < UsingGrid.Cols; iExcel++) { ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns\[iExcel + 1, t\]).ColumnWidth = UsingGrid.get\_ColWidth(iExcel) \* 0.13171875; } if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xls") worksheet.SaveAs(Excelpfad, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, t, t, t, t, t, t, t, t); else if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xlsx") worksheet.SaveAs(Excelpfad, t, t, t, t, t, t, t, t, t);
and another question: how can I close that file correct? I have:
newWorkbook.Close(true, n, n);
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);but the "EXCEL.EXE" process still run thank you
-
Hey, I want to create a .xlsx file based on data from a grid. My problem: I have values like 01234 (starts with zero) exporting them make 1234 X| ... so, how can I export it with right format? :confused: I fill the file with:
if (Excelpfad == "") return false; Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.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"; Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get\_Item(currentSheet); for (int i = 0; i < UsingGrid.Cols; i++) { for (int j = 0; j < UsingGrid.Rows; j++) { worksheet.Cells\[j + 1, i + 1\] = Convert.ToString(UsingGrid\[j, i\]); } } // Autosize cols for (int iExcel = 0; iExcel < UsingGrid.Cols; iExcel++) { ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns\[iExcel + 1, t\]).ColumnWidth = UsingGrid.get\_ColWidth(iExcel) \* 0.13171875; } if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xls") worksheet.SaveAs(Excelpfad, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, t, t, t, t, t, t, t, t); else if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xlsx") worksheet.SaveAs(Excelpfad, t, t, t, t, t, t, t, t, t);
and another question: how can I close that file correct? I have:
newWorkbook.Close(true, n, n);
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);but the "EXCEL.EXE" process still run thank you
-
Hey, I want to create a .xlsx file based on data from a grid. My problem: I have values like 01234 (starts with zero) exporting them make 1234 X| ... so, how can I export it with right format? :confused: I fill the file with:
if (Excelpfad == "") return false; Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.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"; Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get\_Item(currentSheet); for (int i = 0; i < UsingGrid.Cols; i++) { for (int j = 0; j < UsingGrid.Rows; j++) { worksheet.Cells\[j + 1, i + 1\] = Convert.ToString(UsingGrid\[j, i\]); } } // Autosize cols for (int iExcel = 0; iExcel < UsingGrid.Cols; iExcel++) { ((Microsoft.Office.Interop.Excel.Range)worksheet.Columns\[iExcel + 1, t\]).ColumnWidth = UsingGrid.get\_ColWidth(iExcel) \* 0.13171875; } if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xls") worksheet.SaveAs(Excelpfad, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, t, t, t, t, t, t, t, t); else if (Excelpfad.Substring(Excelpfad.LastIndexOf(".")) == ".xlsx") worksheet.SaveAs(Excelpfad, t, t, t, t, t, t, t, t, t);
and another question: how can I close that file correct? I have:
newWorkbook.Close(true, n, n);
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);but the "EXCEL.EXE" process still run thank you
Mschauder wrote:
for (int j = 0; j < UsingGrid.Rows; j++) { worksheet.Cells[j + 1, i + 1] = Convert.ToString(UsingGrid[j, i]); } }
first question: something like:
r.NumberFormat = "@";//where r is a Excel.Range
Note this is language specific(system language and settings). Best way to do it is record a macro while you set the Format to text and look at the VBA produced and translate it to C#. This silly article is especially about formatting. And the export is a lot faster than the cell by cell aproach.