我想在excel中使用带有cuurency格式化程序的poi和一些带有数字格式化程序的单元格编写一些单元格。似乎整个文档使用我在constructData
方法中传递给它的格式,到最后一个单元格。为什么这样,我该如何解决?我相信代码是自我解释的,但如果你需要帮助可以自由地问:)
private static final int TITLE_COLUMN_WIDTH = 8500;
private static final int VALUES_COLUMN_WIDTH = 6000;
private static final float VALUES_ROW_HEIGHT_IN_POINTS = 26.25f;
private static final float HEADER_ROW_HEIGHT_IN_POINTS = 21.5f;
private static final String CURRENCY_DATA_FORMAT = "#,##0.00 \u20AC";
private static final String NUMBER_DATA_FORMAT = "#,##0";
private final String NO_STYLE_CURRENCY = "NO_STYLE";
private final String NO_STYLE_NUMBER = "NO_STYLE_NUMBER";
private final String LIGHT_GREY= "LIGHT_GREY";
private final String HEADER_BLUE = "HEADER_BLUE";
Map <String, HSSFCellStyle> styles = new HashMap<String, HSSFCellStyle>();
// there are other styles in this method that I call from buildHeader that don't seem to cause problems
private void populateStyles(Map <String, HSSFCellStyle> styles, HSSFWorkbook workbook){
HSSFCellStyle noStyleWithCurrency = workbook.createCellStyle();
noStyleWithCurrency.setDataFormat(workbook.createDataFormat().getFormat(CURRENCY_DATA_FORMAT));
styles.put(NO_STYLE_CURRENCY,noStyleWithCurrency);
HSSFCellStyle noStyleNumber = workbook.createCellStyle();
noStyleNumber.setDataFormat(workbook.createDataFormat().getFormat(NUMBER_DATA_FORMAT));
styles.put(NO_STYLE_NUMBER,noStyleNumber); }
private void ConstructData(HSSFWorkbook workbook,HSSFSheet sheet, List<Long> sortedProductNames, SalesGridModelBean model){
Map<Long, String> productTypeNames = model.getProductTypeNames();
Map<Long, SalesGridRowBean> gridRows = model.getGridRows();
Long totalProductId = model.getTotalProductTypeId();
List<SalesGridRowBean> sortedGridRows = sortAndMoveToList(gridRows, model.getTotalProductTypeId());
int rowCount = 2;
for(SalesGridRowBean row : sortedGridRows){
HSSFRow dataRow = sheet.createRow(rowCount);
dataRow.setHeightInPoints(VALUES_ROW_HEIGHT_IN_POINTS);
int cellCount = 0;
HSSFCell nameCell = dataRow.createCell(cellCount);
nameCell.setCellValue(row.getName());
if(productTypeNames.size() == 1){
SalesBean salesBean = row.getSalesBeanForProductType(totalProductId);
HSSFCell revenueCell = dataRow.createCell(++cellCount);
revenueCell.getCellStyle().cloneStyleFrom((styles.get(NO_STYLE_CURRENCY)));
revenueCell.setCellValue(salesBean.getRevenue());
HSSFCell quantityCell = dataRow.createCell(++cellCount);
quantityCell.getCellStyle().cloneStyleFrom((styles.get(NO_STYLE_NUMBER)));
quantityCell.setCellValue(salesBean.getSales());
HSSFCell averageCell = dataRow.createCell(++cellCount);
averageCell.getCellStyle().cloneStyleFrom((styles.get(NO_STYLE_CURRENCY)));
averageCell.setCellValue(salesBean.getAveragePrice());
} else {
SalesBean salesBean = row.getSalesBeanForProductType(totalProductId);
HSSFCell revenueCell = dataRow.createCell(++cellCount);
revenueCell.getCellStyle().cloneStyleFrom((styles.get(NO_STYLE_CURRENCY)));
revenueCell.setCellValue(salesBean.getRevenue());
HSSFCell quantityCell = dataRow.createCell(++cellCount);
quantityCell.getCellStyle().cloneStyleFrom((styles.get(NO_STYLE_NUMBER)));
quantityCell.setCellValue(salesBean.getSales());
for(int j = 0; j < sortedProductNames.size(); j++){
salesBean = row.getSalesBeanForProductType(sortedProductNames.get(j));
HSSFCell revenueCell2 = dataRow.createCell(++cellCount);
revenueCell2.getCellStyle().cloneStyleFrom((styles.get(NO_STYLE_CURRENCY)));
revenueCell2.setCellValue(salesBean.getRevenue());
HSSFCell quantityCell2 = dataRow.createCell(++cellCount);
quantityCell2.getCellStyle().cloneStyleFrom((styles.get(NO_STYLE_NUMBER)));
quantityCell2.setCellValue(salesBean.getSales());
HSSFCell averageCell = dataRow.createCell(++cellCount);
// This seems to cause the problem. The last assignment !!!
//If replaced with NO_STYLE_NUMBER uses that one
averageCell.getCellStyle().cloneStyleFrom((styles.get(NO_STYLE_CURRENCY)));
averageCell.setCellValue(salesBean.getAveragePrice());
}
}
rowCount++;
cellCount = 0;
}
}
PS:我使用HEADER_BLUE样式构造文件的标题(前2行)并且没有错误。我是用另一种方法做的。