0% found this document useful (0 votes)
28 views1 page

Add From Grid Into Excel

This document discusses how to append data to an existing Excel file using C#. It describes opening an existing Excel workbook, getting the active worksheet, finding the last used row, calling a method to add data starting from the next row, saving the workbook, closing it, and cleaning up resources. The key steps are opening the workbook, getting the worksheet, finding the last row, adding data starting from the next empty row, saving, and closing.

Uploaded by

danccelena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views1 page

Add From Grid Into Excel

This document discusses how to append data to an existing Excel file using C#. It describes opening an existing Excel workbook, getting the active worksheet, finding the last used row, calling a method to add data starting from the next row, saving the workbook, closing it, and cleaning up resources. The key steps are opening the workbook, getting the worksheet, finding the last row, adding data starting from the next empty row, saving, and closing.

Uploaded by

danccelena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

https://stackoverflow.

com/questions/41841251/appending-data-to-existing-excel-file-using-c-sharp

xlApp = new Microsoft.Office.Interop.Excel.Application();


xlApp.Visible = true;
xlWorkBook = xlApp.Workbooks.Open(newPath, 0,
false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"", true, false, 0, true, false, false);

xlBigSheet = xlWorkBook.Worksheets;
string x = "Sheet1";
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBigSheet.get_Item(x);

////////////////////////////////////////////////////////////////////////////////////////////////////////////

.
.

xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBigSheet.get_Item("Sheet1");
Microsoft.Office.Interop.Excel.Range last =
xlWorkSheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
int lastUsedRow = last.Row;
getData(lastUsedRow + 1);
.
.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

xlWorkBook.SaveAs(newPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
misValue, misValue, misValue, misValue,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
misValue, misValue, misValue,
misValue, misValue);

xlWorkBook.Close(misValue, misValue, misValue);


xlWorkBook = null;
xlApp.Quit();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

You might also like