poi excel 合并单元格
结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip,
colId, colId + cellSkip); // 起始行, 终止行, 起始列, 终止列 // 终止行,
sheet.addMergedRegion(cra);
1.创建workbeet
public static HSSFWorkbook getHSSFWorkbook(String sheetName, JsonArray title,
String[][] values, HSSFWorkbook wb) { // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if (wb == null) {
wb = new HSSFWorkbook();
} // -个sheet限制存65536条,此处仅存60000
System.out.println(values.length);
final int sheetNum = (int) Math.ceil((float) values.length / 60000);
for (int n = 1; n <= sheetNum; n++) { // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
final HSSFSheet sheet = wb.createSheet(sheetName + "_" + n);
System.out.println("sheetName" + sheetName + "_" + n);
sheet.setDefaultColumnWidth(12); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row; // 第四步,创建单元格,并设置值表头样式
final HSSFCellStyle headerStyle = wb.createCellStyle();
headerStyle.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中
headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直居中
final Font fontStyle = wb.createFont(); // 字体样式
fontStyle.setBoldweight(Font.BOLDWEIGHT_BOLD); // 加粗
fontStyle.setFontName("黑体"); // 字体
fontStyle.setFontHeightInPoints((short) 11); // 大小
// 将字体样式添加到单元格样式中
headerStyle.setFont(fontStyle);
// 边框
headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
headerStyle.setBorderRight(CellStyle.BORDER_THIN);
headerStyle.setBorderTop(CellStyle.BORDER_THIN); // 普通单元格样式,边框,水平居中
final HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
// cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
final HSSFDataFormat df = wb.createDataFormat(); // 此处设置数据格式
cellStyle.setDataFormat(df.getFormat("#,#0.0")); // 小数点后保留两位,可以写contentStyle.setDataFormat(df.getFormat("#,#0.00"));
// 声明列对象
HSSFCell cell = null; // 创建标题
JsonArray cellArray = new JsonArray();
JsonObject object = new JsonObject();
JsonObject temobj = new JsonObject();
int rowSkip = 0, cellSkip = 0; String tempCell = "";
for (int rowId = 0; rowId < title.size(); rowId++) {
row = sheet.createRow(rowId);
object = title.get(rowId).getAsJsonObject();
cellArray = object.get("row").getAsJsonArray(); // colId为excel列索引,cellId为行标题值的数组索引,cellId遇到当前单元格已使用时,填充至下一个可使用的单元格
for (int colId = 0, cellId = 0; cellId < cellArray.size(); colId++) {
cell = row.createCell(colId);
cell.setCellStyle(headerStyle); if (isMergedRegion(sheet, rowId, colId)) {
continue;
}
temobj = cellArray.get(cellId).getAsJsonObject();
tempCell = temobj.get("cellvalue").toString().replace("\"", "");
try {
// System.out.println(tempCell);
tempCell = new String(tempCell.getBytes("UTF-8"), "ISO-8859-1");
// System.out.println(tempCell);
tempCell = new String(tempCell.getBytes("ISO-8859-1"), "UTF-8");
// System.out.println(tempCell);
} catch (final Exception e) {
e.printStackTrace();
}
cell.setCellValue(tempCell);
// System.out.println(cell);
// System.out.println(cell.getStringCellValue());
cell.setCellStyle(headerStyle);
cellId++; // 合并单元格
rowSkip = temobj.get("rowspan").getAsInt();
cellSkip = temobj.get("colspan").getAsInt();
// System.out.println(rowSkip + "=skip==" + cellSkip);
if (rowSkip > 1 && rowSkip-- > 0 || cellSkip > 1 && cellSkip-- > 0) {// 用于起始行列计算时需减1
final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip,
colId, colId + cellSkip); // 起始行, 终止行, 起始列, 终止列 // 终止行,
sheet.addMergedRegion(cra);
}
}
}
// 创建内容 -个sheet只能存65536条
for (int i = 0; i < 60000 && i < values.length - (n - 1) * 60000; i++) {
row = sheet.createRow(title.size() + i);
for (int j = 0; j < values[i].length; j++) {
// 将内容按顺序赋给对应的列对象
cell = row.createCell(j);
cell.setCellValue(values[(n - 1) * 60000 + i][j]);
cell.setCellStyle(cellStyle); }
}
}
return wb;
}
2.isMergedRegion方法
public static boolean isMergedRegion(HSSFSheet sheet, int row, int column) {
final int sheetMergeCount = sheet.getNumMergedRegions();
for (int i = 0; i < sheetMergeCount; i++) {
final CellRangeAddress range = sheet.getMergedRegion(i);
final int firstColumn = range.getFirstColumn();
final int lastColumn = range.getLastColumn();
final int firstRow = range.getFirstRow();
final int lastRow = range.getLastRow();
if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
return true;
}
}
}
return false;
}
3.title数据结构
"row":
[{"cellvalue":"","rowspan":0,"colspan":0},
{"cellvalue":"","rowspan":0,"colspan":0},
{"cellvalue":"us","rowspan":0,"colspan":"2"},
{"cellvalue":"uk","rowspan":0,"colspan":"2"},
{"cellvalue":"ch","rowspan":0,"colspan":"2"},
{"cellvalue":"ck","rowspan":0,"colspan":"2"},
{"cellvalue":"au","rowspan":0,"colspan":"2"},
{"cellvalue":"ja","rowspan":0,"colspan":"2"},
{"cellvalue":"cn","rowspan":0,"colspan":"2"},
{"cellvalue":"","rowspan":0,"colspan":0},
{"cellvalue":"","rowspan":0,"colspan":0}]
},{
"row":
[{"cellvalue":"group","rowspan":0,"colspan":0},
{"cellvalue":"name","rowspan":0,"colspan":0},
{"cellvalue":"合计汇总","rowspan":0,"colspan":0},
{"cellvalue":"Count","rowspan":0,"colspan":0},
{"cellvalue":"合计汇总","rowspan":0,"colspan":0},
{"cellvalue":"Count","rowspan":0,"colspan":0},
{"cellvalue":"合计汇总","rowspan":0,"colspan":0},
{"cellvalue":"Count","rowspan":0,"colspan":0},
{"cellvalue":"合计汇总","rowspan":0,"colspan":0},
{"cellvalue":"Count","rowspan":0,"colspan":0},
{"cellvalue":"合计汇总","rowspan":0,"colspan":0}]
}]
poi excel 合并单元格的更多相关文章
- poi读取合并单元格
poi读取合并单元格 学习了:http://blog.csdn.net/ycb1689/article/details/9764191 进行了列合并单元格的修正:原来是我自己找错了地方: import ...
- asp.net C#取Excel 合并单元格内容
asp教程.net c#取excel 合并单元格内容读取excel数据,填充dataset// 连接字符串 string xlspath = server.mappath("~/www.11 ...
- Java导出Excel表,POI 实现合并单元格以及列自适应宽度(转载)
POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress ...
- java poi导出Excel合并单元格并设置边框
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; i ...
- poi导出excel合并单元格(包括列合并、行合并)
1 工程所需jar包如下:commons-codec-1.5.jarcommons-logging-1.1.jarlog4j-1.2.13.jarjunit-3.8.1.jarpoi-3.9-2012 ...
- poi获取合并单元格内的第一行第一列的值
当读取如图所示的excel时,显示为第1行 第1列 的内容是:合并单元格 其它在合并单元格区域内的单元格不显示 示例代码如下: import java.io.FileInputStream; impo ...
- NPOI之Excel——合并单元格、设置样式、输入公式、设置筛选等
首先建立一个空白的工作簿用作测试,并在其中建立空白工作表,在表中建立空白行,在行中建立单元格,并填入内容: //建立空白工作簿 IWorkbook workbook = new HSSFWorkboo ...
- 创建excel,合并单元格,设置单元格样式
package com.huawei.excel; import java.io.File;import java.io.FileOutputStream;import java.util.Date; ...
- POI 实现合并单元格以及列自适应宽度
POI是apache提供的一个读写Excel文档的开源组件,在操作excel时常要合并单元格,合并单元格的方法是: sheet.addMergedRegion(new CellRangeAddress ...
随机推荐
- PHP 汉字数字互转(100以内)| 汉字转数字 | 数字转汉字
<?php function numDatabase(){ $numarr =array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,2 ...
- C/C++基础--模板与泛型编程
模板参数 函数模板,编译器根据实参来为我们推断模板实参. 模板中可以定义非类型参数,表示一个值而非一个类型,这些值必须是常量表达式,从而允许编译器在编译时实例化模板. 非类型参数可以是整型,或者一个指 ...
- (转载)通向架构师的道路(第四天)之Tomcat性能调优-让小猫飞奔
转载自:https://blog.csdn.net/lifetragedy/article/details/7708724 参考文章:tomcat以及常用web容器线程池的实现原理https://bl ...
- Docker启动一个Centos镜像
docker镜像的获取与使用 docker中使用centos7镜像 接着上文,我们下载完成一个Centos镜像之后,开始启动 #运行命令 docker run -d -i -t <imageID ...
- MySQL数据库InnoDB存储引擎中的锁机制(转载)
http://www.uml.org.cn/sjjm/201205302.asp 00 – 基本概念 当并发事务同时访问一个资源的时候,有可能导致数据不一致.因此需要一种致机制来将访问顺序化. 锁就是 ...
- Jmeter(十八)Logic Controllers 之 Random Controller and Random order Controller
Random Controller就比较简单了,完全随机!毫无章法. 毫无任何规律的运行. 还有一个Random order Controller,随机顺序控制器就像一个简单的控制器,它将最多执行一次 ...
- [UE4]移动惯性
2个因素影响滑行: 1.摩擦力:Ground Frition 2.减速度:Braking decelearation Walking
- [UE4]不推荐的UI更新方式
在创建UI的时候,把UI保存到一个变量,直接访问其中的控件. 这种方法会增加耦合,不推荐,应当尽量避免使用这种方式.
- 使用CacheCloud管理Redis实例
转载来源:http://www.ywnds.com/?p=10610 一.CacheCloud是什么? 最近在使用CacheCloud管理Redis,所以简单说一下,这里主要说一下我碰到的问题.Cac ...
- Java基础知识_毕向东_Java基础视频教程笔记(19-21 IO流)
18天-06-IO流 字节流和字符流 字节流两个基类:InputStream,FileInputStream,BufferedInputStream OutputStream,FileOutputSt ...