POI生成excel表格,如何合并单元格

news/2024/7/21 6:12:09 标签: Excel, Apache
Java代码
import java.io.FileOutputStream;
import java.io.IOException;

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.hssf.util.Region;

public class ExcelTest {

/**
* @param args
*/
public static void main(String[] args) throws IOException {

try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 样式对象

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);

sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 中文处理
ce.setCellValue("项目\\日期"); // 表格的第一行第一列显示的数据
ce.setCellStyle(style); // 样式,居中
int num = 0;
for (int i = 0; i < 9; i++) { // 循环9次,每一次都要跨单元格显示
// 计算从那个单元格跨到那一格
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 单元格合并
// 四个参数分别是:起始行,起始列,结束行,结束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨单元格显示的数据
cell.setCellStyle(style); // 样式
// 不跨单元格显示的数据,如:分两行,上一行分别两格为一格,下一行就为两格,“数量”,“金额”
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
num++;
}

// 在后面加上合计百分比

// 合计 在最后加上,还要跨一个单元格
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("合计");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金额");
cell2.setCellStyle(style);

// 百分比 同上
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb.setCellValue("百分比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb1.setCellValue("数量");
cellb1.setCellStyle(style);
cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("金额");
cellb2.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}

}

}

http://www.niftyadmin.cn/n/595247.html

相关文章

@RequestParam什么时候可以省略

1 <form action"${pageContext.request.contextPath}/user/param" method"get">2 用户名&#xff1a;<input type"text" name"name">3 <input type"submit" value"param">4 &l…

url中有空格等特殊字符及中文字符处理

在做爬虫时&#xff0c;爬下来的书籍的URL地址各式各样&#xff0c;什么情况都有。有的url地址既有中文&#xff0c;又有%20 空格等特殊字符。如 http://www.ishareread.com/book/2018/不成问题的问题%20-%20老舍.mobi如果是在url中有中文需要转码&#xff0c;但转码后会将%号替…

潘震seo教程:总结seo毒害案例

潘震seo教程&#xff1a;总结seo毒害案例 "SEO"毒害案例一 刚刚潘震seo教程在卢松松博客上面看文章&#xff0c;看到一篇标题为【投递简历与求职技巧】 的文章&#xff0c;文中一哥们写简历&#xff0c;有人用邮件建议他是不是应该将简历的标题重点SEO一下&#xff0…

html文字滚动代码

代码如下<MARQUEE>滚动文字</MARQUEE><marquee style"WIDTH: 388px; HEIGHT: 200px" scrollamount"2" direction"up" > <div align"left" ><br /> </div > <center ><font face"黑…

DataV实践:创建迪士尼乐园实况大屏

我是一个迪士尼乐园的爱好者&#xff0c;我想拥有一块大屏能实时显示迪士尼乐园的情况&#xff0c;以便我觉得今天去不去乐园&#xff0c;去乐园玩什么项目。。。 数据来源 我写了一个抓包的程序&#xff0c;每天获取一次乐园运营数据&#xff08;项目开放时间&#xff0c;状态…

12.1912.20 -基础命令练习二

12.19&12.20基础命令练习二第1章 Linux开机启动过程linux启动过程1.开启开关2.bios开机自检3.mbr引导4.grub菜单 选择内核5.加载内核6.启动init进程 init进程是linux启动的时候运行的第一个进程 7.从/etc/inittab读取运行级别 8.根据/etc/rc.d/rc.sysinit 初始化系统 &…

网卡、集线器、交换机、路由器、调制解调器

网 卡-------计算机和不同设备通信就要用到网卡。现在所有的网络通信都是靠它来实现的。 集线器-------集线器也叫Hub&#xff0c;工作在物理层&#xff08;最底层&#xff09;&#xff0c;没有相匹配的软件系统&#xff0c;是纯硬件设备。 集线器主要用来连接计算机等网络终端…