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 ();
}
}
}