我正在编写一些代码,其中有一些行要合并。使用POI,我已经实现了这一目标。但问题是在合并我需要使它们居中的行之后。
以下是我的代码。
public static void main(String[] args) throws IOException {
FileInputStream fin = new FileInputStream(new File("C:\\D\\Sheets\\Sample Sheets\\dummy.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(fin);
HSSFSheet sheet = workbook.getSheetAt(0);
int row = sheet.getPhysicalNumberOfRows();
String currentLawName, currentCountry, currentAssociate, previousLawName, previousCountry, previousAssociate;
String currentPages, previousPages;
int startIndex = 1, finalIndex = 0, tempNum = 0;
System.out.println(row);
for (int i = 2; i < (row); i++) {
currentAssociate = sheet.getRow(i).getCell(0).toString();
currentLawName = sheet.getRow(i).getCell(1).toString();
currentCountry = sheet.getRow(i).getCell(2).toString();
currentPages = sheet.getRow(i).getCell(3).toString();
previousAssociate = sheet.getRow(i - 1).getCell(0).toString();
previousLawName = sheet.getRow(i - 1).getCell(1).toString();
previousCountry = sheet.getRow(i - 1).getCell(2).toString();
previousPages = sheet.getRow(i - 1).getCell(3).toString();
if (currentAssociate.equals(previousAssociate) && currentCountry.equals(previousCountry)
&& currentLawName.equals(previousLawName) && currentPages.equals(previousPages)) {
finalIndex += 1;
if (((i + 1) == row)) {
System.out.println("yes");
finalIndex += 1;
sendRangeToMergeCells(startIndex + 1, finalIndex - 1, sheet);
}
} else {
sendRangeToMergeCells(startIndex + 1, finalIndex, sheet);
startIndex = i;
finalIndex = 0;
}
}
FileOutputStream fileOut = new FileOutputStream("C:\\D\\Sheets\\Sample Sheets\\dummy.xls");
workbook.write(fileOut);
fileOut.close();
}
private static void sendRangeToMergeCells(int startIndex, int finalIndex, HSSFSheet sheet) {
System.out.println("B:" + startIndex + "\tB:" + (finalIndex + startIndex) + "\t else");
CellRangeAddress region = CellRangeAddress.valueOf("D" + (startIndex) + ":D" + ((finalIndex + startIndex)));
sheet.addMergedRegion(region);
}
请告诉我如何将内容垂直居中。
答案 0 :(得分:3)
要向单元格添加对齐(和其他属性),您需要设置CellStyle
。
有很多方法可以做到这一点,但在你的情况下,你已经有了一个包含内容的单元格(也就是合并区域的左上角),所以你可以使用CellUtil.setProperty()
。< / p>
CellUtil.setCellProperty(cell, CellUtil.VERTICAL_ALIGNMENT, CellStyle.VERTICAL_CENTER);
如果您需要一次设置多个属性,则应使用CellUtil.setCellProperties()
Map<String, Object> properties = new HashMap<String, Object>();
// border around a cell
properties.put(CellUtil.BORDER_TOP, CellStyle.BORDER_MEDIUM);
properties.put(CellUtil.BORDER_BOTTOM, CellStyle.BORDER_MEDIUM);
properties.put(CellUtil.BORDER_LEFT, CellStyle.BORDER_MEDIUM);
properties.put(CellUtil.BORDER_RIGHT, CellStyle.BORDER_MEDIUM);
// Give it a color (RED)
properties.put(CellUtil.TOP_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.LEFT_BORDER_COLOR, IndexedColors.RED.getIndex());
properties.put(CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex());
// Apply it to a cell
CellUtil.setCellStyleProperties(cell, properties);
阅读Apache POI快速指南:https://poi.apache.org/spreadsheet/quick-guide.html