简体   繁体   English

Apache POI Java - 写入 excel 并动态更新单元格

[英]Apache POI Java - Write to excel and dynamically update cells

My java spring boot app needs to create a new excel file based on the contents of my DB.我的 java spring 启动应用程序需要根据我的 DB 的内容创建一个新的 excel 文件。 My current solution places all the data from my DB and inserts it in my excel sheet, but I want to improve it by not stating what the cell values are.我当前的解决方案将我的数据库中的所有数据放入我的 excel 工作表中,但我想通过不说明单元格值来改进它。 For example, although it works, my solution has 34 fields so I am stating the userRow.createCell line 34 times for each field which is repetitive.例如,虽然它有效,但我的解决方案有 34 个字段,所以我为每个重复的字段声明 userRow.createCell 行 34 次。 Ideally I want to say create the cell(n) and take all the values from each row in the DB.理想情况下,我想说创建单元格(n)并从数据库中的每一行中获取所有值。 How can this be done?如何才能做到这一点? Another for loop within this for loop?这个for循环中的另一个for循环? Every example I looked at online seems to specifically state what the cell value is.我在网上查看的每个示例似乎都专门针对 state 单元格值是什么。

List<CaseData> cases = (List<CaseData>) model.get("cases");
    Sheet sheet = workbook.createSheet("PIE Cases");
int rowCount = 1;
    for (CaseData pieCase : cases) {
        Row userRow = sheet.createRow(rowCount++);
        userRow.createCell(0).setCellValue(pieCase.getCaseId());
        userRow.createCell(1).setCellValue(pieCase.getAcknowledgementReceivedDate());
    }

Use the Reflection API使用反射 API

Example:例子:

try {
    Class caseDataObj = CaseData.class;
    Method [] methods = caseDataObj.getDeclaredMethods();
    Sheet sheet = workbook.createSheet("PIE Cases");
    int rowCount = 1;
    for(CaseData cd : cases) {
        int cellIndex = 0;
        Row userRow = sheet.createRow(rowCount++);
        for (Method method : methods) {
            String methodName = method.getName();
            if(methodName.startsWith("get")) {
                // Assuming all getters return String
                userRow.createCell(cellIndex++).setCellValue((String) method.invoke(cd));
            }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

There are probably many ways to do this, You can try something like this, this is how I usually go about it for things like what you are doing.可能有很多方法可以做到这一点,你可以尝试这样的事情,这就是我通常对它的处理方式,比如你正在做的事情。

public enum DATA {
     CASE_ID(0), 
     ACK_RECIEVED(1), 
     ETC(2); 
     //ETC(3) and so on

     public int index;

     DATA(int index) { 
         this.index = index; 
     }

     public Object parse(CaseData data) throws Exception {
          switch (this) {
              case CASE_ID:
                 return data.getCaseId();
              case ACK_RECIEVED:
                 return data.getAcknowledgementReceivedDate();
              case ETC:
                 return "etc...";
              default: return null;
         }
     }
}

Then, the implementation is:然后,实现是:

List<CaseData> cases = (List<CaseData>) model.get("cases");
Sheet sheet = workbook.createSheet("PIE Cases");
int rowCount = 1;
for (CaseData pieCase : cases) {
    Row userRow = sheet.createRow(rowCount++);
    for (DATA DAT : DATA.values()) {
        userRow.createCell(DAT.index).setCellValue(DAT.parse(pieCase));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM