Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Convert Workbook with Multiple Worksheets into Text or CSV Format

Ranch Hand
Posts: 714
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This technical tip explains how developers can covert entire workbook into text or CSV format . Sometimes, you want to convert/save the entire workbook with multiple worksheets into text format. For text formats (e.g Txt, Tab Delimited, CSV etc), by default both Ms-Excel and Aspose.Cells save the contents of active worksheet only. The following code example explains how you can save your entire workbook into text format. We load the source workbook which could be any Ms-Excel or OpenOffice Spreadsheet (e.g xls, xlsx, xlsm, xlsb, ods etc) file and it could have any number of worksheets. After the execution of the code, it converts the data of all sheets inside the workbook into txt format. You can modify the same example to save your file into CSV format. By default TxtSaveOptions.Separator is comma, so you should not specify any separator if you want save your file in CSV format.

Sample Code for converting Entire Workbook into Text or CSV Format.


String filePath = "F:/Downloads/source.xlsx";

//Load your source workbook
Workbook workbook = new Workbook(filePath);

//0-byte array
byte[] workbookData = new byte[0];

//Text save options. You can use any type of separator
TxtSaveOptions opts = new TxtSaveOptions();

//Copy each worksheet data in text format inside workbook data array
for (int idx = 0; idx < workbook.getWorksheets().getCount(); idx++)
//Save the active worksheet into text format
ByteArrayOutputStream bout = new ByteArrayOutputStream();
workbook.getWorksheets().setActiveSheetIndex(idx);, opts);

//Save the worksheet data into sheet data array
byte[] sheetData = bout.toByteArray();

//Combine this worksheet data into workbook data array
byte[] combinedArray = new byte[workbookData.length + sheetData.length];
System.arraycopy(workbookData, 0, combinedArray, 0, workbookData.length);
System.arraycopy(sheetData, 0, combinedArray, workbookData.length, sheetData.length);

workbookData = combinedArray;

//Save entire workbook data into file
FileOutputStream fout = new FileOutputStream(filePath + ".out.txt");

Overview: Aspose.Cells is a Java

Aspose.Cells is a Java component for spreadsheet reporting without using Microsoft Excel. Other features include creating spreadsheets, opening encrypted excel files, macros, VBA, unicode, formula settings, pivot tables, importing data from JDBC ResultSet and support of CSV, SpreadsheetML, PDF, ODS and all file formats from Excel 97 to Excel 2007. It is compatible with Windows, Linux & Unix and supports all advanced features of data management, formatting, worksheet, charting and graphics.

More about Aspose.Cells is a Java

- Homepage of Aspose.Cells is a Java
- Download Aspose.Cells is a Java
- More Technical Tips by Aspose.Cells is a Java
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic