Skip to content

InsertDataTable.cs

Avrigeanu Laurian edited this page Apr 12, 2023 · 1 revision

InsertDataTable(object sheet, int row, DataTable dataTable, bool deleteEntireSheet = true, bool dataTableHeader = true)

Parameters

  • sheet: An object representing the worksheet where the data is to be inserted.
  • row: The row number where the data insertion should start.
  • dataTable: The DataTable containing the data to be inserted.
  • deleteEntireSheet (optional): A boolean value indicating whether to delete the entire sheet before inserting the data. The default value is true.
  • dataTableHeader (optional): A boolean value indicating whether to include the column headers of the DataTable in the sheet. The default value is true.

Returns

  • void

Remarks

  • This method inserts the data from a DataTable into a worksheet in an Excel workbook.
  • It starts inserting the data from the specified row number in the worksheet.
  • If deleteEntireSheet is set to true, it first deletes all the existing data in the worksheet before inserting the new data.
  • If dataTableHeader is set to true, it inserts the column headers of the DataTable as the first row in the worksheet.
  • It converts any DBNull values in the DataTable to empty strings before inserting the data.
  • The data is inserted in tab-separated format.
  • This method uses the Microsoft.VisualBasic.Strings class to join the column names and data values with tab separators before inserting them into the worksheet.

InsertDataTableModern(object sheet, DataTable dataTable, bool deleteEntireSheet = true, bool dataTableHeader = true, int startRow = 1, int startColumn = 1)

Parameters

  • sheet: An object representing the worksheet where the data is to be inserted.
  • dataTable: The DataTable containing the data to be inserted.
  • deleteEntireSheet (optional): A boolean value indicating whether to delete the entire sheet before inserting the data. The default value is true.
  • dataTableHeader (optional): A boolean value indicating whether to include the column headers of the DataTable in the sheet. The default value is true.
  • startRow (optional): The row number where the data insertion should start. The default value is 1.
  • startColumn (optional): The column number where the data insertion should start. The default value is 1.

Returns

  • void

Remarks

  • This method inserts the data from a DataTable into a worksheet in an Excel workbook.
  • It starts inserting the data from the specified row and column numbers in the worksheet.
  • If deleteEntireSheet is set to true, it first deletes all the existing data in the worksheet before inserting the new data.
  • If dataTableHeader is set to true, it inserts the column headers of the DataTable as the first row in the worksheet.
  • The data is inserted in the range starting from the specified row and column numbers in the worksheet.
  • This method uses the Microsoft.Office.Interop.Excel.Range interface to access the cells and ranges in the worksheet.
  • It also releases the COM objects after use to avoid memory leaks.
Clone this wiki locally