我正在使用Apache poi库创建Excel文件。 我的问题是标题文本未与中心对齐。 [问题]
我按照StackOverflow的“合并Excel”问题进行操作,但不适用于此代码。 链接在这里: Merge and align center cell using apache poi
当我刚创建标题时,单元格被合并,文本与中心对齐。但是,当我创建标题,标题(列名)和行时,标题未居中对齐。
public void aaa() throws Exception {
String title = "testTitle";
String head = "A,B,C,D";
String row1 = "1,2,3,4";
String row2 = "5,6,7,8";
ArrayList<String> headerList = new ArrayList<String>(Arrays.asList(head.split(",")));
ArrayList<ArrayList<String>> rowsList = new ArrayList<ArrayList<String>>();
ArrayList<String> rowdata1 = new ArrayList<String>(Arrays.asList(row1.split(",")));
ArrayList<String> rowdata2 = new ArrayList<String>(Arrays.asList(row2.split(",")));
rowsList.add(rowdata1);
rowsList.add(rowdata2);
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("test");
sheet.setDefaultColumnWidth(1300);
Row row = null;
Cell cell = null;
int rowNo = 0;
int colNo = 0;
int maxCol = headerList.size();
// Title Font
Font font = wb.createFont();
font.setFontHeightInPoints((short) 20);
font.setFontName("Arial");
// Header , Rows Style
Font defaultFont = wb.createFont();
defaultFont.setFontHeightInPoints((short) 13);
defaultFont.setFontName("Arial");
// 1-1 Title Style
CellStyle cellTitleStyle = wb.createCellStyle();
cellTitleStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellTitleStyle.setFont(font);
// Header Style
CellStyle cellHeaderStyle = wb.createCellStyle();
cellHeaderStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
cellHeaderStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellHeaderStyle.setAlignment(CellStyle.ALIGN_CENTER);
// Data Style
CellStyle cellDataStyle = wb.createCellStyle();
cellDataStyle.setAlignment(CellStyle.ALIGN_CENTER);
// Merge and combine cells, apply title style to cells.
if (maxCol > 0) {
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, maxCol - 1));
cell = sheet.createRow(0).createCell(0);
cell.setCellValue(title);
cell.setCellStyle(cellTitleStyle);
sheet.createRow(rowNo).createCell(colNo).setCellValue(title);
sheet.addMergedRegion(new CellRangeAddress(rowNo + 1, rowNo + 1, colNo, colNo + maxCol - 1));
}
rowNo++;
rowNo++;
// create a header
row = sheet.createRow(rowNo);
for (int i = 0; i < maxCol; i++) {
cell = row.createCell(i);
cell.setCellValue(headerList.get(i));
cell.setCellStyle(cellHeaderStyle);
}
// create rows
for (int i = 0; i < rowsList.size(); i++) {
rowNo++;
row = sheet.createRow(rowNo);
for (int j = 0; j < rowsList.get(i).size(); j++) {
cell = row.createCell(j);
cell.setCellValue(rowsList.get(i).get(j));
}
}
CellRangeAddress cellRangeAdress = new CellRangeAddress(2, rowNo, 0, maxCol - 1);
final short borderMediumDashed = CellStyle.BORDER_MEDIUM;
RegionUtil.setBorderBottom(borderMediumDashed, cellRangeAdress, sheet, wb);
RegionUtil.setBorderTop(borderMediumDashed, cellRangeAdress, sheet, wb);
RegionUtil.setBorderLeft(borderMediumDashed, cellRangeAdress, sheet, wb);
RegionUtil.setBorderRight(borderMediumDashed, cellRangeAdress, sheet, wb);
FileOutputStream fs = null;
fs = new FileOutputStream("C:\\test/test.xls");
wb.write(fs);
fs.close();
}