目录

1、 Maven仓库下载导入

2、写入

2.1 数据格式

2.2 代码

2.2 输出的Excel结果:

附录:


本文以Java示例展示Excel中的写入“合并单元格”的方法。

1、 Maven仓库下载导入

在pom.xml中配置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>

2、写入

2.1 数据格式

要把每个单元格信息(包含:位置、值等)都一一列举清楚,

需要的字段:

位置:firstRow,lastRow,firstCol,lastCol;(从0开始)

值:value;

[{"firstRow":0,"lastRow":6,"lastCol":0,"firstCol":0,"value":"秦时明月汉时关"},{"firstRow":0,"lastRow":0,"lastCol":1,"firstCol":1,"value":"万"},{"firstRow":1,"lastRow":1,"lastCol":1,"firstCol":1,"value":"里"},{"firstRow":2,"lastRow":2,"lastCol":1,"firstCol":1,"value":"长"},{"firstRow":3,"lastRow":3,"lastCol":1,"firstCol":1,"value":"征"},{"firstRow":4,"lastRow":4,"lastCol":1,"firstCol":1,"value":"人"},{"firstRow":5,"lastRow":5,"lastCol":1,"firstCol":1,"value":"未"},{"firstRow":6,"lastRow":6,"lastCol":1,"firstCol":1,"value":"还"},{"firstRow":3,"lastRow":3,"lastCol":2,"firstCol":2,"value":"但使龙城飞将在"},{"firstRow":0,"lastRow":0,"lastCol":3,"firstCol":3,"value":"不"},{"firstRow":1,"lastRow":1,"lastCol":3,"firstCol":3,"value":"教"},{"firstRow":2,"lastRow":2,"lastCol":3,"firstCol":3,"value":"胡"},{"firstRow":3,"lastRow":3,"lastCol":3,"firstCol":3,"value":"马"},{"firstRow":4,"lastRow":4,"lastCol":3,"firstCol":3,"value":"度"},{"firstRow":5,"lastRow":5,"lastCol":3,"firstCol":3,"value":"阴"},{"firstRow":6,"lastRow":6,"lastCol":3,"firstCol":3,"value":"山"}]

2.2 代码

package com;import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;public class ExcelTest {/*** 写入excel数据** @param path    excel保存路径* @param name    excel名称* @param version excel格式,支持xls、xlsx* @param rows    excel单元格信息(包含位置、值等信息,rows中每个Json必须包含位置和值的信息,*                位置:firstRow,lastRow,firstCol,lastCol,值:value)*/public static void writeMergeExcel(String path, String name, String version,List<JSONObject> rows) {try {Workbook wb = produce(version);createSheet(wb, name, version, rows,null);String filePath = path + name;if (!filePath.endsWith(version)) {filePath = filePath +"."+ version;}FileOutputStream fileOut = new FileOutputStream(filePath);wb.write(fileOut);fileOut.close();} catch (Exception e) {e.printStackTrace();}}/*** 写入excel数据** @param path    excel保存路径* @param name    excel名称* @param version excel格式,支持xls、xlsx* @param rows    excel单元格信息(包含位置、值等信息)* @param rowKeys excel单元格字段信息(rows中每个Json必须包含位置和值的信息,如果存储的字段与工具中的字段不一致,做个映射,工具中的字段::firstRow,lastRow,firstCol,lastCol,值:value)*/public static void writeMergeExcel(String path, String name, String version,List<JSONObject> rows, JSONObject rowKeys) {try {Workbook wb = produce(version);createSheet(wb, name, version, rows,  rowKeys);String filePath = path + name;if (!filePath.endsWith(version)) {filePath = filePath +"."+ version;}FileOutputStream fileOut = new FileOutputStream(filePath);wb.write(fileOut);fileOut.close();} catch (Exception e) {e.printStackTrace();}}private static void createSheet(Workbook wb, String name, String version,List<JSONObject> rows, JSONObject rowKeys) {Integer max = getMaxRowNumber(version);int sheetNum = 1;Sheet sheet = wb.createSheet(name + sheetNum);CellStyle style = wb.createCellStyle();style.setAlignment(HorizontalAlignment.LEFT);//设置水平对齐方式style.setVerticalAlignment(VerticalAlignment.CENTER); //设置垂直对齐方式String firstRowKey = rowKeys != null ? rowKeys.getString("firstRow"):"firstRow";String lastRowKey  = rowKeys != null ? rowKeys.getString("lastRow"):"lastRow";String firstColKey = rowKeys != null ? rowKeys.getString("firstCol"):"firstCol";String lastColKey  = rowKeys != null ? rowKeys.getString("lastCol"):"lastCol";String valueKey    = rowKeys != null ? rowKeys.getString("value"):"value";for (JSONObject rowJson : rows) {int lastRow  = rowJson.getInteger(lastRowKey);if (lastRow > max){List<JSONObject> subRows = rows.subList(lastRow,rows.size());createSheet(wb, name, version, subRows,  rowKeys);} else{int firstRow = rowJson.getInteger(firstRowKey);int firstCol = rowJson.getInteger(firstColKey);int lastCol  = rowJson.getInteger(lastColKey);System.out.println(firstRow+"_"+lastRow +"_"+ firstCol +"_"+ lastCol);if (lastCol - firstCol > 0 || lastRow - firstRow > 0 ){CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);sheet.addMergedRegion(cellRangeAddress);}Row row ;if (sheet.getRow(firstRow)!=null){row = sheet.getRow(firstRow);} else {row = sheet.createRow(firstRow);}Cell cell = row.createCell(firstCol);cell.setCellValue(rowJson.getString(valueKey));cell.setCellStyle(style);}}}public static Workbook produce(String version) {switch (version) {case "xls":return new HSSFWorkbook();case "xlsx":return new XSSFWorkbook();case "sxlsx":return new SXSSFWorkbook();default:return null;}}private static int maxXls = 65024;private static int maxXlsx = 1048576;public static Integer getMaxRowNumber(String version) {switch (version) {case "xls":return maxXls;case "xlsx":return maxXlsx;case "sxlsx":return maxXlsx;default:return null;}}public static void main(String[] args) {try {List<JSONObject> list = (List<JSONObject>) JSONObject.parse("[{\"firstRow\":0,\"lastRow\":6,\"lastCol\":0,\"firstCol\":0,\"value\":\"秦时明月汉时关\"},{\"firstRow\":0,\"lastRow\":0,\"lastCol\":1,\"firstCol\":1,\"value\":\"万\"},{\"firstRow\":1,\"lastRow\":1,\"lastCol\":1,\"firstCol\":1,\"value\":\"里\"},{\"firstRow\":2,\"lastRow\":2,\"lastCol\":1,\"firstCol\":1,\"value\":\"长\"},{\"firstRow\":3,\"lastRow\":3,\"lastCol\":1,\"firstCol\":1,\"value\":\"征\"},{\"firstRow\":4,\"lastRow\":4,\"lastCol\":1,\"firstCol\":1,\"value\":\"人\"},{\"firstRow\":5,\"lastRow\":5,\"lastCol\":1,\"firstCol\":1,\"value\":\"未\"},{\"firstRow\":6,\"lastRow\":6,\"lastCol\":1,\"firstCol\":1,\"value\":\"还\"},{\"firstRow\":3,\"lastRow\":3,\"lastCol\":2,\"firstCol\":2,\"value\":\"但使龙城飞将在\"},{\"firstRow\":0,\"lastRow\":0,\"lastCol\":3,\"firstCol\":3,\"value\":\"不\"},{\"firstRow\":1,\"lastRow\":1,\"lastCol\":3,\"firstCol\":3,\"value\":\"教\"},{\"firstRow\":2,\"lastRow\":2,\"lastCol\":3,\"firstCol\":3,\"value\":\"胡\"},{\"firstRow\":3,\"lastRow\":3,\"lastCol\":3,\"firstCol\":3,\"value\":\"马\"},{\"firstRow\":4,\"lastRow\":4,\"lastCol\":3,\"firstCol\":3,\"value\":\"度\"},{\"firstRow\":5,\"lastRow\":5,\"lastCol\":3,\"firstCol\":3,\"value\":\"阴\"},{\"firstRow\":6,\"lastRow\":6,\"lastCol\":3,\"firstCol\":3,\"value\":\"山\"}]");writeMergeExcel("E:\\","mergeExcel", "xls" ,list);} catch (IOException e) {e.printStackTrace();}}
}

2.2 输出的Excel结果:

秦时明月汉时关
但使龙城飞将在

附录:

合并单元格的读取见另一篇文章:

Java读取Excel中的合并单元格https://blog.csdn.net/u012998680/article/details/124557925

Java:Excel写入“合并单元格“相关推荐

  1. JAVA EXCEL导出合并单元格自定义封装方法

    转载在C站上面看到一个大神写的自定义封装方法 转载地址:springboot 使用Poi 自定义封装方法 合并excel中的单元格_Tongyao-CSDN博客_springboot合并单元格 之前都 ...

  2. Java 利用hutool工具实现导出excel并合并单元格

    Java 利用hutool工具实现导出excel并合并单元格 controller层调用service,就一个核心方法,没错就下面这个代码就能实现了.前提是项目里面要引用hutool包.把我这个复制到 ...

  3. Springboot导出excel,合并单元格示例

    原文链接:Springboot导出excel,合并单元格示例 更多文章,欢迎访问:Java知音,一个专注于技术分享的网站 以下用一个示例来说明springboot如何导出数据到excel. 首先引入M ...

  4. 玩电脑的岂能不知道excel怎么合并单元格?

    excel怎么合并单元格?单元格太小导致输入的内容只能看见一部分,这种情况下需要将两列合并起来,空出更多输入内容的空间,以此达到美观又详细的视觉效果,下面来看看如何操作吧! 方法一 第1步:打开一个e ...

  5. POI进行Excel的合并单元格数据处理

    POI进行Excel的合并单元格数据处理 近日接到一个要处理合并Excel单元格的上料表的需求,就到网上找了一些模板,发现有的技术大牛还是挺厉害的,对他们致以敬意. 合并单元格工具类 在这个类中将传入 ...

  6. html表格里面怎么合并单元格的快捷键,合并单元格快捷键:Excel怎么合并单元格...

    今天来聊聊一篇关于合并单元格快捷键:Excel怎么合并单元格的文章,现在就为大家来简单介绍下合并单元格快捷键:Excel怎么合并单元格,希望对各位小伙伴们有所帮助. 方法如下: 1.首先我们需要将&q ...

  7. 【excel】合并单元格拆分后每一格都填充为与原来相同内容

    用的比较频繁的一个操作,单元格合并拆分出的单元格自动填充为合并前内容 比较烦合并单元格,记得以前看过一本excel书里把合并单元格列为一大恶习,因为这严重影响数据分析,但那本书没说怎么处理,导致我才学 ...

  8. 【VB6.0 数据库连接EXCEL查询合并单元格成功解决办法】

    [VB6.0 数据库连接EXCEL查询合并单元格成功解决办法] Private Sub Command1_Click() On Error Resume Next Dim i As Integer, ...

  9. Excel 中合并单元格的快捷键(ALT+H+M+M)

    要在 excel 中合并单元格,首先选择要合并到一个单元格中的单元格,然后可以使用从ALT开始的快捷键,然后同时按H+M+M. 一旦我们使用快捷键执行该函数,它将弹出一条警告消息"合并单元格 ...

最新文章

  1. 如何促使团队紧密协作
  2. iscroll动态加载数据完美解决方案
  3. properties文件不能输入中文
  4. Py之neurolab:Python库之neurolab的简介、安装、使用方法之详细攻略
  5. v8 编译 linux,安装与编译 Javascript V8 Engine
  6. Apollo进阶课程 ① | 带你纵览无人车
  7. 深入Atlas系列:综合示例(1) - 调用服务器端方法时直接获得客户端具体类型...
  8. linux安装nfs服务器
  9. Chap6:风险与监督[《区块链中文词典》维京甲子]
  10. javafx 通过 css 去掉 tableView 滑动条
  11. python链表操作_python操作链表的示例代码
  12. 图层蒙版和图层剪贴路径_PS蒙版使用教程、快速蒙版、剪切蒙版、矢量蒙版、图层蒙版要点...
  13. 【阿里云镜像】配置阿里巴巴开源镜像站镜像——Epel镜像
  14. 视频压缩 I P B 帧 详解
  15. iso14443_TypeB TR0/TR1/TR2 时序观测
  16. 杭州拱墅区委副书记、区长冯晶一行莅临利尔达参观调研
  17. fuel8 生成bootstrap映像
  18. 开源贴片机OpenPnp使用体验
  19. IPO(INPUT PROCESS OUTPUT)图
  20. 专访|LinkedIn资深数据科学家黄申:人工智能不会一夜之间改变人们的生活

热门文章

  1. Hat trick [又名帽子戏法]
  2. Excel拆分同一单元格的两行内容为两行(备忘记录)
  3. 挑战程序设计竞赛(算法和数据结构)——19.2九宫格拼图问题的JAVA实现
  4. 请编写程序,读入CSV文件中数据,循环获得用户输入,直至用户直接输入“Q”退出。根据用户输入的星座名称,输出此星座的出生日期范围及对应字符形式。如果输入的名称有误,请输出“输入星座名称有误”
  5. java无法解析zip
  6. 《上古天真论》第七讲文字版
  7. mediautil.jar java操作jpg信息,添加水印
  8. 《UNIX编程艺术》精彩语录
  9. 将三维模型(obj)导出js格式供threeJS中调用
  10. 公司网站制作需要多少钱?