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:

Devan

Member
Licensed User
Longtime User
Hi Erel,
Thank you for the advice. I will try to figure out how to do it.
Have a nice day.
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
I just wanted to test this library so I created this sub and called it from a menu item.

B4X:
Sub create_newworkbook_click

Dim newWorkbook As WritableWorkbook
    newWorkbook.Initialize(File.DirRootExternal, "Inventory.xls")
    Dim sheet1 As WritableSheet
sheet1 = newWorkbook.AddSheet("Inventory", 0)

' write to cells
    Dim cell As WritableCell
    cell.InitializeText(1, 0, "Hello")
    sheet1.AddCell(cell)
  
    cell.InitializeText(2, 0, "Goodbye")
    sheet1.AddCell(cell)

newWorkbook.write

End Sub

It created the file in dirrootexternal ok, but with 0 bytes and the file format wasn't recognized. I wasn't able to open it up.

I've got all 3 library files in the library folder.

I was going write some cells into an excel file and then attach it to an email.
 
Last edited:

Cebuvi

Active Member
Licensed User
Longtime User
Hi,

I'm testing the example of Excel library and I get this error

B4A version: 5.00
Parsing code. (0.01s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Generating R file. (0.11s)
Compiling generated Java code. (0.87s)
Convert byte code - optimized dex. (0.60s)
Packaging files. (0.26s)
Copying libraries resources (0.02s)
Found 7 resource files.
Signing package file (private key) (0.52s)
ZipAlign file. (0.07s)
Installing file to device. Error
pkg: /data/local/tmp/ExcelExample.apk
Failure [INSTALL_FAILED_VERSION_DOWNGRADE]

Where is the problem?

Thanks

PROBLEM SOLVED. there was another app with the same name.
 
Last edited:

ilan

Expert
Licensed User
Longtime User
is it possible to add to a specific cell a link to a different sheet??

like i have 3 sheets in my excel file and i want to click on cell 0,0 in sheet1 to open sheet2

is it possible to create such a excel file with this lib?

thanx
 

ilan

Expert
Licensed User
Longtime User
You can create a template Excel workbook with this link and then read the template, add your data and save it as a new workbook.

Thanx erel, this is a good idea.

So from your answer i also understand that i cannot create such a cell (that will link to a specific sheet) unless i have create by myself the excel file and just change the content i want, right?
 

merlin2049er

Well-Known Member
Licensed User
Longtime User
That doesn't seem to hard, just split your strings up and then write them each into to an separate excel cell.
 
Last edited:

wonder

Expert
Licensed User
Longtime User
I have a small app that does the following:
- Import Excel data into SQLite DB
- Process the data through MySQL
- Export back into an .xls file

The problem is that I have some hyperlinks in the original .xls file which are lost in the process.
Is it possible to retrieve the hyperlinks from the original and add them back in the output file?
 

Pilar-JLSineriz

Active Member
Licensed User
Longtime User
Really, I want to export SQL results that I've realiced to sqlite file and to export to xls. It's possible to see your app??
 

wonder

Expert
Licensed User
Longtime User
Really, I want to export SQL results that I've realiced to sqlite file and to export to xls. It's possible to see your app??
Sure! :)
Only I don't have access to the computer where it's stored right now.
I'll send you the code by Monday.
 
Last edited:
Status
Not open for further replies.
Top