HSSFWorkbook-用不同的值的单独列写入Excel数据

时间:2018-10-04 09:47:12

标签: java excel apache-poi

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

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



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

我正在尝试的示例代码,

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Report");
HSSFRow header = sheet.createRow(0);

 int counter = 1;
 for (DataClass e : getdata ) {  
    if(counter != 1)
       if(getdata.get(counter-1).getDeviceId() != (getdata.get(counter).getDeviceId())
                      #here i want to create seperate column for different deviceId values


  HSSFRow row = sheet.createRow(counter++);());
  row.createCell(0).setCellValue(e.getValue());
  row.createCell(1).setCellValue(e.getDate());

   }

我想像下面那样将这些数据写入excel中,

enter image description here

该怎么做?

1 个答案:

答案 0 :(得分:1)

创建一个新类以保存有关数据位置的信息:

<h2>Grid View</h2>
<div class="grid">
  <div class="column" style="background-color:#aaa;">
    <div id="bottom-lab">
      <h2>Column 1</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#bbb;">
    <div id="bottom-lab">
      <h2>Column 2</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#ccc;">
    <div id="bottom-lab">
      <h2>Column 3</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#ddd;">
    <div id="bottom-lab">
      <h2>Column 4</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#ddd;">
    <div id="bottom-lab">
      <h2>Column 5</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#ddd;">
    <div id="bottom-lab">
      <h2>Column 6</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#ddd;">
    <div id="bottom-lab">
      <h2>Column 7</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#ddd;">
    <div id="bottom-lab">
      <h2>Column 8</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#ddd;">
    <div id="bottom-lab">
      <h2>Column 9</h2>
      <p>Some text..</p>
    </div>
  </div>
  <div class="column" style="background-color:#ddd;">
    <div id="bottom-lab">
      <h2>Column 10</h2>
      <p>Some text..</p>
    </div>
  </div>
</div>

在循环数据时,请检查deviceId是否已包含在excel中。如果是这样,则使用与该deviceId对应的最后一列和行号。如果不包括在内,请使用最后一列信息添加新设备数据并将其存储在包含该数据位置的地图中

class DataPosition {
    public Integer colNum;
    public Integer rowNum;

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