zoukankan      html  css  js  c++  java
  • 从数据库查询数据写入excel(Java使用POI写入Excel)

      因为要查一个线上bug,需要排查的线上订单量非常大,时间紧任务重,为了快速查询排查我花了1个多小时写了一个方法来解决这个问题,

    大概的思路:连接线上数据库,循环查询数据后写入excel文件。后续通个excel数据分析,排查问题。

      在实现我的这个思路的过程中,我需要知道怎么写入excel,我参考了Dreamer-1 写的这篇文章《Java使用POI读取和写入Excel指南》https://www.cnblogs.com/Dreamer-1/p/10469430.html,

    怎么读写excel,全中文,我觉得写的很棒。我就是抄他的。下面图解我的实现过程:

    第一步,我要先从数据库查询到我要的数据

    用了一个很好用的小的数据库插件NewbieJdbc,对于测试同学来说,我觉得非常实用,推荐使用,自己需要可以百度一下。

    通过上一步得到数据后,就要映射成需要打印的文件

    第二步输出文档

    这里有一个很重要的步骤,负责excel对象数据初始化的

    Workbook workbook = PoilWriteExcel.exportData(searchData());

    整个的PoilWriteExcel这个类都是抄的Dreamer-1 写的方法,只是把我要的数据改了一下,另外重写了我的vo对象TransExcelDataVO
    他的文章写的很详细。因为时间很急,事后再看的时候觉得后面可以根据个人的需要做其他改动,非常棒。贴一下我改的PoilWriteExcel这个类。
    public  class PoilWriteExcel {
    private static List<String> CELL_HEADS; //列头

    static{
    // 类装载时就载入指定好的列头信息,如有需要,可以考虑做成动态生成的列头
    CELL_HEADS = new ArrayList<>();
    // CELL_HEADS.add("姓名");
    // CELL_HEADS.add("年龄");
    // CELL_HEADS.add("居住城市");
    // CELL_HEADS.add("职业");
    CELL_HEADS.add("订单号");
    CELL_HEADS.add("租客号");
    CELL_HEADS.add("订单状态");
    CELL_HEADS.add("操作时间");
    CELL_HEADS.add("车辆号");
    }

    /**
    * 生成Excel并写入数据信息
    * @param dataList 数据列表
    * @return 写入数据后的工作簿对象
    */
    public static Workbook exportData(List<TransExcelDataVO> dataList){
    // 生成xlsx的Excel
    Workbook workbook = new SXSSFWorkbook();

    // 如需生成xls的Excel,请使用下面的工作簿对象,注意后续输出时文件后缀名也需更改为xls
    //Workbook workbook = new HSSFWorkbook();

    // 生成Sheet表,写入第一行的列头
    Sheet sheet = buildDataSheet(workbook);
    //构建每行的数据内容
    int rowNum = 1;
    for (Iterator<TransExcelDataVO> it = dataList.iterator(); it.hasNext(); ) {
    TransExcelDataVO data = it.next();
    if (data == null) {
    continue;
    }
    //输出行数据
    Row row = sheet.createRow(rowNum++);
    convertDataToRow(data, row);
    }
    return workbook;
    }

    /**
    * 生成sheet表,并写入第一行数据(列头)
    * @param workbook 工作簿对象
    * @return 已经写入列头的Sheet
    */
    private static Sheet buildDataSheet(Workbook workbook) {
    Sheet sheet = workbook.createSheet();
    // 设置列头宽度
    for (int i=0; i<CELL_HEADS.size(); i++) {
    sheet.setColumnWidth(i, 4000);
    }
    // 设置默认行高
    sheet.setDefaultRowHeight((short) 400);
    // 构建头单元格样式
    CellStyle cellStyle = buildHeadCellStyle(sheet.getWorkbook());
    // 写入第一行各列的数据
    Row head = sheet.createRow(0);
    for (int i = 0; i < CELL_HEADS.size(); i++) {
    Cell cell = head.createCell(i);
    cell.setCellValue(CELL_HEADS.get(i));
    cell.setCellStyle(cellStyle);
    }
    return sheet;
    }

    /**
    * 设置第一行列头的样式
    * @param workbook 工作簿对象
    * @return 单元格样式对象
    */
    private static CellStyle buildHeadCellStyle(Workbook workbook) {
    CellStyle style = workbook.createCellStyle();
    //对齐方式设置
    style.setAlignment(HorizontalAlignment.CENTER);
    //边框颜色和宽度设置
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 下边框
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); // 左边框
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(IndexedColors.BLACK.getIndex()); // 右边框
    style.setBorderTop(BorderStyle.THIN);
    style.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边框
    //设置背景颜色
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    //粗体字设置
    Font font = workbook.createFont();
    font.setBold(true);
    style.setFont(font);
    return style;
    }

    /**
    * 将数据转换成行
    * @param data 源数据
    * @param row 行对象
    * @return
    */
    private static void convertDataToRow(TransExcelDataVO data, Row row){
    // private String order_no; //订单
    // private String renter_no; //租客号
    // private String status; //订单状态
    // private String op_time; //订单操作时间
    // private String car_no; //车辆号
    int cellNum = 0;
    Cell cell;
    // 订单
    cell = row.createCell(cellNum++);
    cell.setCellValue(null == data.getOrder_no() ? "" : data.getOrder_no());

    // 租客号
    cell = row.createCell(cellNum++);
    if (null != data.getRenter_no()) {
    cell.setCellValue(data.getRenter_no());
    } else {
    cell.setCellValue("");
    }
    // 订单状态
    cell = row.createCell(cellNum++);
    cell.setCellValue(null == data.getStatus() ? "" : data.getStatus());

    // 订单操作时间
    cell = row.createCell(cellNum++);
    cell.setCellValue(null == data.getOp_time() ? "" : data.getOp_time());

    // 车辆号
    cell = row.createCell(cellNum++);
    cell.setCellValue(null == data.getCar_no() ? "" : data.getCar_no());
    }
    }

    写这文章的目的不只是分享,重要的是让自己再学习一遍。因为不经常写代码,总有很多功能有想法,但是实现起来特别难。
    最近的一段时间工作节奏不是很忙,有时间的时候我就完善自己写的app的ui自动化框架,最大的收获就是抄代码越来越快了。今天就写到这边吧。
    你再不自己敲代码,你的四肢就退化了
  • 相关阅读:
    课程设置
    专家答疑:ASP.NET MVC与WebForm的区别
    PHP环境的搭建之利器 – APMServ
    office access vs mysql
    IBM Tivoli Identity Manager 5.1 Basic Implementation
    Windows安全认证是如何进行的?
    landesk桌面管理
    转:HBase在淘宝的应用和优化小结
    Workstation ITbased Security Best Practice Configurations
    Scale Your Applications with Component Load Balancing (cont'd)
  • 原文地址:https://www.cnblogs.com/boqinyaxin/p/13627751.html
Copyright © 2011-2022 走看看