资源简介

本文件内含有支持所有Excel文件解析导入的方法

资源截图

代码片段和文件信息

package com.travelsky.szky.pmp.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import org.apache.poi.POIxmlDocument;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
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;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;

public class ImportExcelUtil {
    static SimpleDateFormat sFormat = new SimpleDateFormat(“yyyy-MM-dd“);

    /** 
     * 获取当前载入的是那种格式 poifsfilesystem 为 excel 2007前的格式, poixmldocument为excel
     * 2007后的格式(包含excel2007)
     * 
     * @param inp
     *            输入流
     * @return
     * @throws IOException
     * @throws InvalidFormatException
     */
    public static Workbook createCommonWb(InputStream inp) throws IOExceptionException{

        if (inp.markSupported()) {
            inp = new PushbackInputStream(inp 8);
        }

        if (POIFSFileSystem.hasPOIFSHeader(inp)) {
            return new HSSFWorkbook(inp);
        } else if (POIxmlDocument.hasOOxmlHeader(inp)) {
            return new XSSFWorkbook(OPCPackage.open(inp));
        } else {
            return null;
        }

    }

    /**
     * 通过输入流返回 数据
     * 
     * @param inp
     * @return
     * @throws InvalidFormatException
     * @throws IOException
     */
    public static object[][] getBackWbData(InputStream inp)
            throws Exception IOException {

        Workbook wb = createCommonWb(inp);
        return getBackWbData(wb);

    }

    /**
     * 通过sheetName获取数据
     * 
     * @param wb
     * @param column
     * @param sheetName
     * @return
     */

    public static object[][] getBackWbData(Workbook wb int column
            String sheetName) {

        Sheet sheet = wb.getSheet(sheetName);
        return getData(column sheet);
    }

    /**
     * 通过sheetNum获取数据,第几个sheet
     * 
     * @param wb
     * @param column
     * @param sheetNum
     * @return
     */
    public static object[][] getBackWbData(Workbook wb int column int sheetNum) {

        Sheet sheet = wb.getSheetAt(sheetNum);
        return getData(column sheet);
    }

    /**
     * 默认第一个s

评论

共有 条评论