Android adds data to a local Excel table

Source: Internet
Author: User

Android adds data to a local Excel table

Due to project requirements, I learned how to add data in the program to a local Excel table today.
The following are the study notes:
First:

First, import the jxl. jar package to the libs folder.
Create an Excel table and add a header to the table.

// Create an excel table. public void createExcel (File file) {WritableSheet ws = null; try {if (! File. exists () {// create table wwb = Workbook. createWorkbook (file); // create a form. sheet indicates the table name, 0 indicates the first table, and ws = wwb. createSheet (sheet1, 0); // insert data Label lbl1 = new Label (0, 0, name) into the specified cell; // The first parameter indicates that 0 indicates the first column, the second parameter indicates the row, the same 0 indicates the first row, and the third parameter indicates the data to be added to the cell. label bll2 = new Label (1, 0, gender); // Add it to the specified table. ws. addCell (lbl1); ws. addCell (bll2); // write wwb to the file from the memory. write (); wwb. close () ;}} catch (Exception e) {e. printStackTrace ();}}

When you want to add data to the table, you should:

Public void writeToExcel (String name, String gender) {try {// each time you insert data, You must retrieve the original table and create a new table, add the contents of the original table to the new table. however, if the two paths are the same, the effect is equivalent to adding them to the original table. workbook oldWwb = Workbook. getWorkbook (excelFile); wwb = Workbook. createWorkbook (excelFile, oldWwb); // obtain the table WritableSheet ws = wwb for the specified index. getSheet (0); // get the existing number of rows in the table int row = ws. getRows (); Label lbl1 = new Label (0, row, name); Label bll2 = new Label (1, row, gender); ws. addCell (lbl1); ws. addCell (bll2); // writes data to the file from the memory and can only be refreshed once. wwb. write (); wwb. close ();} catch (Exception e) {e. printStackTrace ();}}

The key code above allows you to add data to a local Excel table.
When writing data to an Excel table, the original file is opened each time and the data is read. Then, a new table is created to read the data to the new table. Even if a file path is connected and the file name is the same, the effect is the same as that directly added to the old table. However, considering the efficiency, I always feel that this method of adding data is too cumbersome, so I hope you can advise me here.
This article Demo Android add data to a local Excel table Demo

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.