This section summarizes how DsExcel Java handles the spreadsheet documents(.xlsx files).
When you create a workbook using DsExcel Java and save it, you automatically export it to an external location or folder. When bringing an Excel file into DsExcel Java (importing a file or opening a file), you can either load the whole model of the imported spreadsheet, or just bring in only data. DsExcel Java provides open method to open a file with various import flags that can be accessed through setImportFlags method of the XlsxOpenOptions class. Similarly, to export a workbook as .xlsx file, you can use save method and provide various save options provided by DsExcel to specify what to skip and what to export. For more information about import and export options provided by DsExcel, see Import and Export Excel Options.
Refer to the following example code in order to import and export .xlsx document from the file name:
Java |
Copy Code |
---|---|
// Create a new workbook. Workbook workbook = new Workbook(); // Open xlsx file. workbook.open("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:
Java |
Copy Code |
---|---|
// Create a new workbook. var streamworkbook = new Workbook(); // Create a new file stream to open a file. InputStream openFile; try { openFile = new FileInputStream("Basic sales report1.xlsx"); // Open xlsx file. streamworkbook.open(openFile, OpenFileFormat.Xlsx); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // Create a new file stream to save a file. OutputStream out; try { out = new FileOutputStream("Exported-Stream.xlsx"); // Save workbook as xlsx file. streamworkbook.save(out, SaveFileFormat.Xlsx); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } |
As another common scenario, you might need to import only data from a spreadsheet or a cell range. To handle such scenarios, DsExcel Java provides importData method to facilitate efficient loading from the external worksheet or a cell range. For more information about import data only, see Import Data Only section below.
To import only data from a specified worksheet or a cell range, DsExcel Java 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.
Java |
Copy Code |
---|---|
// Create a new workbook Workbook workbook = new Workbook(); // Open an excel file. InputStream fileStream = getResourceStream("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". String[] names = Workbook.getNames(fileStream); // The InputStream of the Java platform cannot be read repeatedly, so you need to create another one. InputStream fileStream2 = getResourceStream("AgingReport.xlsx"); // Import the data of a table "'Aging Report'!tblAging" from the fileStream. Object[][] data = Workbook.importData(fileStream2, names[2]); // Assign the data to current workbook. workbook.getWorksheets().get(0).getRange(0, 0, data.length, data[0].length).setValue(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