Java使用POI解析Excel文件的工具类【通用】

       这个工具类针对 .xls 和 .xlsx 的文件都可以进行解析,并且返回List> 类型,外面的List是excel中有多少行,里面的是每一行的每个单元格的数据,都转为了String类型,以“,”号分隔。进行遍历循环时,拿到每个String直接以“,”分割就可以获取到里面的值。

     1. 先给出pom 依赖:

        


    org.apache.poi
    poi
    3.10-FINAL


    org.apache.poi
    poi-ooxml
    3.10-FINAL

  2. 下面就是工具类的代码:2个方法,分别对2003和2007的excel进行解析:

    (需要上面加上包名呦

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelParse {


    public static List upLoadExcel(InputStream in) throws Exception {
        HSSFWorkbook wookbook = new HSSFWorkbook(in);
        HSSFSheet sheet = wookbook.getSheetAt(0);
        HSSFRow row = sheet.getRow(0);
        int cells = row.getPhysicalNumberOfCells();
        int rows = sheet.getPhysicalNumberOfRows();
        List list = new ArrayList();

        for(int i = 1; i < rows; ++i) {
            HSSFRow row1 = sheet.getRow(i);
            if (row1 != null) {
                String value = "";

                for(int j = 0; j < cells; ++j) {
                    HSSFCell cell = row1.getCell(j);
                    if (cell != null) {
                        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                        switch(cell.getCellType()) {
                            case 0:
                                value = value + cell.getNumericCellValue() + ",";
                                break;
                            case 1:
                                value = value + cell.getStringCellValue() + ",";
                            case 2:
                                break;
                            default:
                                value = value + " ,";
                        }
                    } else {
                        value = value + " ,";
                    }
                }

                list.add(value);
            }
        }

        return list;
    }

   


    public static List upload2007Excel(InputStream in) throws Exception {
        XSSFWorkbook wookbook = new XSSFWorkbook(in);
        XSSFSheet sheet = wookbook.getSheetAt(0);
        XSSFRow row = sheet.getRow(0);
        int cells = row.getPhysicalNumberOfCells();
        int rows = sheet.getPhysicalNumberOfRows();
        List list = new ArrayList();

        for(int i = 1; i < rows; ++i) {
            XSSFRow row1 = sheet.getRow(i);
            if (row1 != null) {
                String value = "";

                for(int j = 0; j < cells; ++j) {
                    XSSFCell cell = row1.getCell(j);
                    if (cell != null) {
                        cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                        switch(cell.getCellType()) {
                            case 0:
                                value = value + cell.getNumericCellValue() + " ,";
                                break;
                            case 1:
                                value = value + cell.getStringCellValue() + " ,";
                            case 2:
                                break;
                            default:
                                value = value + ",";
                        }
                    } else {
                        value = value + " ,";
                    }
                }

                list.add(value);
            }
        }

        return list;
    }
}

 

你可能感兴趣的:(工具类)