Excel operations in Java

Source: Internet
Author: User

Create a file temp and create an outpath.xls file under the temp directory

Write operation

 

Package com;
Import java. Io. file;
Import java. Io. ioexception;
Import java. Text. dateformat;

Import jxl. workbook;
Import jxl. format. underlinestyle;
Import jxl. Write. datetime;
Import jxl. Write. label;
Import jxl. Write. numberformat;
Import jxl. Write. writablecell;
Import jxl. Write. writablecellformat;
Import jxl. Write. writablefont;
Import jxl. Write. writablesheet;
Import jxl. Write. writableworkbook;
Import jxl. Write. writeexception;
Import jxl. Write. Biff. rowsexceededexception;

Public class demo {
// Define a static variable
Private Static string Path = "D:/temp /";
// Check whether the target file exists
Static void checkexist (string path ){
Java. Io. File file = new java. Io. File (PATH );
If (file. isdirectory ()){
System. Out. println ("the Dir is exits ");
File. Delete ();
} Else {
File. mkdirs ();
System. Out. println ("made a dir! ");
}
}

Public static void main (string ARGs []) throws ioexception, rowsexceededexception, writeexception {
Demo. checkexist (PATH );
File tempfile = new file (path + "outpath.xls ");
Writableworkbook workbook = Workbook. createworkbook (tempfile );
Writablesheet sheet = Workbook. createsheet ("testcreateexcel", 0 );
// Some temporary variables for writing to excel
Label L = NULL;
Jxl. Write. number n = NULL;
Jxl. Write. datetime d = NULL;

// Predefined fonts and formats. It is best not to have too many formats in the same Excel file.
Writablefont headerfont = new writablefont (writablefont. Arial, 12, writablefont. Bold, false, underlinestyle. no_underline, jxl. format. colour. Blue );
Writablecellformat headerformat = new writablecellformat (headerfont );

Writablefont titlefont = new writablefont (writablefont. Arial, 10, writablefont. no_bold, false, underlinestyle. no_underline, jxl. format. colour. Red );
Writablecellformat titleformat = new writablecellformat (titlefont );

Writablefont detfont = new writablefont (writablefont. Arial, 10, writablefont. no_bold, false, underlinestyle. no_underline, jxl. format. colour. Black );
Writablecellformat detformat = new writablecellformat (detfont );

Numberformat NF = new numberformat ("0.00000"); // used for the number format
Writablecellformat priceformat = new writablecellformat (detfont, NF );

Label label1 = new label (1, 1, "Excel file for test", titleformat );
Label label2 = new label (1, 2, "Excel file for test", headerformat );
Label label3 = new label (1, 3, "Excel file for test", detformat );
Label label4 = new label (1, 4, "0.12450", priceformat );
Sheet. addcell (label1 );
Sheet. addcell (label2 );
Sheet. addcell (label3 );
Sheet. addcell (label4 );
Workbook. Write ();
Workbook. Close ();
}
}

Read operation

Formfile = (formfile) DYNAFORM. Get ("objexcel ");
Inputstream excelstream = formfile. getinputstream ();
Workbook workbook = Workbook. getworkbook (excelstream );
Sheet sheet = Workbook. getsheet (0 );
Int rows = sheet. getrows ();
For (INT I = 1; I <rows; I ++ ){
If (rows <2 ){
This. writejsmessage (response,
"Alert ('Enter the information'); window. Parent. returnvalue = 'OK'; window. Parent. Close ();");
}
String xsbh = sheet. getcell (0, I). getcontents (). Trim ();

}

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.