Jxl-Java: Reading and modifying Excel files

Source: Internet
Author: User

Read:

Public static void main (string [] ARGs) throws exception {
String filepath = "d :\\ test.xls ";
Workbook book = Workbook. getworkbook (new file (filepath ));
// Obtain the first worksheet object
Sheet sheet = book. getsheet (0 );
// Obtain the cell in the second row of the first column
Cell cell1 = sheet. getcell (0, 1 );
String result = cell1.getcontents ();
System. Out. println (result );
Book. Close ();
}

 

Operation modification:

Public class jxlmodify {
Public static void main (string [] ARGs) throws exception
{
// Obtain the Excel file
String filepath = "d :\\ test.xls ";
Workbook WB = Workbook. getworkbook (new file (filepath ));

// Open a copy of a file and write the data back to the source file
Note that the second parameter here is the Excel parameter obtained above.  
Writableworkbook book = Workbook. createworkbook (new file (filepath), WB );

// Obtain the first table
Writablesheet oldsheet = book. getsheet (0 );
Label L = new label (0, 1, "Modify Test ID ");
Oldsheet. addcell (L );

  
// Add a new worksheet
Writablesheet sheet = book. createsheet ("second table", 1 );
Label L = new label (0, 0, "test data on the second page ");
Sheet. addcell (L );

// Close
Book. Write ();
Book. Close ();

}
}

 

Operate images (note the image format:

Warning: Image Type JPEG not supported. Supported types are PNG

)

Public static void main (string [] ARGs) throws exception {
String filepath = "d :\\ test.xls ";
Workbook WB = Workbook. getworkbook (new file (filepath ));
Writableworkbook WWB = Workbook. createworkbook (new file (filepath), WB );
Writablesheet sheet = WWB. getsheet (2 );

File imagefile = new file ("D: \ jxl.png ");
//Five parameters (row column) span (row column) file objects
Writableimage image = new writableimage (1, 4, 2, 2, imagefile );

// Write Method
Sheet. addimage (image );
WWB. Write ();

WWB. Close ();
}

 

 

Cell operations:

Writablesheet. setrowview (int I, int height); // specify the height of row I + 1

Writablesheet. setcolumnview (int I, int width); // specify the width of column I + 1

 

 

 

 

 

 

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.