Read and Write EXCEL files in JAVA

Source: Internet
Author: User

Use the jxl. jar package and download it from the Internet. Hope to help you.


Next, paste the Code directly:

Public List getValue (String fileName) {String str = ExcelOparations. readExcel (fileName ). trim (); String [] str4n = str. split ("\ n"); List list1 = new ArrayList (); List list2 = null; for (int I = 0; I <= str4n. length-1; I ++) {String [] str4t = str4n [I]. trim (). split ("\ t"); list2 = new ArrayList (); for (int j = 0; j <= str4t. length-1; j ++) {if (str4t [j]! = Null &&! Str4t [j]. equals ("") {System. out. println ("------------" + str4t [j]); list2.add (str4t [j]) ;}} list1.add (list2);} return list1 ;} /*** read all content from the excel file ** @ param file * excel file * @ return content of the excel file */public static String readExcel (String fileName) {StringBuffer sb = new StringBuffer (); Workbook wb = null; try {// construct the Workbook (working thin) object wb = Workbook. getWorkbook (new File (fileName);} catch (BiffExcep Tion e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} if (wb = null) return null; // after obtaining the Workbook object, you can get the Sheet (worksheet) through it) sheet [] sheet = wb. getSheets (); if (sheet! = Null & sheet. length> 0) {// cycle each worksheet for (int I = 0; I <sheet. length; I ++) {// get the number of rows in the current worksheet int rowNum = sheet [I]. getRows (); for (int j = 0; j <rowNum; j ++) {// obtain all cells in the current row. Cell [] cells = sheet [I]. getRow (j); if (cells! = Null & cells. length> 0) {// cycle each cell for (int k = 0; k <cells. length; k ++) {// read the current cell value String cellValue = cells [k]. getContents (); sb. append (cellValue + "\ t") ;}} sb. append ("\ r \ n");} sb. append ("\ r \ n") ;}// closes the resource and releases the memory wb. close (); return sb. toString ();} /*** import content into the excel file ** @ param fileName * Name of the file to be imported */public static void writeExcel (String fileName) {WritableWorkbook wwb = null; Try {// first use the Workbook class factory method to create a writeable Workbook object wwb = Workbook. createWorkbook (new File (fileName);} catch (IOException e) {e. printStackTrace ();} if (wwb! = Null) {// create a writable worksheet // The createSheet method of the Workbook has two parameters. The first one is the worksheet name, the second is the position of the worksheet in the workbook WritableSheet ws = wwb. createSheet ("sheet1", 0); // Add the cell for (int I = 0; I <10; I ++) {for (int j = 0; j <5; j ++) {// note that in Excel, the first parameter table represents the column, and the second indicates the row Label labelC = new Label (j, I, "This is the" + (I + 1) + "Row, the" + (j + 1) + "column "); try {// Add the generated cells to the worksheet ws. addCell (labelC);} catch (RowsExceededException e) {e. printStackTrace ();} catch (WriteException e) {e. printStackTrace () ;}}try {// write wwb to the file from the memory. write (); // close the resource and release the memory wwb. close ();} catch (IOException e) {e. printStackTrace ();} catch (WriteException e) {e. printStackTrace ();}}}


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.