亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 apache-poi 在列中顯示對象

如何使用 apache-poi 在列中顯示對象

HUH函數 2022-06-04 17:33:06
我有一個包含 234 個字段的大 DTO,我必須在使用 apache-poi 創建的 Excel 文件的列中顯示此 DTO 的每個字段的值。這是我的代碼:// Blank workbookXSSFWorkbook workbook = new XSSFWorkbook();Sheet sheet = workbook.createSheet("Export values");// Get the EntitySimfoot simEntity = simService.findById(simId).get();Row row = sheet.createRow(0);row.createCell(1).setCellValue("Consult our values");// and after this I want to convert my Simfoot object to a column in the third column ( so creteCell(2) ..... ).我想在我的第一列:什么都沒有,在我的第二列中只有字符串顯示(“咨詢我們的價值觀”),在我的第三列中我需要有我的 234 個字段。在一個單元格中有一個字段(字段的值)。因此,234 行在第三列中顯示一個值。
查看完整描述

2 回答

?
慕尼黑的夜晚無繁華

TA貢獻1864條經驗 獲得超6個贊

使用一些反射:


    // Blank workbook

    XSSFWorkbook workbook = new XSSFWorkbook();


    final Sheet sheet = workbook.createSheet("Export values");


    // Get the Entity

    final Simfoot simEntity = simService.findById(simId).get();


    Row row = sheet.createRow(0);

    row.createCell(1).setCellValue("Consult our values");


    // and after this I want to convert my Simfoot object to a column in the third column ( so creteCell(2) ..... ).

    Arrays.stream(simEntity.getClass().getDeclaredMethods())

            .filter(m -> m.getName().startsWith("get") && m.getParameterTypes().length == 0 && !void.class.equals(m.getReturnType()))

            .forEach(m -> {

                    try {

                            Object value = m.invoke(simEntity, null);

                            Row r = sheet.createRow(sheet.getLastRowNum()+1);

                            r.createCell(2).setCellValue(value == null ? "" : value.toString());

                    }

                    catch (Exception ex) {

                            // Manage Exception....

                    }

            });


查看完整回答
反對 回復 2022-06-04
?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

我將添加一個方法Simfoot來返回所有值:


public List<String> getAllValues() {

    return Arrays.asList(getAtt1(), getAtt2(), .. , getAtt234());

}

然后為每個屬性創建一行,然后您可以合并前 2 列的行。這里的例子有 6 個屬性:


int n = 6; // would be 234 for you

XSSFCellStyle styleAlignTop = workbook.createCellStyle();

styleAlignTop.setVerticalAlignment(VerticalAlignment.TOP);

Row row;

for(int i=0; i<n; i++) {

    row = sheet.createRow(i);

    if(i==0) {

        Cell cell = row.createCell(1);

        cell.setCellStyle(styleAlignTop);

        cell.setCellValue("Consult our values");

    }

    row.createCell(2).setCellValue(simEntity.getAllValues().get(i));

}

sheet.addMergedRegion(new CellRangeAddress(0, n-1, 0, 0));

sheet.addMergedRegion(new CellRangeAddress(0, n-1, 1, 1));

它顯示如下:

http://img1.sycdn.imooc.com//629b26f50001d06003420175.jpg

列出屬性的另一種方法是使用反射,但我發現它非常笨拙:


Simfoot simEntity = new Simfoot("pap", "pep", "pip", "pop", "pup", "pyp");


for(PropertyDescriptor propertyDescriptor :

    Introspector.getBeanInfo(Simfoot.class).getPropertyDescriptors()) {

        System.out.println(propertyDescriptor.getReadMethod().invoke(simEntity));

}

輸出:


pap

pep

pip

pop

pup

pyp

class Simfoot

所以你必須過濾掉getClass任何其他不需要的方法和吸氣劑


查看完整回答
反對 回復 2022-06-04
  • 2 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號