• 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);
        }
    }
  • 相关阅读:
    限制结果集行数
    函数的差异
    运算符的差异
    数据类型的差异
    简介
    Spring源码阅读入门指引
    Spring的三种注入方式
    AOP面向切面编程
    leetcode771
    leetcode669
  • 原文地址:https://www.cnblogs.com/zengqinghong/p/11991966.html
Copyright © 2020-2023  润新知