package DEMO;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class ExcelDemo2 {
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Data Sheet");
Row row1 = sheet.createRow(0);
Cell cell11 = row1.createCell(0);
cell11.setCellValue("填报单位");
sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 0));
Cell cell12 = row1.createCell(1);
cell12.setCellValue("住院分娩活产数");
sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 9));
Row row2 = sheet.createRow(1);
Cell cell21 = row2.createCell(1);
cell21.setCellValue("分性别活产数");
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 4));
Cell cell22 = row2.createCell(5);
cell22.setCellValue("分孕周活产数");
sheet.addMergedRegion(new CellRangeAddress(1, 1, 5, 8));
Cell cell23 = row2.createCell(9);
cell23.setCellValue("<28周存货儿数");
sheet.addMergedRegion(new CellRangeAddress(1, 2, 9, 9));
Row row3 = sheet.createRow(2);
Cell cell31 = row3.createCell(1);
cell31.setCellValue("男");
Cell cell32 = row3.createCell(2);
cell32.setCellValue("女");
Cell cell33 = row3.createCell(3);
cell33.setCellValue("性别不明");
Cell cell34 = row3.createCell(4);
cell34.setCellValue("合计");
Cell cell35 = row3.createCell(5);
cell35.setCellValue("≥37周");
Cell cell36 = row3.createCell(6);
cell36.setCellValue("28~36周");
Cell cell37 = row3.createCell(7);
cell37.setCellValue("合计");
Cell cell38 = row3.createCell(8);
cell38.setCellValue("早产率%");
CellStyle style = wb.createCellStyle();
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
int columSum = 10;
int rowCount = 100;
for (int i = 3; i < rowCount + 3; i++) {
Row row = sheet.createRow(i);
for (int j = 0; j < columSum; j++) {
Cell c = row.createCell(j);
c.setCellValue(i+"---"+j);
}
}
for (Row row : sheet) {
for (Cell cell : row) {
if (cell == null || cell.getCellType() == CellType.BLANK) {
continue;
}
cell.setCellStyle(style);
}
}
List<CellRangeAddress> mergedCellsPosition = sheet.getMergedRegions();
for (CellRangeAddress cellRangeAddress : mergedCellsPosition) {
RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet);
RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet);
}
try (FileOutputStream fileOut = new FileOutputStream("c9.xlsx")) {
wb.write(fileOut);
}
wb.close();
}
}
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.1.0</version>
</dependency>