Java Web uses POI to Operate Excel and javawebpoiexcel

Source: Internet
Author: User

Java Web uses POI to Operate Excel and javawebpoiexcel
1. Add POI for the project

POI Official Website

Click it and download it (the above is the compiled class, and the following is the source code)

 

Decompress the folder, copy the following three files to the WebComtent> WEB-INF> lib folder

Copy the three files to the lib folder of Tomcat. Otherwise, Tomcat will report an error because the class cannot be found (this is a depressing place for one morning)

Use the import org. apache. poi. hssf. usermodel. *; Package content, for example, HSSFWorkbook.
Use the import org. apache. poi. xssf. usermodel. *; Package content, for example, XSSFWorkbook.
Read the content of the import org. apache. poi. ss. usermodel. * package in two formats, for example: Workbook

Since I read the export X file, I used the above jar files.

Note:

In the two folders also have the jar file we need, specifically which few forgot (of course, for insurance can also put all into the WebContent> WEN-INF> lib and then BuildPath into the project ), it doesn't matter. An error will be reported during the running process. Find the relevant jar file BuildPath based on the error message,Make sure to place a copy under Tomcat> lib.

2. Read Excel files

Official Tutorial: Link

 

Class Library: Link

It's hard to understand the Code directly.

// Traverse an Excel file
Private void getExcelData (File file) {System. out. println ("now in getExcelData"); System. out. println ("get file name:" + file. getName (). toString (); XSSFWorkbook workBook = null; try {workBook = new XSSFWorkbook (file); int sheetCount = workBook. getNumberOfSheets (); // Number of Sheet systems. out. println ("num of sheet is:" + sheetCount); // traverse each sheetfor (int I = 0; I <sheetCount; I ++) {XSSFSheet sheet = workBook. getSheetAt (I); // get the total number of rows int rowCount = sheet. getPhysicalNumberOfRows (); System. out. println ("num of row:" + rowCount); System. out. println ("I now in sheet:" + I); // traverse each row for (int r = 0; r <rowCount; r ++) {XSSFRow row = sheet. getRow (r); // get the total number of columns int cellCount = row. getPhysicalNumberOfCells (); // traverse each column for (int c = 0; c <cellCount; c ++) {XSSFCell cell = row. getCell (c); String cellValue = null; switch (cell. getCellTypeEnum () {case STRING: // System. out. println ("celltype is string"); cellValue = cell. getStringCellValue (); break; case NUMERIC: // System. out. println ("celltype is Number"); // integer, decimal, date cellValue = String. valueOf (cell. getNumericCellValue (); break; case BOOLEAN: // System. out. println ("celltype is Boolean"); cellValue = String. valueOf (cell. getBooleanCellValue (); break; case FORMULA: // System. out. println ("celltype is Formula"); // Formula cellValue = "error, cannot be a Formula"; break; case BLANK: // System. out. println ("celltype is Blank"); // Blank cellValue = cell. getStringCellValue (); break; case ERROR: // System. out. println ("celltype is Error"); cellValue = "Error"; break; default: // System. out. println ("celltype: default"); cellValue = "error"; break;} System. out. println (cellValue. toString () ;}}} catch (IOException e) {System. out. println ("File Error IOException:" + e. getMessage ();} catch (Exception e) {// TODO: handle exception} finally {try {workBook. close ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace (); System. out. println ("workBook. close () & fileInputStream. close () Error: "+ e. getMessage ();} System. out. println ("Try Catch: finally");} System. out. println ("hi feipeng8848 getExcelData is done ");}

  

 

Related Article

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.