[]
This section summarizes how GcExcel .NET handles the spreadsheet documents (.xlsx files).
When you create a workbook using GcExcel .NET and save it, you automatically export it to an external location or folder. While opening or loading a file in GcExcel .NET, you can choose whether to import whole model of the target spreadsheet, or just bring in only data. GcExcel .NET provides Open method to open a file with various import flags that can be accessed through ImportFlags property of the XlsxOpenOptions class. Similarly, to export a workbook as .xlsx file, you can use the Save method and provide various save option provided by GcExcel to specify what to skip and what to export. For more information about import and export options provided by GcExcel, see Import and Export Excel Options.
Refer to the following example code in order to import and export .xlsx document from the file name:
// Create a new workbook.
Workbook workbook = new Workbook();
// Open xlsx file.
workbook.Open(Path.Combine("Resource", "Basic sales report1.xlsx"), OpenFileFormat.Xlsx);
// Save workbook as xlsx file.
workbook.Save("Exported.xlsx", SaveFileFormat.Xlsx);
Refer to the following example code in order to import and export .xlsx document from a file stream:
// Create a new file stream to open a file.
using FileStream openFile = new FileStream(Path.Combine("Resource", "Basic sales report1.xlsx"), FileMode.OpenOrCreate, FileAccess.Read);
// Create a new workbook.
var streamworkbook = new GrapeCity.Documents.Excel.Workbook();
// Open xlsx file.
streamworkbook.Open(openFile, OpenFileFormat.Xlsx);
// Create a new file stream to save a file.
using FileStream saveFile = new FileStream("Exported-Stream.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
// Save workbook as xlsx file.
streamworkbook.Save(saveFile, SaveFileFormat.Xlsx);
As another common scenario, you might need to import only data from a spreadsheet or a cell range. To handle such scenarios, GcExcel.NET provides ImportData method to facilitate efficient loading from an external worksheet or a cell range. For more information about importing data only, see Import Data Only section below.
To import only data from a specified worksheet or a cell range, GcExcel.NET provides ImportData method which simply opens the worksheet and fetches the data for you. This method is useful in scenarios where only data is required and you do not need to deal with rest of the object model. The ImportData method uses name of the file or filestream and source name as main parameters. You can specify name of a worksheet, table or a range as the source of data. To fetch names of sheets and tables used in a file or file stream, the Workbook class provides GetNames method which returns an array of possible source names.
//create a new workbook
var workbook = new GrapeCity.Documents.Excel.Workbook();
// Open an excel file.
var fileStream = GetResourceStream("xlsx\\AgingReport.xlsx");
// Get the possible import names in the file.
// The names[0] and names[1] are sheet names: "Aging Report", "Invoices".
// The names[2] and names[3] are table names: "'Aging Report'!tblAging", "Invoices!tblInvoices".
var names = GrapeCity.Documents.Excel.Workbook.GetNames(fileStream);
// Import the data of a table "'Aging Report'!tblAging" from the fileStream.
var data = GrapeCity.Documents.Excel.Workbook.ImportData(fileStream, names[2]);
// Assign the data to current workbook.
workbook.Worksheets[0].Range[0, 0, data.GetLength(0), data.GetLength(1)].Value = data;
// Save to an excel file
workbook.Save("importdatafortable.xlsx");
While working with heavy files having multiple sheets, or many formulas, you may optimize the load performance by using ImportData method as it reads only data. The method also provides overloads where you can specify the range of target cells and can read that particular part only, even if your file contains huge amounts of data.
Limitation
Formula are not taken into consideration while using ImportData method, as CalcEngine does not work in such case. Hence, the cell value is set to null. In case a formula has cached value stored in the file, GcExcel returns that value.
If the worksheet name contains character !, such as "Sheet!1", the worksheetName cannot be parsed by calling ImportData(worksheetName), and this function returns null.
Note: In version v5, name of the parameter of ImportData method has been changed from worksheetName to sourceName. This has resulted into a breaking change for users using the prior version if their code used parameter name "worksheetName" while calling the ImportData method, For details, see Release Notes.