1. 程式人生 > >Excel匯出+解析通用工具類

Excel匯出+解析通用工具類

參照原文:http://blog.csdn.net/houxuehan/article/details/50960259

maven配置:

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.10-FINAL</version>
    </dependency>
    <dependency>
<groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId>
<version>1.2.44</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>
3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>

測試model: student

import java.util.Date;
/**
 *<p>
*</p>
*
 * @author lishw
 * @version $Id: Student.java, v 0.1 2017-12-28 18:09 lishw $$
 */
public class Student {
    private String name;
    private int age;
    private Date birthday;
    private float height;
    private double weight;
    private boolean sex;
    public String getName() {
        return name;
}

    public void setName(String name) {
        this.name = name;
}

    public Integer getAge() {
        return age;
}

    public Date getBirthday() {
        return birthday;
}

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
}

    public float getHeight() {
        return height;
}

    public void setHeight(float height) {
        this.height = height;
}

    public double getWeight() {
        return weight;
}

    public void setWeight(double weight) {
        this.weight = weight;
}

    public boolean isSex() {
        return sex;
}

    public void setSex(boolean sex) {
        this.sex = sex;
}

    public void setAge(Integer age) {
        this.age = age;
}

    /**
     * @see Object#toString()
     */
@Override public String toString() {
        StringBuilder builder = new StringBuilder();
builder.append("Student[name=");
builder.append(name);
builder.append(",age=");
builder.append(age);
builder.append(",birthday=");
builder.append(birthday);
builder.append(",height=");
builder.append(height);
builder.append(",weight=");
builder.append(weight);
builder.append(",sex=");
builder.append(sex);
builder.append(",super.toString()=");
builder.append(super.toString());
builder.append("]");
        return builder.toString();
}
}
工具類,及測試:
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 *<p>
*     excel解析匯出通用工具類
 *</p>
*
 * @author lishw
 * @version $Id: ExcelUtil.java, v 0.1 2017-12-28 18:02 lishw $$
 */
public class ExcelUtil {

    public static String DEFAULT_DATE_PATTERN  = "yyyy年MM月dd日";//預設日期格式
public static int    DEFAULT_COLOUMN_WIDTH = 17;
/**
     * 讀取excel,預設首行為標題行
     * @param filepath 檔名稱
     * @param startIndex 標題行數
     * @param headMap 標題名稱和欄位對應
     * @return 資料結果
     * @throws Exception
     */
public static JSONArray readExcelWithTitle(String filepath, Integer startIndex, Map<String, String> headMap)
            throws Exception {
        String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length());
InputStream is = null;
Workbook wb = null;
JSONArray array = new JSONArray();
        try {
            is = new FileInputStream(filepath);
            if (fileType.equals("xls")) {
                wb = new HSSFWorkbook(is);
} else if (fileType.equals("xlsx")) {
                wb = new XSSFWorkbook(is);
} else {
                throw new Exception("讀取的不是excel檔案");
}

            int sheetSize = wb.getNumberOfSheets();
            for (int i = 0; i < sheetSize; i++) {//遍歷sheet頁
Sheet sheet = wb.getSheetAt(i);
List<String> titles = new ArrayList<String>();//放置所有的標題
int rowSize = sheet.getLastRowNum() + 1;
                for (int j = 0; j < rowSize; j++) {//遍歷行
Row row = sheet.getRow(j);
                    if (row == null || j < ((startIndex==null ||startIndex<0)?1:startIndex)) {//略過空行
continue;
}
                    int cellSize = row.getLastCellNum();//行中有多少個單元格,也就是有多少列
if (j == startIndex) {//標題行
for (int k = 0; k < cellSize; k++) {
                            Cell cell = row.getCell(k);
titles.add(cell.toString());
}
                    } else {//其他行是資料行
JSONObject obj = new JSONObject();
                        for (int k = 0; k < titles.size(); k++) {
                            Cell cell = row.getCell(k);
String key = titles.get(k);
String cellValue = null;
                            if (cell != null) {
                                // 以下是判斷資料的型別
switch (cell.getCellType()) {
                                    case HSSFCell.CELL_TYPE_NUMERIC: // 數字
cellValue = cell.getNumericCellValue() + "";
                                        break;
                                    case HSSFCell.CELL_TYPE_STRING: // 字串
cellValue = cell.getStringCellValue();
                                        break;
                                    case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
cellValue = cell.getBooleanCellValue() + "";
                                        break;
                                    case HSSFCell.CELL_TYPE_FORMULA: // 公式
cellValue = cell.getCellFormula() + "";
                                        break;
                                    case HSSFCell.CELL_TYPE_BLANK: // 空值
cellValue = "";
                                        break;
                                    case HSSFCell.CELL_TYPE_ERROR: // 故障
cellValue = "非法字元";
                                        break;
                                    default:
                                        cellValue = "未知型別";
                                        break;
}
                            }
                            obj.put(headMap.get(key), cellValue);
array.add(obj);
}
                    }
                }
            }
            return array;
} catch (FileNotFoundException e) {
            throw e;
} finally {
            if (is != null) {
                is.close();
}
        }
    }
    /**
     * 匯出Excel 97(.xls)格式 ,少量資料
     * @param title 標題行
     * @param headMap 屬性-列名
     * @param jsonArray 資料集
     * @param datePattern 日期格式,null則用預設日期格式
     * @param colWidth 列寬 預設 至少17個位元組
     * @param out 輸出流
     */
public static void exportExcel(String title, Map<String, String> headMap, JSONArray jsonArray, String datePattern,
                                   int colWidth, OutputStream out) {
        if (datePattern == null)
            datePattern = DEFAULT_DATE_PATTERN;
// 宣告一個工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
workbook.createInformationProperties();
workbook.getDocumentSummaryInformation().setCompany("*****公司");
SummaryInformation si = workbook.getSummaryInformation();
si.setAuthor("lsw");  //填加xls檔案作者資訊
si.setApplicationName("匯出程式"); //填加xls檔案建立程式資訊
si.setLastAuthor("lsw"); //填加xls檔案最後儲存者資訊
si.setComments("lsw so cool!"); //填加xls檔案作者資訊
si.setTitle("POI匯出Excel"); //填加xls檔案標題資訊
si.setSubject("POI匯出Excel");//填加檔案主題資訊
si.setCreateDateTime(new Date());
//設定單元格樣式
HSSFCellStyle titleStyle = getHssfTitleStyle(workbook);
HSSFCellStyle headerStyle = getHssfHeaderStyle(workbook);
HSSFCellStyle cellStyle = getHssfCellStyle(workbook, headerStyle);
// 生成一個(帶標題)表格
HSSFSheet sheet = workbook.createSheet();
// 宣告一個畫圖的頂級管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定義註釋的大小和位置,詳見文件
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
// 設定註釋內容
comment.setString(new HSSFRichTextString("可以在POI中添加註釋!"));
// 設定註釋作者,當滑鼠移動到單元格上是可以在狀態列中看到該內容.
comment.setAuthor("lsw");
//設定列寬
int minBytes = colWidth < DEFAULT_COLOUMN_WIDTH ? DEFAULT_COLOUMN_WIDTH : colWidth;//至少位元組數
int[] arrColWidth = new int[headMap.size()];
// 產生表格標題行,以及設定列寬
String[] properties = new String[headMap.size()];
String[] headers = new String[headMap.size()];
        int ii = 0;
        for (Iterator<String> iter = headMap.keySet().iterator(); iter.hasNext(); ) {
            String fieldName = iter.next();
properties[ii] = fieldName;
headers[ii] = fieldName;
            int bytes = fieldName.getBytes().length;
arrColWidth[ii] = bytes < minBytes ? minBytes : bytes;
sheet.setColumnWidth(ii, arrColWidth[ii] * 256);
ii++;
}
        // 遍歷集合資料,產生資料行
int rowIndex = 0;
        for (Object obj : jsonArray) {
            if (rowIndex == 65535 || rowIndex == 0) {
                if (rowIndex != 0)
                    sheet = workbook.createSheet();//如果資料超過了,則在第二頁顯示
HSSFRow titleRow = sheet.createRow(0);//表頭 rowIndex=0
titleRow.createCell(0).setCellValue(title);
titleRow.getCell(0).setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headMap.size() - 1));
HSSFRow headerRow = sheet.createRow(1); //列頭 rowIndex =1
for (int i = 0; i < headers.length; i++) {
                    headerRow.createCell(i).setCellValue(headers[i]);
headerRow.getCell(i).setCellStyle(headerStyle);
}
                rowIndex = 2;//資料內容從 rowIndex=2開始
}
            JSONObject jo = (JSONObject) JSONObject.toJSON(obj);
HSSFRow dataRow = sheet.createRow(rowIndex);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell newCell = dataRow.createCell(i);
Object o = jo.get(properties[i]);
String cellValue = "";
                if (o == null)
                    cellValue = "";
                else if (o instanceof Date)
                    cellValue = new SimpleDateFormat(datePattern).format(o);
                else if (o instanceof Float || o instanceof Double)
                    cellValue = new BigDecimal(o.toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                else
cellValue = o.toString();
newCell.setCellValue(cellValue);
newCell.setCellStyle(cellStyle);
}
            rowIndex++;
}
        // 自動調整寬度
for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
}
        try {
            workbook.write(out);
} catch (IOException e) {
            e.printStackTrace();
}
    }

    private static HSSFCellStyle getHssfCellStyle(HSSFWorkbook workbook, HSSFCellStyle headerStyle) {
        // 單元格樣式
HSSFCellStyle cellStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor((short) 11);// 設定背景色
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont cellFont = workbook.createFont();
cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
cellStyle.setFont(cellFont);
        return cellStyle;
}

    private static HSSFCellStyle getHssfHeaderStyle(HSSFWorkbook workbook) {
        // 列頭樣式
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor((short) 11);// 設定背景色
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont headerFont = workbook.createFont();
headerFont.setFontHeightInPoints((short) 12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerStyle.setFont(headerFont);
        return headerStyle;
}

    private static HSSFCellStyle getHssfTitleStyle(HSSFWorkbook workbook) {
        //表頭樣式
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont titleFont = workbook.createFont();
titleFont.setFontHeightInPoints((short) 20);
titleFont.setBoldweight((short) 700);
titleStyle.setFont(titleFont);
        return titleStyle;
}

    /**
     * 匯出Excel 2007 OOXML (.xlsx)格式
     * @param title 標題行
     * @param headMap 屬性-列頭
     * @param jsonArray 資料集
     * @param datePattern 日期格式,傳null值則預設 年月日
     * @param colWidth 列寬 預設 至少17個位元組
     * @param out 輸出流
     */
public static void exportExcelX(String title, Map<String, String> headMap, JSONArray jsonArray, String datePattern,
                                    int colWidth, OutputStream out) {
        if (datePattern == null)
            datePattern = DEFAULT_DATE_PATTERN;
// 宣告一個工作薄
SXSSFWorkbook workbook = new SXSSFWorkbook(1000);//快取
workbook.setCompressTempFiles(true);
//定義單元格樣式
CellStyle titleStyle = getTitleStyle(workbook);
CellStyle headerStyle = getHeaderStyle(workbook);
CellStyle cellStyle = getCellStyle(workbook);
// 生成一個(帶標題)表格
Sheet sheet = workbook.createSheet();
//設定列寬
int minBytes = colWidth < DEFAULT_COLOUMN_WIDTH ? DEFAULT_COLOUMN_WIDTH : colWidth;//至少位元組數
int[] arrColWidth = new int[headMap.size()];
// 產生表格標題行,以及設定列寬
String[] properties = new String[headMap.size()];
String[] headers = new String[headMap.size()];
        int ii = 0;
        for (Iterator<String> iter = headMap.keySet().iterator(); iter.hasNext(); ) {
            String fieldName = iter.next();
properties[ii] = fieldName;
headers[ii] = headMap.get(fieldName);
            int bytes = fieldName.getBytes().length;
arrColWidth[ii] = bytes < minBytes ? minBytes : bytes;
sheet.setColumnWidth(ii, arrColWidth[ii] * 256);
ii++;
}
        // 遍歷集合資料,產生資料行
int rowIndex = 0;
        for (Object obj : jsonArray) {
            if (rowIndex == 65535 || rowIndex == 0) {
                if (rowIndex != 0)
                    sheet = workbook.createSheet();//如果資料超過了,則在第二頁顯示
Row titleRow = sheet.createRow(0);//表頭 rowIndex=0
titleRow.createCell(0).setCellValue(title);
titleRow.getCell(0).setCellStyle(titleStyle);
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headMap.size() - 1));
Row headerRow = sheet.createRow(1); //列頭 rowIndex =1
for (int i = 0; i < headers.length; i++) {
                    headerRow.createCell(i).setCellValue(headers[i]);
headerRow.getCell(i).setCellStyle(headerStyle);
}
                rowIndex = 2;//資料內容從 rowIndex=2開始
}
            JSONObject jo = (JSONObject) JSONObject.toJSON(obj);
Row dataRow = sheet.createRow(rowIndex);
            for (int i = 0; i < properties.length; i++) {
                Cell newCell = dataRow.createCell(i);
Object o = jo.get(properties[i]);
String cellValue = "";
                if (o == null)
                    cellValue = "";
                else if (o instanceof Date)
                    cellValue = new SimpleDateFormat(datePattern).format(o);
                else if (o instanceof Float || o instanceof Double)
                    cellValue = new BigDecimal(o.toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                else
cellValue = o.toString();
newCell.setCellValue(cellValue);
newCell.setCellStyle(cellStyle);
}
            rowIndex++;
}
        // 自動調整寬度
for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
}
        try {
            workbook.write(out);
//            workbook.close();
workbook.dispose();
} catch (IOException e) {
            e.printStackTrace();
}
    }

    private static CellStyle getCellStyle(SXSSFWorkbook workbook) {
        // 單元格樣式
CellStyle cellStyle = workbook.createCellStyle();
//        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
Font cellFont = workbook.createFont();
cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
cellStyle.setFont(cellFont);
        return cellStyle;
}

    private static CellStyle getHeaderStyle(SXSSFWorkbook workbook) {
        // 列頭樣式
CellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFillForegroundColor((short) 11);// 設定背景色
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
Font headerFont = workbook.createFont();
headerFont.setFontHeightInPoints((short) 12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerStyle.setFont(headerFont);
        return headerStyle;
}

    private static CellStyle getTitleStyle(SXSSFWorkbook workbook) {
        //表頭樣式
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
Font titleFont = workbook.createFont();
titleFont.setFontHeightInPoints((short) 20);
titleFont.setBoldweight((short) 700);
titleStyle.setFont(titleFont);
        return titleStyle;
}

    public static void main(String[] args) throws IOException {

        //測試匯出
int count = 140;
JSONArray ja = new JSONArray();
        for (int i = 0; i < 140; i++) {
            Student s = new Student();
s.setName("POI" + i);
s.setAge(i);
s.setBirthday(new Date());
s.setHeight(i);
s.setWeight(i);
s.setSex(i / 2 == 0 ? false : true);
ja.add(s);
}
        Map<String, String> headMap = new LinkedHashMap<String, String>();
headMap.put("name", "姓名");
headMap.put("age", "年齡");
headMap.put("birthday", "生日");
headMap.put("height", "身高");
headMap.put("weight", "體重");
headMap.put("sex", "性別");
String title = "測試";
//匯出xls
String dist_xlsPath = "C:\\Users\\Lishw\\Desktop\\匯出測試.xls";
OutputStream outXls = new FileOutputStream(dist_xlsPath);
System.out.println("正在匯出xls....");
Date d = new Date();
ExcelUtil.exportExcel(title, headMap, ja, null, 0, outXls);
System.out.println("共" + count + "條資料,執行" + (new Date().getTime() - d.getTime()) + "ms");
outXls.close();
//匯出xlsx
String dist_xlsXPath = "C:\\Users\\Lishw\\Desktop\\匯出測試.xlsx";
OutputStream outXlsX = new FileOutputStream(dist_xlsXPath);
System.out.println("正在匯出xlsx....");
Date d2 = new Date();
ExcelUtil.exportExcelX(title, headMap, ja, null, 0, outXlsX);
System.out.println("共" + count + "條資料,執行" + (new Date().getTime() - d2.getTime()) + "ms");
outXlsX.close();
//測試讀取excel
        //        String dist_xlsPath = "C:\\Users\\Lishw\\Desktop\\匯出測試.xlsx";
        //        Map<String, String> headMap = new LinkedHashMap<String, String>();
        //        headMap.put("姓名", "name");
        //        headMap.put("年齡", "age");
        //        headMap.put("生日", "birthday");
        //        headMap.put("身高", "height");
        //        headMap.put("體重", "weight");
        //        headMap.put("性別", "sex");
        //        JSONArray array  = readExcelWithTitle(dist_xlsPath,1,headMap);
        //        List<Student> list= JSON.parseArray(JSON.toJSONString(array),Student.class);
        //        list.forEach(l->System.out.println(l.toString()));
}
}
如有問題,歡迎大家指正