Android Tutorial Read / Write Excel files on Android

Status
Not open for further replies.
The Excel library is a new library that wraps the open source jexcel project and allows you to read or write Excel workbooks. This library supports XLS files. The new xml based format (xslx) is not supported.

Setup

- Download the attached library and copy both files to the libraries folder.
- Download jexcel library: A Java library for reading/writing Excel - Browse /jexcelapi/2.6.12 at SourceForge.net (jexcelapi_2_6_12.zip).
- Open the zip file and copy jxl.jar to the libraries folder.

Reading data

Reading data is quite simple. First we create a ReadableWorkbook object. The Initialize method opens the file and reads the data.
The next step is to create a ReadableSheet object. Calling Workbook.GetSheet(Index) returns a sheet:
B4X:
Dim workbook1 As ReadableWorkbook
Dim moviesSheet As ReadableSheet
workbook1.Initialize(Dir, FileName)
moviesSheet = workbook1.GetSheet(0)

ReadableSheet.GetCellValue(Col, Row) returns the value of a cell.

Note that all the indices (sheets, cells, rows and columns) are zero based.

Creating a new Workbook

The data is always written to a new file. It is not possible to directly modify an existing file.
WritableWorkbook.Initialize creates a new file.
The next step is to add a sheet with WritableWorkbook.AddSheet (Name, Index). The Index parameter determines the index of the new sheet.
B4X:
Dim newWorkbook As WritableWorkbook
newWorkbook.Initialize(File.DirRootExternal, "1.xls")
Dim sheet1 As WritableSheet
sheet1 = newWorkbook.AddSheet("Movies", 0)

Adding data is done by adding WritableCell objects to the sheet.
B4X:
Dim cell As WritableCell
cell.InitializeText(1, 0, "Hello")
sheet1.AddCell(cell)
The above code adds a cell at B0 with the value Hello.

You can also set the cell format. This is done with a WritableCellFormat. Note that one object can be used with multiple cells (that share the same format).
The attached example demonstrates the usage of this object.

The attached example first loads a workbook from the assets folder and shows the data with the Table class. When you press Save it creates a new workbook. You can copy this file (1.xls) to the desktop and see how it is formatted:

SS-2013-01-25_08.01.57.png


Modifying an existing workbook

As noted above it is not possible to directly modify a workbook file. Instead you need to create a new file that will include a copy of the original workbook.
This is done by first loading the existing workbook with ReadableWorkbook.

Then you should create a WritableWorkbook and call Initialize2 instead of Initialize. Initialize2 expects a ReadableWorkbook as the last parameter. It creates a copy of the given ReadableWorkbook.

V0.91:
- The default encoding was changed to Cp1252.
- ReadableWorkbook.Encoding and WritableWorkbook.Encoding fields were added. You can set these fields before initializing the object in order to change the encoding.

V1.00:
New initialize methods:
WritableCell.InitializeNumber - Creates a number cell.
WritableCell.InitializeFormula - Creates a formula cell.
 

Attachments

  • ExcelExample.zip
    12.8 KB · Views: 9,853
  • ExcelLibrary.zip
    13.1 KB · Views: 9,161
Last edited:

fabio55

Member
Licensed User
Longtime User
I need the excel table to be drawn a little bit down the page. This to allow an editing text box on top of the table itself. There is a Table class in the example but if a change the SV (the internal scroll view) top property I get an error..The best wold be to place the table inside a panel or a TabHost, but how to do that?
 

Firpas

Active Member
Licensed User
Longtime User
Is possible insert images in a cell with this lib??

Thanks for your answer
 

lorebarita

Member
Licensed User
Longtime User
I am trying to read an excel document with xlsx extention and I get the following error

java.lang.RuntimeException: jxl.read.biff.BiffException: Unable to recognize OLE stream

So my question is does the library support excel 2013 format
 

jefflynn1974

Member
Licensed User
Longtime User
I want to make some backup of my workbook but after I use newWorkbook.Write I can not save later the same workbook again. Is there any solution for this situation?
 

fabio55

Member
Licensed User
Longtime User
If this can help I save my workbook several times without problems, I think you should show your code..
 

jefflynn1974

Member
Licensed User
Longtime User
I attached my sample application.

It records the touches on a button to an .xls file. The data have been saved when you quit from the app and in every minute as a backup.
The problem is that after the automatic backup is executed the save at the exit is do nothing. You can check this when you click the button after a minute and you won't see it in the workbook.
 

Attachments

  • demo.zip
    4.6 KB · Views: 723

Devan

Member
Licensed User
Longtime User
Hi to all, I'm not a programmer, but im learning Android programming language since January'15. This B4A and the active community has been very helpful for my project. I manage to convert SQL db to CSV. But I want to convert my SQL db to excel. After going through Excel tutorials & Questions im unable to find any sample code or understand how to do it.

Below error is from ExcelExample code from Erel.
What the reason for this error
Parsing code. 0.08
Compiling code. Error
Error compiling program.
Error description: Cannot access private member: numberofcolumns
Occurred on line: 444
For col = 0 To Table1.NumberofColumns - 1
Word: numberofcolumns

Parsing code. 0.08
Compiling code. Error
Error compiling program.
Error description: Cannot access private member: header
Occurred on line: 430
For Each lbl As Label In Table1.Header
Word: header
 

Devan

Member
Licensed User
Longtime User
I've just downloaded the example from the first post and it compiled successfully. NumberOfColumns is a public variable. Have you modified the code?

Hi Erel, yes i modified than i change back to same as per your example. Anyway i will download again & let me check. Meanwhile can you provide sample code to convert Sql db into excel as per .csv.
What the difference between public variable & private member.
Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Status
Not open for further replies.
Top