• java-Excel导出中的坑


    在Excel导出过程中,若遇到合并单元格样式只有第一行合并,而下面要合并的行没有边框显示。

    一般问题出在将单元格样式设置与合并单元格放在同一个循环中导致。

    以下为一个完整版的demo以供参考

    定义边框样式方法:

     1 package com.ccb.excel.export;
     2 
     3 import java.io.Serializable;
     4 import java.net.URLEncoder;
     5 
     6 import javax.servlet.http.HttpServletRequest;
     7 
     8 import org.apache.commons.lang.StringUtils;
     9 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    10 import org.apache.poi.hssf.usermodel.HSSFFont;
    11 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    12 import org.apache.poi.hssf.util.HSSFColor;
    13 
    14 public class ExportFileNameUtils implements Serializable {
    15     public static HSSFCellStyle initColumnHeadStyle(HSSFWorkbook wb) {  
    16         HSSFCellStyle columnHeadStyle = wb.createCellStyle();  
    17         HSSFFont columnHeadFont = wb.createFont();  
    18         columnHeadFont.setFontName("宋体");  
    19         columnHeadFont.setFontHeightInPoints((short) 10);  
    20         columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
    21         columnHeadStyle.setFont(columnHeadFont);  
    22         columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
    23         columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中  
    24         columnHeadStyle.setLocked(true);  
    25         columnHeadStyle.setWrapText(true);  
    26         columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色  
    27         columnHeadStyle.setBorderLeft((short) 1);// 边框的大小  
    28         columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色  
    29         columnHeadStyle.setBorderRight((short) 1);// 边框的大小  
    30         columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体  
    31         columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色  
    32         // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)  
    33         columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);  
    34         return columnHeadStyle;  
    35     }  
    36 
    37     public static HSSFCellStyle initColumnCenterstyle(HSSFWorkbook wb) {  
    38         HSSFFont font = wb.createFont();  
    39         font.setFontName("宋体");  
    40         font.setFontHeightInPoints((short) 10);  
    41         HSSFCellStyle centerstyle = wb.createCellStyle();  
    42         centerstyle.setFont(font);  
    43         centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
    44         centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中  
    45         centerstyle.setWrapText(true);  
    46         centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);  
    47         centerstyle.setBorderLeft((short) 1);  
    48         centerstyle.setRightBorderColor(HSSFColor.BLACK.index);  
    49         centerstyle.setBorderRight((short) 1);  
    50         centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体  
    51         centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.  
    52         centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.  
    53         return centerstyle;  
    54     }
    55 }
    View Code

    调用过程中步骤应为:

    1、初始化带边框的表头样式

    2、填充单元格数据

    3、合并单元格

     1 package com.ccb.excel.export;
     2 
     3 import java.io.FileOutputStream;
     4 
     5 import org.apache.poi.hssf.usermodel.HSSFCell;
     6 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
     7 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
     8 import org.apache.poi.hssf.usermodel.HSSFRow;
     9 import org.apache.poi.hssf.usermodel.HSSFSheet;
    10 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    11 import org.apache.poi.hssf.util.CellRangeAddress;
    12 
    13 public class ExcelExport {
    14     public static void main(String[] args) throws Exception {  
    15         HSSFWorkbook wb = new HSSFWorkbook();// 创建一个Excel文件  
    16         HSSFSheet sheet = wb.createSheet("银行存余额表(1)");// 创建一个Excel的Sheet  
    17         // 定义样式  
    18         HSSFCellStyle cellStyleCenter = ExportFileNameUtils.initColumnHeadStyle(wb);//表头样工  
    19         HSSFCellStyle cellStyleRight = ExportFileNameUtils.initColumnCenterstyle(wb);//单元格样式  
    20         HSSFCellStyle cellStyleLeft = ExportFileNameUtils.initColumnCenterstyle(wb);  
    21         cellStyleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//右对齐  
    22         cellStyleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左对齐  
    23         // 设置列宽  
    24         sheet.setColumnWidth(0, 7200);  
    25         sheet.setColumnWidth(1, 5000);  
    26         sheet.setColumnWidth(2, 5000);  
    27         sheet.setColumnWidth(3, 5000);  
    28         sheet.setColumnWidth(4, 5000);  
    29         sheet.setColumnWidth(5, 5000);  
    30         try {  
    31             HSSFRow row = null;  
    32             HSSFCell cell = null;  
    33             // ---------------------------1.初始化带边框的表头------------------------------  
    34             for (int i = 0; i < 5; i++) {  
    35                 row = sheet.createRow(i);  
    36                 for (int j = 0; j <= 5; j++) {  
    37                     cell = row.createCell(j);  
    38                     cell.setCellStyle(cellStyleCenter);  
    39                 }  
    40             }  
    41             // ---------------------------2.填充单元格数据------------------------------  
    42             cell = sheet.getRow(0).getCell(0);  
    43             cell.setCellValue(new HSSFRichTextString("银行存余额表"));  
    44             cell = sheet.getRow(1).getCell(0);  
    45             cell.setCellValue(new HSSFRichTextString("2018-01-31"));  
    46             cell = sheet.getRow(2).getCell(0);  
    47             cell.setCellValue(new HSSFRichTextString("开户行"));  
    48             cell = sheet.getRow(2).getCell(1);  
    49             cell.setCellValue(new HSSFRichTextString("活期"));  
    50             cell = sheet.getRow(2).getCell(3);  
    51             cell.setCellValue(new HSSFRichTextString("定期"));  
    52             cell = sheet.getRow(2).getCell(5);  
    53             cell.setCellValue(new HSSFRichTextString("存款合计"));  
    54             cell = sheet.getRow(3).getCell(1);  
    55             cell.setCellValue(new HSSFRichTextString(" "));  
    56             cell = sheet.getRow(3).getCell(4);  
    57             cell.setCellValue(new HSSFRichTextString("折合本位币合计"));  
    58             cell = sheet.getRow(4).getCell(1);  
    59             cell.setCellValue(new HSSFRichTextString("人民币"));  
    60             cell = sheet.getRow(4).getCell(2);  
    61             cell.setCellValue(new HSSFRichTextString("折合本位币合计"));  
    62   
    63             // ---------------------------3.合并单元格------------------------------  
    64             sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));// 开始行,结束行,开始列,结束列  
    65             sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 5));  
    66             sheet.addMergedRegion(new CellRangeAddress(2, 4, 0, 0));  
    67             sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 2));  
    68             sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 4));  
    69             sheet.addMergedRegion(new CellRangeAddress(3, 4, 4, 4));  
    70             sheet.addMergedRegion(new CellRangeAddress(2, 4, 5, 5));  
    71             FileOutputStream fileOut = new FileOutputStream("d:\银行存款余额表.xls");  
    72             wb.write(fileOut);  
    73             fileOut.close();  
    74         } catch (Exception e) {  
    75             e.printStackTrace();  
    76         }  
    77     }  
    78 
    79 }
    View Code
  • 相关阅读:
    mapreduce 的过程
    bootstrap当中,实现一些常用的元素居中
    如何理解人工智能、机器学习和深度学习三者的关系
    MapReduce的局限性
    MapReduce的计算资源划分
    Java中的堆和栈的区别
    java面试01-网络知识
    01Java经典问题
    06数据库复习03
    05数据库复习02
  • 原文地址:https://www.cnblogs.com/lq147760524/p/8392460.html
Copyright © 2020-2023  润新知