涉及样式设置,单元格合并问题
如下Excel:
实现代码:
//操作Excel 做笔记
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个Excel表单,参数为sheet的名字
String sheetName = "交通运输企业节能减排统计报表";
XSSFSheet sheet = workbook.createSheet(sheetName);
XSSFRow row = sheet.createRow(0);
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
// sheet.setDefaultColumnWidth(40*256);
sheet.setDefaultRowHeight((short) (2 * 256)); //设置默认行高,表示2个字符的高度
sheet.setDefaultColumnWidth(17); //设置默认列宽,实际上回多出2个字符,不知道为什么
row.setHeight((short) 1000);
//设置为居中加粗
XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setBold(true);
cellStyle.setFont(font);
cellStyle.setAlignment(HorizontalAlignment.CENTER); // 横向居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 横向居中
for (int i=0; i < 7; i++) {
XSSFCell cell;
cell = row.createCell(i);
if(i==0) {
cell.setCellValue(sheetName);
}
cell.setCellStyle(cellStyle);
}
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 6);
sheet.addMergedRegion(region);
XSSFRow row1 = sheet.createRow(1);
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
row1.setHeight((short) 800);
for (int i=0; i < 7; i++) {
XSSFCell cell;
cell = row1.createCell(i);
if(i==0) {
cell.setCellValue("(第"+"三"+"季度)");
}
cell.setCellStyle(cellStyle);
}
CellRangeAddress region1 = new CellRangeAddress(1, 1, 0, 6);
sheet.addMergedRegion(region1);
XSSFRow row2 = sheet.createRow(2);
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
row2.setHeight((short) 600);
XSSFCellStyle cellStyle1 = workbook.createCellStyle();
XSSFFont font1 = workbook.createFont();
font1.setBold(false);
cellStyle1.setFont(font1);
cellStyle1.setAlignment(HorizontalAlignment.LEFT); // 横向居中
for (int i=0; i < 7; i++) {
XSSFCell cell;
cell = row2.createCell(i);
if(i==0) {
cell.setCellValue("填报单位(章):");
}
cell.setCellStyle(cellStyle1);
}
String columnNames[]= {
"公司名称",
"法人代表或负责人",
"2020年第三季度年能源消耗量(单位:吨标准煤)",
"较2019年第三季度年节能量(单位:吨标准煤)",
"填报人",
"联系电话",
"填报时间"
};
XSSFRow row3 = sheet.createRow(3);
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
row3.setHeight((short) 1000);
cellStyle1.setAlignment(HorizontalAlignment.CENTER); // 横向居中
cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER); // 横向居中
cellStyle1.setWrapText(true);// 设置自动换行
cellStyle1.setBorderBottom(BorderStyle.THIN);
cellStyle1.setBorderTop(BorderStyle.THIN);
cellStyle1.setBorderLeft(BorderStyle.THIN);
cellStyle1.setBorderRight(BorderStyle.THIN);
for (int i=0; i < 7; i++) {
XSSFCell cell;
cell = row3.createCell(i);
cell.setCellValue(columnNames[i]);
cell.setCellStyle(cellStyle1);
}
XSSFRow row4 = sheet.createRow(4);
//设置列宽,setColumnWidth的第二个参数要乘以256,这个参数的单位是1/256个字符宽度
row4.setHeight((short) 600);
font1.setBold(false);
cellStyle1.setFont(font1);
cellStyle1.setAlignment(HorizontalAlignment.LEFT); // 横向居中
for (int i=0; i < 7; i++) {
XSSFCell cell;
cell = row4.createCell(i);
if(i==0) {
cell.setCellValue("备注:公交、出租企业、客运企业、水运企业及10辆(含)以上营运车辆规模的货企填报");
}
cell.setCellStyle(cellStyle1);
}
CellRangeAddress region2 = new CellRangeAddress(4, 4, 0, 6);
sheet.addMergedRegion(region2);
File file = new File("E:\\demo.xlsx");
FileOutputStream fout = new FileOutputStream(file);
workbook.write(fout);
fout.close();
}