Use Java Excel APIs to update and read Excel tables

Source: Internet
Author: User

Package myexceltest;

Import jxl .*;
Import java. Io .*;
Import jxl. Write .*;
/**
* <P> Read the Excel table in Java, copy and update the Excel worksheet </P>
* <P> Description: Java developers can read the content of an Excel file and update the Excel worksheet.
* You can also useProgramGenerate a new Excel table, but I think there is almost no such requirement in our work.
* It will not be listed. If any colleague needs it, I will write the program again.
* </P>
* <P> copyright: Copyright (c) kongzhong corparation 2005 </P>
* <P> the program development environment is jb9 </P>
* @ Author Zhang Lipeng
* @ Version 1.0
*/

Public class test1 {
Public static void main (string [] ARGs ){
Jxl. Workbook RWB = NULL;
Try {
// Construct a workbook object, read-only workbook object
// Create a workbook directly from a local file
// Create a workbook from the input stream
Inputstream is = new fileinputstream ("D:/jb9test/myexceltest/book1.xls ");
RWB = Workbook. getworkbook (is );

// Sheet (term: Worksheet) is sheet1, sheet2, and sheet3 in the lower left corner of the Excel table, but in the program
// The subscript of sheet starts from 0.
// Obtain the first sheet
Sheet rs = RWB. getsheet (0 );
// Obtain the total number of columns in the sheet table
Int rscolumns = Rs. getcolumns ();
// Obtain the total number of rows in the sheet table
Int rsrows = Rs. getrows ();
// Obtain the object reference of the specified Cell
For (INT I = 0; I <rsrows; I ++ ){
For (Int J = 0; j <rscolumns; j ++ ){
Cell cell = Rs. getcell (J, I );
System. Out. Print (cell. getcontents () + "");
}
System. Out. println ();
}
// Create a new writable Excel worksheet using the created Excel worksheet
Jxl. Write. writableworkbook WWB = Workbook. createworkbook (new file ("D:/jb9test/myexceltest/book2.xls"), RWB );
// Read the first Worksheet
Jxl. Write. writablesheet Ws = WWB. getsheet (0 );

// Obtain the first cell object
Jxl. Write. writablecell WC = ws. getwritablecell (0, 0 );
// Determine the cell type and convert it accordingly
If (WC. GetType () = celltype. Label ){
Label L = (Label) WC;
L. setstring ("the value has been modified .");
}
// Write an Excel Object
WWB. Write ();
WWB. Close ();

} Catch (exception e ){
E. printstacktrace ();
}
Finally {
// When the operation is complete, close the object and release the occupied memory space
RWB. 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.