ApachePOI创建复杂表格

ApachePOI教程:https://www.yiibai.com/apache_poi/
1.先导入ApachePOI需要的jar包
第一张
第二张
第三张
2.简单测试类
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.;
public class ApaPOIExcelTest {
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();//创建一个工作簿
XSSFSheet sheet = workbook.createSheet(“xxx”);//创建一个表格
Font font=workbook.createFont();//设置字体
font.setFontName(“宋体”);
((XSSFFont) font).setBold(true);
XSSFCellStyle style = workbook.createCellStyle();//创建样式
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);//设置样式水平居中
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//设置垂直居中
style.setFont(font);
XSSFRow row1=sheet.createRow((short)0);//创建第一行
row1.setHeight((short)500);//设置行高
XSSFCell cell1 = (XSSFCell) row1.createCell((short) 0);//创建第一个单元格
cell1.setCellValue(“小星星”);//设置单元格值
sheet.addMergedRegion(new CellRangeAddress(
0, //first row (0-based)
0, //last row (0-based)
0, //first column (0-based)
8 //last column (0-based)
));//合并单元格
cell1.setCellStyle(style);
XSSFRow row2=sheet.createRow((short)1);//创建第二行
XSSFCell cell2_One = (XSSFCell) row2.createCell((short)0);//第2行1单元格
cell2_One.setCellValue(“乡镇(街道)”);//单元格值
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
2, //last row (0-based)
0, //first column (0-based)
0 //last column (0-based)
));//合并单元格
sheet.setColumnWidth(0,256
15+184);//设置列宽公式256*宽+184,得到值15.09
cell2_One.setCellStyle(style);
XSSFCell cell2_two = (XSSFCell) row2.createCell((short)1);//第二单元格
cell2_two.setCellValue(“总数”);//单元格值
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
2, //last row (0-based)
1, //first column (0-based)
1 //last column (0-based)
));//合并单元格
cell2_two.setCellStyle(style);
XSSFCell cell2_three = (XSSFCell) row2.createCell((short) 2);//第3单元格
cell2_three.setCellValue(“已采集”);//单元格值
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
1, //last row (0-based)
2, //first column (0-based)
4 //last column (0-based)
));//合并单元格
cell2_three.setCellStyle(style);
XSSFCell cell2_five = (XSSFCell) row2.createCell((short) 5);//第6单元格
cell2_five.setCellValue(“未采集”);
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
1, //last row (0-based)
5, //first column (0-based)
7 //last column (0-based)
));//合并单元格
cell2_five.setCellStyle(style);
XSSFRow row3=sheet.createRow((short)2);//创建第三行
XSSFCell cell3_three = (XSSFCell) row3.createCell((short)2);//第3单元格
cell3_three.setCellValue(“企业职工”);//单元格值
cell3_three.setCellStyle(style);
XSSFCell cell3_four = (XSSFCell) row3.createCell((short)3);//第4单元格
cell3_four.setCellValue(“机关事业”);//单元格值
cell3_four.setCellStyle(style);
XSSFCell cell3_five = (XSSFCell) row3.createCell((short)4);//第5单元格
cell3_five.setCellValue(“城镇居民”);//单元格值
cell3_five.setCellStyle(style);
XSSFCell cell3_six = (XSSFCell) row3.createCell((short)5);//第六单元格
cell3_six.setCellValue(“企业职工”);//单元格值
cell3_six.setCellStyle(style);
XSSFCell cell3_seven = (XSSFCell) row3.createCell((short)6);//第七单元格
cell3_seven.setCellValue(“机关事业”);//单元格值
cell3_seven.setCellStyle(style);
XSSFCell cell3_eight = (XSSFCell) row3.createCell((short)7);//第八单元格
cell3_eight.setCellValue(“城镇居民”);//单元格值
cell3_eight.setCellStyle(style);
FileOutputStream out = new FileOutputStream(
new File(“D:\Idea\photos\xxx.xlsx”));
workbook.write(out);
out.close();
}
}
3.效果如图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43686722/article/details/84329807