iMOOC Learning Notes: Java Excel Reading and Writing Initial Practice - Using POI to Parse Excel Files (II)

Keywords: Excel Apache Java Steam

Mr. David's Decrypt JAVA to Import and Export Excel

Relevant jar packages:
poi-3.11-20141221.jar
commons-io-2.2.jar

Three Common Techniques of Reading and Writing Excel
1. POI
2. JXL
3. FASTEXCEL

Apache POI is an open source library of Apache Software Foundation. POI provides API functions for JAVA programs to read and write files in Microsoft Office format.
HSSF is the abbreviation of Horrible SpreadSheet Format, or "hateful spreadsheet format". Through HSSF, you can read, write and modify Excel files with pure JAVA code.

HSSF - The ability to read files in Microsoft Excel format.
XSSF - The ability to read files in Microsoft Excel OOOXML format.
HWPF - The ability to read and write files in Microsoft Word format.
HSLF - The ability to read and write files in Microsoft PowerPoint format.
HDGF - The ability to read and write files in Microsoft Visio format.

IText: Through iText, not only PDF or rtf documents can be generated, but also XML and Html files can be converted into PDF files. After downloading iText.jar files, only the path of iText.jar can be added to the CLASSPATH of the system, and iText class library can be used in the program.

Parsing Excel Files with POI

package com.imooc.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class PoiReadExcel {
    /**
     * Parsing Excel File Content to Console with POI
     * @author David
     * @param args
     */
    public static void main(String[] args) {
        //Introduce Excel files that need to be parsed, which files are in which path
        File file = new File("e:/poi_test.xls");
        try {
            //Open the file's input stream FileUtils.openInputStream(file)
            FileInputStream steam = FileUtils.openInputStream(file);
            //Create Excel workbook to read the contents of files in the input stream
            HSSFWorkbook workbook = new HSSFWorkbook(steam);
            //The first way is to open Sheet0 worksheet of workbook: workbook.getSheet("Sheet0");
            //HSSFSheet sheet = workbook.getSheet("Sheet0");
            //Second way: read the default first worksheet: workbook.getSheetAt(0)
            HSSFSheet sheet = workbook.getSheetAt(0);
            //Read the data in the worksheet
            int firstRowNum = 0;
            int lastRowNum = sheet.getLastRowNum();//Get the last line number in the sheet
            //Loop read data for each row
            for (int i = firstRowNum; i <=lastRowNum; i++) {
                HSSFRow row = sheet.getRow(i);//Create row of the current row object
                int lastCellNum = row.getLastCellNum();//Gets the last cell column number of the current row
                //Loop through each column of the row
                for (int j = 0; j < lastCellNum; j++) {
                    HSSFCell cell = row.getCell(j);//Create cells for the column in which the current row is located
                    String value = cell.getStringCellValue();//Set the content of the cell
                    System.out.print(value + "  ");//Gaps in data between peer cells
                }
                System.out.println();//Line-feeding without reading a line of data
            }
            steam.close();//Close the input stream
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Posted by Traveller29 on Thu, 14 Feb 2019 18:57:19 -0800