Operations on Excel tables (read, update, and create)

Source: Internet
Author: User

In daily work, the operations on the Excel worksheet are countless. The following are the operations performed on the worksheet by the java language. copy the jar package to the lib directory of the project ):
Engineering architecture:


Create Excel: CreateExcel. java
[Java]
<Span style = "font-size: 16px;"> package test1;

Import java. io. File;
Import java. io. IOException;

Import jxl. Workbook;
Import jxl. write. Label;
Import jxl. write. WritableSheet;
Import jxl. write. WritableWorkbook;
Import jxl. write. WriteException;
Import jxl. write. biff. RowsExceededException;

Public class CreateExcel {

Public static void main (String args []) {

Try {
// Open the file
WritableWorkbook book = Workbook. createWorkbook (new File ("test2.xls "));
// Generate a worksheet named "first page". "0" indicates the first page.
WritableSheet sheet = book. createSheet ("first page", 0 );
// Construct the first column in the Label object, with the first row)
// And the cell content is "testtest"
Label label = new Label (0, 0, "testtest ");
// Add the value to the cell
Sheet. addCell (label );
// Generate a cell that saves numbers. The full package path of Number must be used; otherwise, the differences may occur.
// The cell position is the second column, the first row, and the value is 555.1234.
Jxl. write. Number number = new jxl. write. Number (1, 0,555.1234 );
// Add the value to the cell
Sheet. addCell (number );
// Write data and close the file
Book. write ();
Book. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (RowsExceededException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (WriteException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
} </Span>
Update Data: UpdateExcel
[Java]
<Span style = "font-size: 16px;"> package test1;

Import java. io. File;
Import java. io. IOException;

Import jxl. Workbook;
Import jxl. read. biff. BiffException;
Import jxl. write. Label;
Import jxl. write. WritableSheet;
Import jxl. write. WritableWorkbook;
Import jxl. write. WriteException;
Import jxl. write. biff. RowsExceededException;

/**
* The jExcelAPI can be used to modify an existing Excel file. When modifying an Excel file, except for opening the file in different ways, other operations are the same as creating an Excel file.
*/
Public class UpdateExcel {

Public static void main (String args []) {

Try {
// Open the file
Workbook wb = Workbook. getWorkbook (new File ("test1.xls "));
// Open a copy of the text and specify that the data is written back to the source file
WritableWorkbook book = Workbook. createWorkbook (new File ("test2.xls"), wb );
// Add a worksheet
WritableSheet sheet = book. createSheet ("Page 2", 1 );
Sheet. addCell (new Label (0, 0, "tested "));
Book. write ();
Book. close ();
} Catch (BiffException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (RowsExceededException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (WriteException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
} </Span>
Read data: ReadExcel. java
[Java]
<Span style = "font-size: 16px;"> package test1;

Import java. io. File;
Import java. io. IOException;

Import jxl. Cell;
Import jxl. Sheet;
Import jxl. Workbook;
Import jxl. read. biff. BiffException;

// Read the Excel file
Public class ReadExcel {

Public static void main (String args []) {

Try {
// Open the file
Workbook book = Workbook. getWorkbook (new File ("test1.xls "));
// Obtain the work object of the first table. "0" indicates the first table.
Sheet sheet = book. getSheet (0 );
Int rows = sheet. getRows ();
Int cols = sheet. getColumns ();
// Obtain the cell (0, 0) in the first row of the first column)
For (int I = 0; I <rows; I ++ ){
For (int j = 0; j <cols; j ++ ){
Cell cell = sheet. getCell (j, I );
String result = cell. getContents ();
System. out. print ("" + result + "");
}
System. out. println ();
}
Book. close ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (BiffException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
} </Span>

Author: wangbaoyin

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.