gpt4 book ai didi

java - 从Excel读取数据并删除空白行并从行中获取数据添加到数组列表中的列表

转载 作者:行者123 更新时间:2023-12-02 05:54:43 27 4
gpt4 key购买 nike

如何从数组列表中删除空白行和单元格。我用过 myList.removeAll(Arrays.asList("", null));此行确实帮助我删除空白单元格。但它仍然向列表中添加空白行。我还可以添加按行读取所有数据并将列表添加为行。

作为列表行输出除外。

    [DataGrouping RWAExposureType AA RWA ex 1.06x AA RWA SA RWA, Credit Available For Sale $ 449,454 $ 476,421 $ 264,503,Contingent $ 113,262 $ 120,057 $ 258,508,Total $ 562,715 $ 596,478 $ 523,011, Total $ 562,715 $ 596,478 $ 523,011]

读取Excel的代码

      ArrayList myList = new ArrayList();
FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "\\src\\main\\resources\\excelfiles\\Mode Report.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(fis);
CreationHelper creationHelper = wb.getCreationHelper();
// Read sheet inside the workbook by its name
XSSFSheet sh1 = wb.getSheetAt(0);
// Data formatter
DataFormatter formatter = new DataFormatter();
FormulaEvaluator formulaEvaluator = creationHelper.createFormulaEvaluator();

for (Row row : sh1) {
if (row == null) {

}
for (Cell cell : row) {

CellStyle cellStyle = cell.getCellStyle();
String dataFormatString = cellStyle.getDataFormatString();

if (dataFormatString != null && dataFormatString.contains(";"))
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(dataFormatString + ";"));


String cellContent = formatter.formatCellValue(cell, formulaEvaluator);

myList.removeAll(Arrays.asList("", null));
myList.add(cellContent);

}
}
System.out.println(myList + "lisi");

使用removeAll输出仍然从Excel中获取空白行

        [


, Data Grouping, RWA Exposure Type, ( $ M ) AA RWA ex 1.06x, ( $ M ) AA RWA, ( $ M ) SA RWA, Credit, $ 562,715, $ 596,478, $ 523,011, Available For Sale, $ 449,454, $ 476,421, $ 264,503, Contingent, $ 113,262, $ 120,057, $ 258,508, Total, $ 562,715, $ 596,478, $ 523,011, 5/6/2019 2:23:35 PM
, ]lisi

不带removeAll的输出。 [

        , , , , , Data Grouping, RWA Exposure Type, ( $ M ) AA RWA ex 1.06x, ( $ M ) AA RWA, ( $ M ) SA RWA, Credit, , $ 562,715, $ 596,478, $ 523,011, , Available For Sale, $ 449,454, $ 476,421, $ 264,503, , Contingent, $ 113,262, $ 120,057, $ 258,508, Total, , $ 562,715, $ 596,478, $ 523,011, 5/6/2019 2:23:35 PM
, , , , ]lisi

预期输出应该是

        [Data Grouping, RWA Exposure Type, ( $ M ) AA RWA ex 1.06x, ( $ M ) AA RWA, ( $ M ) SA RWA, Credit, $ 562,715, $ 596,478, $ 523,011, Available For Sale, $ 449,454, $ 476,421, $ 264,503, Contingent, $ 113,262, $ 120,057, $ 258,508, Total, $ 562,715, $ 596,478, $ 523,011, 5/6/2019 2:23:35 PM]lisi

最佳答案

根据 XtremeBaumer 的评论

if(cellContent != null && !cellContent.trim().isEmpty())
myList.add(cellContent);

关于java - 从Excel读取数据并删除空白行并从行中获取数据添加到数组列表中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56023822/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com