A good tool for dealing with databases in EXCEL-JXL

Source: Internet
Author: User
If you often need to deal with EXCEL and databases, we recommend that you use a good open-source tool JXL. The following section describes how to import the EXCEL data to the database,
And how to export the database data to EXCEL.

1) Import EXCEL tables to the database
Suppose there is an EXCEL worksheet with two sheets. Remember that the first SHEET number is 0 and the 2nd sheet number is 1.
Import jxl. Cell;
Import jxl. Sheet;
Import jxl. Workbook;
Import jxl. read. biff. BiffException;

The main methods are as follows:

InputStream fs = null;
Workbook rbw = null;
Try {

Try {
Fs = new FileInputStream (file );
Rbw = Workbook. getWorkbook (fs );
System. out. println ("Local excel file loaded successfully! ");
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (BiffException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}


Sheet book = null;
Sheet bookSingle = null;
Try {
Book = rbw. getSheet (0); // the first sheet
} Catch (Exception e ){
E. printStackTrace ();
}
System. out. println ("the excel file sheet is obtained successfully! ");

RsRows = book. getRows (); // obtain the total number of rows in the first sheet.
For (int I = 1; I <rsRows; I ++ ){
// Obtain data in an excel file
// Column 0th of row I
Cell cell0 = book. getCell (0, I );
String usercode = cell0.getContents ();

// Column 1st of row I
Cell cell1 = book. getCell (1, I );
String org_name = cell1.getContents ();
}
Note that book. getCell (0, I) is written from column 0th of row I, which is the opposite.

2) database to EXCEL
File file = new File ("xxx.xls ");
WritableWorkbook book = Workbook. createWorkbook (new File (fileName ));

// Generate a worksheet named "SheetOne". The parameter 0 indicates that this is the first sheet.
WritableSheet sheet = book. createSheet ("SheetOne", 0 );

// In the constructor of the Label object, the cell position is the first row (0, 0) in the first column and the cell content.
Label chs_name_LbTitle = new Label (0, 0, "Chinese name ");

........................................ ....
Sheet. addCell (chs_name_LbTitle );

Label ent_name_cCell = new Label (0, 1, actual data to be written );
Sheet. addCell (ent_name_cCell );
Book. write ();
Book. close ();

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.