Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Excel数据使用poi仅使用一种数据格式进行格式化_Java_Excel_Apache Poi - Fatal编程技术网

Java Excel数据使用poi仅使用一种数据格式进行格式化

Java Excel数据使用poi仅使用一种数据格式进行格式化,java,excel,apache-poi,Java,Excel,Apache Poi,我想在excel中使用poi和CUURENCE格式化程序编写一些单元格,还有一些使用数字格式化程序。整个文档似乎使用了我在constructData方法中传递给它的格式,即最后一个单元格。这是为什么?我该如何修复它?我相信代码是不言自明的,但如果您需要帮助,请随时询问:) private static final int TITLE\u COLUMN\u WIDTH=8500; 私有静态最终整数值\列\宽度=6000; 专用静态最终浮动值\u行\u高\u英寸\u点=26.25f; 专用静态最终浮

我想在excel中使用poi和CUURENCE格式化程序编写一些单元格,还有一些使用数字格式化程序。整个文档似乎使用了我在
constructData
方法中传递给它的格式,即最后一个单元格。这是为什么?我该如何修复它?我相信代码是不言自明的,但如果您需要帮助,请随时询问:)

private static final int TITLE\u COLUMN\u WIDTH=8500;
私有静态最终整数值\列\宽度=6000;
专用静态最终浮动值\u行\u高\u英寸\u点=26.25f;
专用静态最终浮动收割台排高英寸点=21.5f;
私有静态最终字符串CURRENCY_DATA_FORMAT=“#,###0.00\u20AC”;
私有静态最终字符串编号_DATA_FORMAT=“#,##0”;
私有最终字符串NO\u STYLE\u CURRENCY=“NO\u STYLE”;
私有最终字符串NO\u STYLE\u NUMBER=“NO\u STYLE\u NUMBER”;
私人最终字符串浅灰色=“浅灰色”;
私有最终字符串头\u BLUE=“头\u BLUE”;
映射样式=新HashMap();
//在这个方法中,我从buildHeader调用的其他样式似乎不会引起问题
专用void populateStyles(地图样式、HSSF工作簿){
HSSFCellStyle noStyleWithCurrency=工作簿.createCellStyle();
noStyleWithCurrency.setDataFormat(工作簿.createDataFormat().getFormat(货币数据格式));
样式。put(无样式货币,noStyleWithCurrency);
HSSFCellStyle noStyleNumber=工作簿.createCellStyle();
noStyleNumber.setDataFormat(工作簿.createDataFormat().getFormat(数字数据格式));
styles.put(无样式编号,鼻腔编号);}
私有void ConstructData(HSSF工作簿、HSSF表、产品名称列表、SalesGridModelBean模型){
Map productTypeNames=model.getProductTypeNames();
Map gridRows=model.getGridRows();
Long totalProductId=model.getTotalProductTypeId();
List sortedGridRows=sortAndMoveToList(网格,model.getTotalProductTypeId());
int rowCount=2;
对于(SalesGridRowBean行:sortedGridRows){
HSSFRow dataRow=sheet.createRow(行计数);
dataRow.setHeightInPoints(值为行、高度为点);
int-cellCount=0;
HSSFCell nameCell=dataRow.createCell(cellCount);
setCellValue(row.getName());
if(productTypeNames.size()==1){
SalesBean SalesBean=row.getSalesBeanForProductType(totalProductId);
HSSFCell revenueCell=dataRow.createCell(++cellCount);
revenueCell.getCellStyle().cloneStyleFrom((STYLE.get(NO_STYLE_CURRENCY));
setCellValue(salesBean.getRevenue());
HSSFCell quantityCell=dataRow.createCell(++cellCount);
quantityCell.getCellStyle().cloneStyleFrom((STYLE.get(NO_STYLE_NUMBER));
setCellValue(salesBean.getSales());
HSSFCell averageCell=dataRow.createCell(++cellCount);
averageCell.getCellStyle().cloneStyleFrom((STYLE.get(NO_STYLE_CURRENCY));
setCellValue(salesBean.getAveragePrice());
}否则{
SalesBean SalesBean=row.getSalesBeanForProductType(totalProductId);
HSSFCell revenueCell=dataRow.createCell(++cellCount);
revenueCell.getCellStyle().cloneStyleFrom((STYLE.get(NO_STYLE_CURRENCY));
setCellValue(salesBean.getRevenue());
HSSFCell quantityCell=dataRow.createCell(++cellCount);
quantityCell.getCellStyle().cloneStyleFrom((STYLE.get(NO_STYLE_NUMBER));
setCellValue(salesBean.getSales());
对于(int j=0;j

PS:我使用HEADER_BLUE样式构造文件的头(前2行),它没有给出错误。我用另一种方法来做这件事。

@AlanHay谢谢,现在可以了:)另外,最好是减少代码片段,删除问题不需要的所有内容,只列出重现问题所需的最小值。这将使其他人更容易阅读和理解问题。
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;
    }       
}