POI-用于不同ID的Excel分隔列

时间:2018-11-13 08:30:07

标签: java apache-poi

我正在尝试从mysql获取数据并将其写入Excel,并具有如下所示的某些条件,

  List<DataClass> getdata = reports.getReportData(); //This will return data from jdbcTemplate

getdata包含如下数据,

    deviceId    value  date
    T01         59        2-sep
    T01         67        3-sep
    T01         78        4-sep
    T01         79        5-sep
    T02         68        2-sep
    T02         69        3-sep
    T02         75        4-sep
    T03         70        2-sep
    T03         78        3-sep
    T03         80        4-sep
    T03         89        5-sep

class DataPosition {
    public Integer colNum;
    public Integer rowNum;

    public DataPosition(Integer colNum,Integer rowNum) {
        this.colNum = colNum;
        this.rowNum = rowNum;
    }
}

Map<String, DataPosition> excelIds = new HashMap<>();
DataPosition position;
// Start at -2 so when it comes to the first value, you add 2 to place the column number to 0
Integer currentColNum = -2;
Integer currentRowNum;
HSSFRow row;
for (DataClass e: getdata) {  
    position = excelIds.get(e.getDeviceId());
    if (position != null) {
        // Add a new row
        position.rowNum++;
        currentRowNum = position.rowNum;
    } else {
        // Add a new column (increments by two because you need two columns for the representation)
        currentColNum += 2;
        currentRowNum = 0;
        excelIds.put(e.getDeviceId(), new DataPosition(currentColNum, currentRowNum));
    }

    row = sheet.getRow(currentRowNum);
    if (row == null) {
        row = sheet.createRow(currentRowNum);
    }

    row.createCell(currentColNum).setCellValue(e.getValue());
    row.createCell(currentColNum + 1).setCellValue(e.getDate());
}

但是它的写法如下,

enter image description here

但是我想显示如下,

enter image description here

如何在此处为每个设备的标题打印deviceId和Date?

更新的代码

Map<String, DataPosition> excelIds = new HashMap<>();
DataPosition position;
// Start at -2 so when it comes to the first value, you add 2 to place the column number to 0
Integer currentColNum = -2;
Integer currentRowNum;
HSSFRow row;
for (DataClass e: getdata) {  
    position = excelIds.get(e.getDeviceId());
    if (position != null) {
        // Add a new row
        position.rowNum++;
        currentRowNum = position.rowNum;
    } else {
        // Add a new column (increments by two because you need two columns for the representation)
        currentColNum += 2;
        currentRowNum = 0;
        excelIds.put(e.getDeviceId(), new DataPosition(currentColNum, currentRowNum));
newValueFlag = true;
    }
    row = sheet.getRow(currentRowNum);
    if (row == null) {
        row = sheet.createRow(currentRowNum);
    }
if (newValueFlag) {
     Cell newCell = row.createCell(currentColNum);
     newCell.setCellValue(e.getDeviceId());
     newCell.setCellStyle(style);
     Cell newCell2 = row.createCell(currentColNum + 1);
     newCell2.setCellValue("Date");
     newCell2.setCellStyle(style);
     row = sheet.createRow(currentRowNum + 1);
    row.createCell(currentColNum).setCellValue(e.getValue());
    row.createCell(currentColNum + 1).setCellValue(e.getDate);
    newValueFlag = false;
    excelIds.put(e.getDeviceId(), new 
    DataPosition(currentColNum, currentRowNum + 1));
}
else{

    row.createCell(currentColNum).setCellValue(e.getValue());
    row.createCell(currentColNum + 1).setCellValue(e.getDate());
}
}

如果我尝试上述更改,那么我将丢失每个deviceId值的第一行。

1 个答案:

答案 0 :(得分:1)

在创建行之前,将currentRowNum增加1并在其中添加详细信息。然后通过

创建静态行
Row heading= sheet.createRow(0);

并在其中设置deviceIds和Date。

您需要在顶部放置一行,因此在循环之后,您将获得完整的位置和设备ID映射,然后可以在顶部的第0个位置创建静态行,并从该映射中添加这些设备ID使用循环。