Reading/Extracting text of Excel file with Aspose.Cells in Java

Hi team,

I’m struggling to find an elegant solution on how to get the text from the excel file. For Aspose.Pdf, this is pretty straightforward, but for Aspose.Cells it seems it is very cumbersome. Would you please provide an example how I could read the text of the file?
Thanks, Robert

Hi,


Thanks for your query.

I think you may try to save your Excel file to CSV or txt file format, see the the document if it helps you to accomplish the task:

Thank you.

Thanks for the quick response.


However, I can’t be creating additional temp files - in this case, is my chance going through all the cells in all worksheets in the workbook and adding them to something like a string builder? If so, is there an example for this please?

Thanks, Robert

Hi Robert,


Thank you for writing back.

I believe, Amjad’s suggested solution is more elegant and simple. Using that approach, you can write all contents of a worksheet to a single CSV or Text file while specifying the desired delimiter. Moreover, the data will be organized in rows. However, if you still wish to iterate all cells and write data to an instance of StringBuilder, you may try the following approach.

Please note, code shared here is for demonstration purposes so you should amend it according to your application requirements.

Java

StringBuilder stringBuilder = new StringBuilder();
Workbook book = new Workbook(dir + “book1.xlsx”);
Worksheet sheet = book.getWorksheets().get(0); //retrieve first worksheet from collection
Cells cells = sheet.getCells();
Range displayRange = cells.getMaxDisplayRange();
for(int row = displayRange.getFirstRow(); row<displayRange.getRowCount(); row++)
{
for(int col = displayRange.getFirstColumn(); col<displayRange.getColumnCount(); col++)
{
stringBuilder.append(displayRange.get(row, col).getStringValue());
stringBuilder.append(" ");
}
}
System.out.println(stringBuilder.toString());

Hi again,


Please note, if the requirement is to process all cells in a Worksheet or a range, it is recommended to use Enumerator (Iterator for Java) instead of accessing individual cells in a for loop. This is because the APIs such as MaxRow, MaxDataRow, MaxColumn, MaxDataColumn & MaxDisplayRange require extra time to gather the corresponding statistics. Secondly, commonly not all cells in a range are instantiated. In such situations to check every cell in the matrix is not so efficient. Moreover, accessing a cell in a loop as Cells[row, column] (Cells.get(row, column) for Java) will cause all cell objects in a range to be instantiated, which may eventually cause OutOfMemoryException.

Please check the following updated code segment that achieves the same results more efficiently as compared to the previously shared code snippet.

Java

StringBuilder stringBuilder = new StringBuilder();
Workbook book = new Workbook(dir + “book1.xlsx”);
Worksheet sheet = book.getWorksheets().get(0);
Cells cells = sheet.getCells();
Iterator iterator = cells.iterator();
while(iterator.hasNext())
{
Cell cell = (Cell)iterator.next();
stringBuilder.append(cell.getStringValue());
stringBuilder.append(" ");
}
System.out.println(stringBuilder.toString());

Hi Babar,

thank you for taking the time to provide me with the optimized solution. Much appreciated!
Thanks, Robert

Hi Robert,


You are most welcome. Please feel free to contact us back in case you need our further assistance with Aspose APIs.

I want to put the excel file data in jtable …I have read the excel file with the help of this code.

@tanishaa32,

Thanks for your query.

Well, I think you may try to export data (in Excel spreadsheet) to fill an array (see the document on how to export data to an array via Aspose.Cells for Java APIs: Import and Export Data|Documentation) and then pass this Object[][] directly to JTable constructor.

Hii,
I have used this code to read my excel file but there is an error saying
com.aspose.cells.Cell cannot be cast to org.apache.poi.ss.usermodel.Cell

I have tried but I think it is not working as the example is exporting only few col and I want the file to be export dynamically.

@tanishaa32

Both Aspose.Cells for Java and Apache POI are different APIs and diverse with different architectures so you cannot parse one’s object into other directly.

Well, Cells.exportArray() method would export an object array for single worksheet cells only, so you have to write your own code and got to loop through each worksheet to export the arrays. Moreover, you have to export the array based on the complete dataset in the worksheet, you may update the third and fourth parameters in the method accordingly.

I want to write the data in excel file after comparing two different excel files I have stored the data in ArrayList and the data is also writing in third file but the proble here arises that they are not setting in rows and column from starting…If I tried to increment the col then data is stored diagonally otherwise in one col only…how can I put the data in all rows and col like excel file dynamically.

@tanishaa32,

Well, Aspose.Cells only provides feasibility to either retrieve the data cell by cell in iterator from the sheet or export to fill an array/array list. To import data into worksheets, the component also provides some useful methods to import data from array, array list of resultset, etc., see the document with examples for your reference:

The methods may place data horizontally or vertically in the cells.

I am afraid, to insert specific data or record (from array/array list) at your desired place in the worksheet cells, you got to browse through the array/array list elements by yourselves and place it one by one accordingly for your custom needs, there is no automatic way to cope with it.

@Amjad_Sahi Hello, I would like to ask. Is there any documentation if I get the excel file from user by using httprequest post to an API, how I could get the directory of the path file that will be used for below code:

String dataDir = Utils.getSharedDataDir(UsingRowAndColumnIndexOfCell.class) + “data/”;

// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + “book1.xls”);

thank you in advance

@arkaz,

Thanks for your query.

These forums are specific to Aspose APIs only. Well, your query is out of context and you should browse internet to accomplish the task by yourself as you have to write your own code to send a file as http request post. For example, see the following threads (external) for your reference: