This technical tip explains how developers can convert 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


StringfilePath = "F:/Downloads/source.xlsx";

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

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

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

//Copy each worksheet data in text format inside workbook data array
for (intidx = 0; idx<workbook.getWorksheets().getCount(); idx++)
//Save the active worksheet into text format
ByteArrayOutputStream bout = newByteArrayOutputStream();
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 = newbyte[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
FileOutputStreamfout = newFileOutputStream(filePath + ".out.txt");