Java reads and writes excel files and java reads and writes excel files

Source: Internet
Author: User

Java reads and writes excel files and java reads and writes excel files

Requirement: use Java to read and write excel files

Use jexcelapi to read and write excel files in Java

 

First download and install jexcelapi

JExcelApi v2.6.12 (1911 kbytes)

Decompress the package and add the jxl. jar file to the Java Build Path.

 

Prepare the test excel file (D: \ test \ employee .xls)

 

Java reads excel files

Package com. test. IF. service; import java. io. file; import java. io. IOException; import jxl. cell; import jxl. sheet; import jxl. workbook; import jxl. read. biff. biffException; public class HandleExcelFile {private String excelFilePath = "D: \ test \ employee .xls"; private String [] [] nReadArray; private int nRows, nColumns = 0; Workbook xWorkbook; // convert the content in Excel to a two-dimensional array and return public String [] [] readExcel () {try {xWorkbook = Workbook. getWorkbook (new File (excelFilePath); Sheet sheet = xWorkbook. getSheet (0); nRows = sheet. getRows (); nColumns = sheet. getColumns (); nReadArray = new String [nRows] [nColumns]; for (int I = 1; I <nRows; I ++) {for (int j = 0; j <nColumns; j ++) {Cell cell = sheet. getCell (j, I); // (column, row) String content = cell. getContents (); nReadArray [I] [j] = content ;}} catch (BiffException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} xWorkbook. close (); return nReadArray;} public static void main (String [] args) {HandleExcelFile he = new HandleExcelFile (); String [] [] xLine = he. readExcel (); for (int I = 1; I <xLine. length; I ++) {for (int j = 0; j <xLine [I]. length; j ++) {System. out. print (xLine [I] [j] + "|");} System. out. println ();}}}

Test the read program. The result is

 

Write excel files in Java

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.