poi导出(生成)excel基础代码

    public XSSFWorkbook generateWarterAlarmMsgExcel(List<WarterAlarmVO> warterAlarmVOS, String date, String timeType) throws ParseException {

        if(warterAlarmVOS != null && warterAlarmVOS.size() != 0){
            // 创建一个webbook,对应一个Excel文件---反馈导入情况
            XSSFWorkbook backBook = new XSSFWorkbook();
            // 设置各列宽度
            Sheet bSheet = backBook.createSheet("故障登记表");
            bSheet.setColumnWidth(0, 120 * 20);
            bSheet.setColumnWidth(1, 120 * 100);
            bSheet.setColumnWidth(2, 120 * 30);
            bSheet.setColumnWidth(3, 120 * 60);
            bSheet.setColumnWidth(4, 120 * 50);
            bSheet.setColumnWidth(5, 120 * 40);
            bSheet.setColumnWidth(6, 120 * 40);
            bSheet.setColumnWidth(7, 120 * 40);
            bSheet.setColumnWidth(8, 120 * 70);

            // 居中加粗样式
            CellStyle cellStyle = backBook.createCellStyle();
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

            // 字体加粗
            Font font = backBook.createFont();
            font.setBoldweight((short) 80);
            cellStyle.setFont(font);

            //字体大小
            Font fontSize = backBook.createFont();
            fontSize.setFontHeightInPoints((short) 12);//设置excel数据字体大小

            // 居中不加粗样式
            CellStyle cellStyleCenter = backBook.createCellStyle();
            cellStyleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyleCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

            // 在sheet中添加表头第1行---行
            Row bRow = bSheet.createRow(0);
            // 表格
            Cell bCell = null;
            // 表头第一行内容
            bCell = bRow.createCell(0);
            bCell.setCellValue("序号");
            bCell.setCellStyle(cellStyle);
            bCell = bRow.createCell(1);
            bCell.setCellValue("联网单位名称");
            bCell.setCellStyle(cellStyle);
            bCell = bRow.createCell(2);
            bCell.setCellValue("水警信息数量");
            bCell.setCellStyle(cellStyle);
            bCell = bRow.createCell(3);
            bCell.setCellValue("报警点位");
            bCell.setCellStyle(cellStyle);
            bCell = bRow.createCell(4);
            bCell.setCellValue("是否电话通知");
            bCell.setCellStyle(cellStyle);
            bCell = bRow.createCell(5);
            bCell.setCellValue("联网单位消防负责人");
            bCell.setCellStyle(cellStyle);
            bCell = bRow.createCell(6);
            bCell.setCellValue("负责人电话");
            bCell.setCellStyle(cellStyle);
            bCell = bRow.createCell(7);
            bCell.setCellValue("备注");
            bCell.setCellStyle(cellStyle);
            bCell = bRow.createCell(8);
            bCell.setCellValue("日期");
            bCell.setCellStyle(cellStyle);
            bRow.setHeight((short) 1000);

            String dateRange = generateExcelDate(date,timeType);
            // 遍历---向数据库中添加数据
            //数据从第二行开始写入
            int rowFlag = 1;
            int num = 1;
            for (WarterAlarmVO warterAlarmVO : warterAlarmVOS) {

                bRow = bSheet.createRow(rowFlag);
                bRow.createCell(0).setCellValue(num);
                bRow.createCell(1).setCellValue(warterAlarmVO.getCompanyName());
                bRow.createCell(2).setCellValue(warterAlarmVO.getWarterAlarmCount());
                bRow.createCell(3).setCellValue(warterAlarmVO.getAlarmPointPosition());
                bRow.createCell(4).setCellValue("是");
                bRow.createCell(5).setCellValue(warterAlarmVO.getFiresafetyDutyPersonName());
                bRow.createCell(6).setCellValue(warterAlarmVO.getFiresafetyDutyPhoneNumber());
                bRow.createCell(7).setCellValue(warterAlarmVO.getRemark());
                bRow.createCell(8).setCellValue(dateRange);
                bRow.setRowStyle(cellStyle);
                num++;
                rowFlag++;
            }
            //合并表头
            bSheet.addMergedRegion(new CellRangeAddress(1, rowFlag-1, 8, 8));

            return backBook;
        }
        else {
            return getAlarmFail("未查询到数据!","报警数统计报表");
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以帮你生成一段Java代码来使用POI导出Excel文件。以下是一个示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class ExcelExporter { public static void exportDataToExcel() { // 创建工作簿 Workbook workbook = new XSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建行对象 Row row = sheet.createRow(0); // 创建单元格样式 CellStyle headerCellStyle = workbook.createCellStyle(); headerCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 创建标题行 Cell cell = row.createCell(0); cell.setCellValue("姓名"); cell.setCellStyle(headerCellStyle); cell = row.createCell(1); cell.setCellValue("年龄"); cell.setCellStyle(headerCellStyle); // 写入数据行 row = sheet.createRow(1); row.createCell(0).setCellValue("张三"); row.createCell(1).setCellValue(25); row = sheet.createRow(2); row.createCell(0).setCellValue("李四"); row.createCell(1).setCellValue(30); // 调整列宽 sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); // 保存Excel文件 try { FileOutputStream outputStream = new FileOutputStream("output.xlsx"); workbook.write(outputStream); workbook.close(); outputStream.close(); System.out.println("Excel导出成功!"); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { exportDataToExcel(); } } ``` 以上代码演示了如何使用POI库创建一个Excel文件,并将数据写入到工作表中。你可以根据自己的需求进行修改和扩展。记得在运行代码之前,确保已经将POI库添加到你的项目中。运行代码后,将会在项目目录下生成一个名为"output.xlsx"的Excel文件,其中包含导出的数据。 希望这段代码对你有所帮助!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值