Excel工具类

Posted zengqinghong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Excel工具类相关的知识,希望对你有一定的参考价值。

Excel需要导入的jar包

    <!--  导出excel  -->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.16</version>
    </dependency>

Excel工具类的代码

import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtil {

    public static void datasToExcel(List<String> fieldList, List<Map<String, Object>> dataList, String fileDir, String fileName){
        SXSSFWorkbook wbs = new SXSSFWorkbook(2000);
        CellStyle headStyle = wbs.createCellStyle();
        headStyle.setBorderBottom(CellStyle.BORDER_THIN);
        headStyle.setBorderLeft(CellStyle.BORDER_THIN);
        headStyle.setBorderRight(CellStyle.BORDER_THIN);
        headStyle.setBorderTop(CellStyle.BORDER_THIN);
        headStyle.setAlignment(CellStyle.ALIGN_CENTER);
        Font font = wbs.createFont();
        wbs.createFont().setBoldweight(Font.BOLDWEIGHT_BOLD);
        headStyle.setFont(font);
        CellStyle contentStyle = wbs.createCellStyle();
        contentStyle.setBorderBottom(CellStyle.BORDER_THIN);
        contentStyle.setBorderLeft(CellStyle.BORDER_THIN);
        contentStyle.setBorderRight(CellStyle.BORDER_THIN);
        contentStyle.setBorderTop(CellStyle.BORDER_THIN);

        // 设置工作蒲的标签名
        SXSSFSheet sheet =  wbs.createSheet("firstSheet");
        SXSSFRow headRow = sheet.createRow(0);
        // 设置excel表头字段列
        for (int i = 0; i < fieldList.size(); i++) {
            SXSSFCell headCell = headRow.createCell(i);
            headCell.setCellStyle(headStyle);
            headCell.setCellValue(fieldList.get(i));
        }
        // 设置excel内容列
        for(int i = 0; i < dataList.size(); i++){
            SXSSFRow contentRow = sheet.createRow(i + 1);
            for (int j = 0; j < fieldList.size(); j++) {
                SXSSFCell contentCell = contentRow.createCell(j);
                contentCell.setCellStyle(contentStyle);

                Object o = dataList.get(i).get(fieldList.get(j));
                contentCell.setCellValue(o==null?"":String.valueOf(o));
            }
        }

        try {
            String filePath = new StringBuilder(fileDir).append(File.separator).append(fileName).toString();
            File file = new File(filePath);
            OutputStream fileOut = new FileOutputStream(file);
            wbs.write(fileOut);
            fileOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            wbs.dispose();
        }
    }
    // field:字段
    public static void main(String[] args) {
        List<String> fieldList = new ArrayList<>();
        fieldList.add("id");
        fieldList.add("name");
        fieldList.add("age");

        List<Map<String, Object>> dataList = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("id", "1");
        map.put("name", "zqd");
        map.put("age", 22);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", "2");
        map2.put("name", "zqd2");
        map2.put("age", 23);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("id","3");
        map3.put("name","zaq");
        map3.put("age",21);

        dataList.add(map);
        dataList.add(map2);
        dataList.add(map3);

        // 文件的路径
        String fileDir = "D:\git_test\test";
        // 文件名   Excel:excel:2007-2016版用的是:xlsx 拓展名   excel:97-2003版用的是:xls 拓展名
        String fileName = "test.xlsx";

        datasToExcel(fieldList, dataList, fileDir, fileName);
    }
}

 

以上是关于Excel工具类的主要内容,如果未能解决你的问题,请参考以下文章

Excel通用类工具

Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

四EasyExcel实现Excel读写,封装工具类

四EasyExcel实现Excel读写,封装工具类

Excel通用类工具

几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!