Java uses jxl for simple reading and writing of excel files

Source: Internet
Author: User

Java uses jxl for simple reading and writing of excel files

Jxl Jar package: Baidu online storage


Import java. io. file; import java. io. IOException; import jxl. sheet; import jxl. workbook; import jxl. read. biff. biffException; import jxl. write. label; import jxl. write. writableCellFeatures; import jxl. write. writableSheet; import jxl. write. writableWorkbook; import jxl. write. writeException; import jxl. write. biff. rowsExceededException;/*** December 30, 2014 7:16:14 * @ author sunfeilong1993 * Introduction: Using jxl to easily read from an excel file Write the data and the data into the excel file **/public class ExcelInputAndOutput {public static void main (String [] args) {writeToFile (); readFromFile ();} // readFromFilepublic static void readFromFile () {File file = new File ("d:/test.xls"); StringBuffer sb = new StringBuffer (); try {Workbook book = Workbook. getWorkbook (file); try {Sheet sheet = book. getSheet (0); for (int I = 0; I <10; I ++) {for (int j = 0; j <10; j ++) {// The first parameter represents Column. The second parameter indicates the row. (The default start value is 0) sb. append (sheet. getCell (j, I ). getContents () + "\ t");} sb. append ("\ n");} System. out. println (sb);} finally {if (book! = Null) {book. close () ;}} catch (BiffException e) {System. err. println (e + "");} catch (IOException e) {System. err. println (e + "File read error") ;}// end readFromFile // witeToFilepublic static void writeToFile () {file File = new File ("d:/test.xls "); try {WritableWorkbook book = Workbook. createWorkbook (file); // create a workspace. (The default excel file contains three sheets. in the lower left corner of the excel file, you can see sheet1/sheet2/sheet3) WritableSheet sheet = book. createSheet ("first page", 0); // Add the content try {for (int I = 0; I <10; I ++) to the Workspace) {for (int j = 0; j <10; j ++) {Label newLabel; if (0 = I) {// The first parameter represents the column, the second parameter indicates the row (the default start value is 0), and the third parameter is newLabel = new Label (j, I, String. valueOf (j);} else if (0 = j) {newLabel = new Label (j, I, String. valueOf (I);} else {newLabel = new Label (j, I, String. v AlueOf (I * j);} // Add the comment WritableCellFeatures cellFeatures = new WritableCellFeatures (); cellFeatures to the cell. setComment ("here is the value of" + I + "*" + j + ""); newLabel. setCellFeatures (cellFeatures); sheet. addCell (newLabel) ;}} catch (RowsExceededException e) {System. err. println (e + "Incorrect row or column parameters! ");} Catch (WriteException e) {System. err. println (e +" Write failed ");} finally {if (book! = Null) {book. write (); try {book. close () ;}catch (WriteException e) {System. err. println (e + "failed to close the file! ") ;}}} Catch (IOException e) {System. err. println (e +" failed to create the file! ");}}}


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.