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,813
  • ExcelLibrary.zip
    13.1 KB · Views: 9,113
Last edited:

logemag

Member
Licensed User
Longtime User
Hello Erel, when i compile the ExelExample, there is an error :

Parsing code. 0.06
Compiling code. 0.18
Compiling layouts code. 0.01
Generating R file. 0.14
Compiling generated Java code. Error
B4A line: 69
cellFormat.Initialize2(cellFormat.FONT_ARIAL, 12, True, False, False,cellFormat.COLOR_GREEN)
javac 1.7.0_09
src\b4a\example\main.java:471: error: cannot access WritableFont
_cellformat.Initialize2(_cellformat.FONT_ARIAL,(int)(12),anywheresoftware.b4a.keywords.Common.True,anywheresoftware.b4a.keywords.Common.False,anywheresoftware.b4a.keywords.Common.False,_cellformat.COLOR_GREEN);
^
class file for jxl.write.WritableFont not found
1 error
 

Mahares

Expert
Licensed User
Longtime User
I do not want to play Erel, but did you do steps 2 and 3 in his setup. It is not sufficent that you copy the 2 lib files in step 1.
Setup
1.Download the attached library and copy both files to the libraries folder.
2. Download jexcel library: A Java library for reading/writing Excel - Browse /jexcelapi/2.6.12 at SourceForge.net (jexcelapi_2_6_12.zip).
3.Open the zip file and copy jxl.jar to the libraries folder.
 

logemag

Member
Licensed User
Longtime User
oops, with the jxl.jar it's works fine... Sorry... too fast !
 

fdx12345

Active Member
Licensed User
Longtime User
Sheet selector

Can one add a sheet selector at the bottom the the spreadsheet thus allowing multiple spreadsheets - ie like a standard Excel spreadsheet?
 

fdx12345

Active Member
Licensed User
Longtime User
thank you

I thought it may be a Table Class application with an Excel read/write front end. Other than reading/writing Excel, nothing to gain over a straight Table.

Now if someone could come up with a spreadsheet control!!
 

rboeck

Well-Known Member
Licensed User
Longtime User
Foreign language characters

Hi,

today i tried the first time to use this library. But when i got the first german umlaut, i stopped my work. I see each value, but i cannot use the textreader and writer to make the translation from windows codepage 1252 to unicode UTF8 code. Did someone has an fast and easy way to translate a string or a map?
Greetings from austria
Reinhard
 

rboeck

Well-Known Member
Licensed User
Longtime User
German Umlaute in Excel file

Hi,

i have only added one last line in the book.xls file. The name of the book in an german excel would look like: Ein Buch mit ß, öäü sowie ÖÄÜ
On Android i get only question marks on a background.
Thanks for your time!
Reinhard
 

Attachments

  • ExcelDemo mit german Umlaute.zip
    15.4 KB · Views: 2,657

masterleous

Member
Licensed User
Longtime User
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.

By following above method what will happen, let say i want to edit / modify file1.xls (which have only one worksheet "Sheet1"), will it copy Sheet1 to another worksheet ? or it copy whole file1.xls to another file for editing?

thanks
 
Last edited:

Creaky

Member
Licensed User
Longtime User
Dear Erel,

Would it be possible to have the "WritableCellFormat.setWrap(true)" option added to this library? That way it should be possible to use newlines in cells and have the content in cells automatically wrap over multiple lines.
 

palmaf

Member
Licensed User
Longtime User
xls

Hi, having an existing file xls, wishing to modify only some cells is possible ? if yes, How? Thanks

Francesco:sign0085:
 

Paolo SD

Member
Licensed User
Longtime User
Hi, how I can write number, date or formula using Reflector library. I have try to write number, but in excel i see single quote in front of number and the cell is in string format.
Thanks to all.
 
Status
Not open for further replies.
Top