Category / Section
How to use ImportOnSave option while importing data using XlsIO?
1 min read
ImportDataTable overload method which has ImportOnSave argument allows to import data with less memory consumption along with improved method performance by serializing the data directly on save method. This option is preferred for larger data that need to be imported in short time.
The following complete code snippet explains this.
//Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; FileStream inputStream = new FileStream("NorthwindDataTemplate.xls", FileMode.Open); IWorkbook existing_workbook = application.Workbooks.Open(inputStream); IWorksheet existing_worksheet = existing_workbook.Worksheets[0]; DataTable table = existing_worksheet.ExportDataTable(1, 1, existing_worksheet.UsedRange.LastRow, existing_worksheet.UsedRange.LastColumn, ExcelExportDataTableOptions.DetectColumnTypes); IWorkbook new_workbook = application.Workbooks.Create(1); new_workbook.Worksheets[0].ImportDataTable(table, 1, 1, true, true); new_workbook.Version = ExcelVersion.Excel2013; //Save the document as a stream and retrun the stream. using (MemoryStream stream = new MemoryStream()) { //Save the created Excel document to MemoryStream new_workbook.SaveAs(stream); return stream; } }
A complete working sample can be downloaded from ImportOnSave.zip.
See Also: