java poi导出Excel合并单元格并设置边框

复制代码
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

//写入Excel,读取模板
InputStream    stram= view.getDesktop().getWebApp().getResourceAsStream(File.separator+"Template"+File.separator+"interviewPublicity.xls");
HSSFWorkbook workbook = new HSSFWorkbook(stram);
HSSFSheet sheet = workbook.getSheetAt(0);
//写入数据
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell(0);
index = 4;
//单元格格式
row = sheet.getRow(index);
cell= row.getCell(0);
HSSFCellStyle cellStyle = cell.getCellStyle();
int num = 1;
for (Entry<String, Map<String, List<BasicDBObject>>> bmd : bmdMap.entrySet()) {
    //单位
    int deptRowIndex = index;
    int deptRowEndIndex = deptRowIndex;
    //岗位集合
    for (Entry<String, List<BasicDBObject>> empList : bmd.getValue().entrySet()) {
        //岗位
        int jobRowIndex = index;
        //考生
        for (BasicDBObject emp : empList.getValue()) {
            if (index > 4) {
                row = sheet.createRow(index);
                cell= row.createCell(0);
            }
            cell.setCellValue(num++);
            cell.setCellStyle(cellStyle);
            cell= row.createCell(3);
            cell.setCellValue(emp.getString("姓名"));
            cell.setCellStyle(cellStyle);
            cell= row.createCell(4);
            cell.setCellValue(emp.getString("examCardId"));
            cell.setCellStyle(cellStyle);
            index++;
        }
        row = sheet.getRow(jobRowIndex);
        if (empList.getValue().size() > 1) {
            int jobRowEndIndex = jobRowIndex + empList.getValue().size() - 1;
            //合并单元格
            CellRangeAddress cellRange = new CellRangeAddress(jobRowIndex, jobRowEndIndex, (short) 2, (short) 2);
            sheet.addMergedRegion(cellRange);
            //添加边框
            RegionUtil.setBorderTop(1, cellRange, sheet, workbook);
            RegionUtil.setBorderBottom(1, cellRange, sheet, workbook);
            RegionUtil.setBorderLeft(1, cellRange, sheet, workbook);
            RegionUtil.setBorderRight(1, cellRange, sheet, workbook);
        }
        cell= row.createCell(2);
        cell.setCellValue(empList.getKey());
        cell.setCellStyle(cellStyle);
        
        deptRowEndIndex += empList.getValue().size();
    }
    row = sheet.getRow(deptRowIndex);
    if (deptRowEndIndex-1 > deptRowIndex) {
        //合并单元格
        CellRangeAddress cellRange = new CellRangeAddress(deptRowIndex, deptRowEndIndex-1, (short) 1, (short) 1);
        sheet.addMergedRegion(cellRange);
        //为合并单元格添加边框
        RegionUtil.setBorderTop(1, cellRange, sheet, workbook);
        RegionUtil.setBorderBottom(1, cellRange, sheet, workbook);
        RegionUtil.setBorderLeft(1, cellRange, sheet, workbook);
        RegionUtil.setBorderRight(1, cellRange, sheet, workbook);
    }
    cell= row.createCell(1);
    cell.setCellValue(bmd.getKey());
    cell.setCellStyle(cellStyle);
}
String fileName = getExamPlan().getString("bmbName")+"面试人员名单公示.xls";
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
workbook.write(byteOut);
byteOut.close();

InputStream is = new ByteArrayInputStream(byteOut.toByteArray());
Filedownload.save(is, null, fileName);//OFBIZ导出
复制代码

 

posted @   Bob.Xie  阅读(9459)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示