java 编辑excel文件合并单元格边框显示问题
现象:
第12行 边框显示不完整。
解决方法:
误区以为不用设置跨掉的单元格 ,第9行是完整单元格,第11行是画了一个格子。
如图:
将单元格数量补全后 设置边框样式整个边框就完整了。
实现完整代码:
引入maven:
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.1</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.1</version></dependency>
代码:
可以直接拷贝运行起来。
package com.weighbridge.test;import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.StringUtils;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;public class test {public static void main(String[] args) {createdExcel();}static String format(Object oo){String object = "";if (!StringUtils.isEmpty(oo)) {object = oo.toString();} return object;}// 初始格子public static void createdExcel() {try {// 创建工作薄对象HSSFWorkbook workbook = new HSSFWorkbook();// 这里也可以设置sheet的Name// 创建工作表对象HSSFSheet sheet = workbook.createSheet();int rowNum = 0;// 跨行参数从0开始 行开始 行结束 列开始 列结束sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0,7));Row titleRow = sheet.createRow(rowNum++);titleRow.setHeightInPoints(30);Cell titleCell = titleRow.createCell(0);// 设置样式titleCell.setCellStyle(getStyleTitle(workbook));titleCell.setCellValue("统计报表");int height = 20 ;// 负责人 TODOint i=rowNum++;HSSFRow row11=sheet.createRow(i); //第三行sheet.addMergedRegion(new CellRangeAddress( i, i, 0, 7 )); //第三行的 第0列到第7列 合并单元格row11.setHeightInPoints(height);row11.createCell(0).setCellValue("交货单位:一车间 2021年03月09日 单位:kg、%vol "); //赋值row11.createCell(1); //赋值row11.createCell(2);row11.createCell(3);row11.createCell(4);row11.createCell(5);row11.createCell(6);row11.createCell(7);for (Cell cell : row11) {cell.setCellStyle(getStyleCell(workbook));}HSSFCellStyle styleCell = getStyleHeadCell(workbook);// 设置列宽 TODOsheet.setColumnWidth(0, 5500);sheet.setColumnWidth(1, 5500);sheet.setColumnWidth(2, 5500);sheet.setColumnWidth(3, 5500);sheet.setColumnWidth(4, 5500);sheet.setColumnWidth(5, 5500);sheet.setColumnWidth(6, 5500);sheet.setColumnWidth(7, 5500);// 创建工作表的行HSSFRow row = sheet.createRow(rowNum++);// 设置第一行,从零开始row.setHeightInPoints(height);int is = 0 ; HSSFCell createCell1 = row.createCell(is++);createCell1.setCellStyle(styleCell);createCell1.setCellValue("单位");// 第一行单位createCell1 = row.createCell(is++);createCell1.setCellStyle(styleCell);createCell1.setCellValue("入库数");// 第一行 入库度大createCell1 = row.createCell(is++);createCell1.setCellStyle(styleCell);createCell1.setCellValue("二入库数");// 第一行 入库度二createCell1 = row.createCell(is++);createCell1.setCellStyle(styleCell);createCell1.setCellValue("合计头");// 第一行合计头createCell1 = row.createCell(is++);createCell1.setCellStyle(styleCell);createCell1.setCellValue("入库量");// 第一行入库量大createCell1 = row.createCell(is++);createCell1.setCellStyle(styleCell);createCell1.setCellValue("入库量二");// 第一行入库量二createCell1 = row.createCell(is++);createCell1.setCellStyle(styleCell);createCell1.setCellValue("合计");// 第一行合计createCell1 = row.createCell(is++);createCell1.setCellStyle(styleCell);createCell1.setCellValue("扣除损耗合计");// 第一行扣除损耗合计HSSFRow row1 = sheet.createRow(rowNum++);// 设置第二行,从零开始row1.setHeightInPoints(height);styleCell = getStyleCell(workbook);String ss = "[ { \"departmentName\": \"一组\", \"hj\": 40.0, \"drclljshh\": 0.0, \"alcoholContent2\": 0.0, \"alcoholContent1\": 65.0, \"jdbdblj\": 0.0, \"optCounter\": 0, \"updatedDate\": 1634199611860, \"delFlag\": 0, \"clljshq\": 0.0, \"jubdbgs\": 0, \"kcshhj\": 39.85, \"createdDate\": 1633934873000, \"clljshh\": 0.0, \"headWeight\": 5.0, \"drjdbdblj\": 0.0, \"jtbdbgs\": 0, \"departmentNo\": \"01010\", \"netWeight1\": 35.0, \"id\": \"0a5d9f2753f34a698f90158f3b2c223a\", \"netWeight2\": 0.0, \"drclljshq\": 0.0 } ]";JSONArray parseArray = JSON.parseArray(ss);for (Object object : parseArray) {Map<String, Object> map = (Map<String, Object>) object;i = 0 ; HSSFCell createCell2 = row1.createCell(i++);createCell2.setCellStyle(styleCell);createCell2.setCellValue(format(map.get("departmentName")));// 第一行单位createCell2 = row1.createCell(i++);createCell2.setCellStyle(styleCell);createCell2.setCellValue(format(map.get("alcoholContent1")));//第一行 入库度大createCell2 = row1.createCell(i++);createCell2.setCellStyle(styleCell);createCell2.setCellValue(format(map.get("alcoholContent2")));// 第一行 入库度二createCell2 = row1.createCell(i++);createCell2.setCellStyle(styleCell);createCell2.setCellValue(format(map.get("headWeight")));// 第一行合计头createCell2 = row1.createCell(i++);createCell2.setCellStyle(styleCell);createCell2.setCellValue(format(map.get("netWeight1")));// 第一行入库量大createCell2 = row1.createCell(i++);createCell2.setCellStyle(styleCell);createCell2.setCellValue(format(map.get("netWeight2")));// 第一行入库量二createCell2 = row1.createCell(i++);createCell2.setCellStyle(styleCell);createCell2.setCellValue(format(map.get("hj")));// 第一行合计createCell2 = row1.createCell(i++);createCell2.setCellStyle(styleCell);createCell2.setCellValue(format(map.get("kcshhj")));// 第一行扣除损耗合计} // 备注 合计 i = rowNum++;HSSFRow rowRemake = sheet.createRow(i);rowRemake.setHeightInPoints(height);// 跨行参数从0开始 行开始 行结束 列开始 列结束sheet.addMergedRegion(new CellRangeAddress(i, i, 0,1));rowRemake.createCell(0).setCellValue("备注:各组各扣除0.15的损耗量"); //赋值rowRemake.createCell(1); //赋值rowRemake.createCell(2).setCellValue("合计"); rowRemake.createCell(3).setCellValue("2"); rowRemake.createCell(4).setCellValue("2"); rowRemake.createCell(5).setCellValue("2"); rowRemake.createCell(6).setCellValue("2"); rowRemake.createCell(7).setCellValue("2"); for (Cell cell : rowRemake) {cell.setCellStyle(getStyleCell(workbook));}// 负责人 TODOi=rowNum++;row11=sheet.createRow(i); //第三行row11.setHeightInPoints(height);sheet.addMergedRegion(new CellRangeAddress( i, i, 0, 7 )); //第三行的 第0列到第7列 合并单元格row11.createCell(0).setCellValue("负责人: 统计员: 报出日期: "); //赋值row11.createCell(1); //赋值row11.createCell(2);row11.createCell(3);row11.createCell(4);row11.createCell(5);row11.createCell(6);row11.createCell(7);for (Cell cell : row11) {cell.setCellStyle(getStyleCell(workbook));}height = 20 ;// 负责人 TODOi=9; // 9 行row11=sheet.createRow(i); //第三行sheet.addMergedRegion(new CellRangeAddress( i, i, 0, 7 )); //第三行的 第0列到第7列 合并单元格row11.setHeightInPoints(height);row11.createCell(0).setCellValue("测试跨行"); //赋值row11.createCell(1); //赋值row11.createCell(2);row11.createCell(3);row11.createCell(4);row11.createCell(5);row11.createCell(6);row11.createCell(7);for (Cell cell : row11) {cell.setCellStyle(getStyleCell(workbook));}height = 20 ;// 负责人 TODOi=11; // 11 行row11=sheet.createRow(i); //第三行sheet.addMergedRegion(new CellRangeAddress( i, i, 0, 7 )); //第三行的 第0列到第7列 合并单元格row11.setHeightInPoints(height);row11.createCell(0).setCellValue("测试跨行2"); //赋值
// row11.createCell(1); //赋值
// row11.createCell(2);
// row11.createCell(3);
// row11.createCell(4);
// row11.createCell(5);
// row11.createCell(6);
// row11.createCell(7);for (Cell cell : row11) {cell.setCellStyle(getStyleCell(workbook));}workbook.setSheetName(0, "sheetName");// 设置sheet的Name// 文档输出dFileOutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\test\\"+ new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() + ".xls");workbook.write(out);out.close();} catch (Exception e) {e.printStackTrace();}}/* * 标题样式*/ public static HSSFCellStyle getStyleTitle(HSSFWorkbook workbook) {// 设置字体HSSFFont font = workbook.createFont();//设置字体大小font.setFontHeightInPoints((short)15);//字体加粗font.setBold(true); //设置字体名字 font.setFontName("Arial");//设置样式; HSSFCellStyle style = workbook.createCellStyle();//在样式用应用设置的字体; style.setFont(font);//设置自动换行; style.setWrapText(false);//设置水平对齐的样式为居中对齐; style.setAlignment(HorizontalAlignment.CENTER);//设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(VerticalAlignment.CENTER);style.setWrapText(true);return style;}/* * 列数据信息单元格样式*/ public static HSSFCellStyle getStyleCell(HSSFWorkbook workbook) {// 设置字体HSSFFont font = workbook.createFont();//设置字体大小font.setFontHeightInPoints((short)12);//字体加粗
// font.setBold(true); //设置字体名字 font.setFontName("Arial");//设置样式; HSSFCellStyle style = workbook.createCellStyle();//在样式用应用设置的字体; style.setFont(font);//设置自动换行; style.setWrapText(false);//设置水平对齐的样式为居中对齐; style.setAlignment(HorizontalAlignment.CENTER);//设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(VerticalAlignment.CENTER);//设置样式对象,这里仅设置了边框属性style.setBorderBottom(BorderStyle.THIN); //下边框style.setBorderLeft(BorderStyle.THIN);//左边框style.setBorderTop(BorderStyle.THIN);//上边框style.setBorderRight(BorderStyle.THIN);//右边框return style;}/* * 头列数据信息单元格样式*/ public static HSSFCellStyle getStyleHeadCell(HSSFWorkbook workbook) {//设置样式; HSSFCellStyle style = workbook.createCellStyle();style = workbook.createCellStyle();style.setAlignment(HorizontalAlignment.CENTER);style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setFillPattern(FillPatternType.SOLID_FOREGROUND);Font headerFont = workbook.createFont();headerFont.setFontName("Arial");headerFont.setFontHeightInPoints((short) 10);headerFont.setBold(true);headerFont.setColor(IndexedColors.WHITE.getIndex());style.setFont(headerFont);//设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(VerticalAlignment.CENTER);return style;}}
java 编辑excel文件合并单元格边框显示问题相关推荐
- 使用poi导出excel,及合并单元格边框显示问题
使用poi导出excel,及合并单元格边框显示问题. 首先创建workbook.sheet HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet s ...
- java实现excel导出合并单元格
随着数据的不断增长,很多时候需要将数据导出到Excel中进行分析.处理和展示.而Java作为一种流行的编程语言,自然也提供了很多实现Excel导出的方法.本文将介绍如何使用Java实现Excel导出, ...
- Java:Excel写入“合并单元格“
目录 1. Maven仓库下载导入 2.写入 2.1 数据格式 2.2 代码 2.2 输出的Excel结果: 附录: 本文以Java示例展示Excel中的写入"合并单元格"的方法. ...
- JAVA导出excel 动态合并单元格
JAVA excel合并单元格原生poi 合并后的效果 直接上代码 (该方法为如果指定行的单元格里面的值一致则进行合并,直接粘贴使用即可) ** @param sheet* @param colIdx ...
- Java操作excel之合并单元格、写入强制换行数据
1.初始化Excel表格 HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(&quo ...
- poi excel设置合并单元格边框格式
版本3.17 //设置合并单元格的边框 public static void setBorderForMergeCell(BorderStyle style,int color, CellRange ...
- java下载excel实现合并单元格功能
在下载excel中偶尔会出现需要合并单元格的情况再导出,代码如下: // 1.创建工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 标题合并单元格设 ...
- Java 利用hutool工具实现导出excel并合并单元格
Java 利用hutool工具实现导出excel并合并单元格 controller层调用service,就一个核心方法,没错就下面这个代码就能实现了.前提是项目里面要引用hutool包.把我这个复制到 ...
- Springboot导出excel,合并单元格示例
原文链接:Springboot导出excel,合并单元格示例 更多文章,欢迎访问:Java知音,一个专注于技术分享的网站 以下用一个示例来说明springboot如何导出数据到excel. 首先引入M ...
最新文章
- oracle proedure 文件_关于oracle存储过程读取文件
- Android中的ViewPager2
- access和mysql4注入小技巧
- Python中fastapi关于异常或者错误的处理
- linux上apache目录在哪,Linux下Apache目录明细
- 【渝粤教育】电大中专建筑材料 (4)作业 题库
- Qt 去除控件边框线
- CodeForces - 274B Zero Tree
- PASCAL VOC数据集简介
- 87键盘怎么用小键盘功能
- 支付宝转账提现相关问题
- python习题练习1224
- 【个人学年总结】大一——“蛰伏”的一年
- 7 展讯Sprd设置-电池-关联自启动-跟踪代码
- Python中的对日期时间的处理
- 解决pcntl_fork() has been disabled for security reasons in file /www/wwwroot/192.168.21.2/vendor/wor
- 天梯赛题目练习L1-001,L1-002,L1-003
- 情人节程序员用HTML网页表白【爱心表白】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
- POJ 2245 Addition Chains(算竞进阶习题)
- 【附源码】Python计算机毕业设计水库洪水预报调度系统
热门文章
- 工程伦理第四章习题答案
- threejs正方体六面贴图
- Java/171. Excel Sheet Column Number Excel序号转换数字
- 使用GIMP for Mac
- 163vip邮箱登录,163邮箱怎么登陆?如何登录163vip邮箱?
- “西游记之大圣归来”短评主题分析-Latent Dirichlet Allocation
- ClinkHouse基本使用说明
- 云免流usb共享电脑_云免流usb共享电脑
- Photo Ninja for Mac(RAW图片转换器)
- Codeforces 802 补题