使用poi从excel跳过或删除列

时间:2018-01-26 08:47:50

标签: java excel apache-poi

执行查询并进入一次所有数据。我不想在excel中显示表的某些列。我正在使用POI。现在最好给列名而不是列号来删除/跳过该列。在app.xml中,我指定了两个属性并尝试添加条件但没有获取如何添加。我的文件中有5张,我必须提到每张纸的属性标签。问题是我无法删除我在app.xml中提到的工作表中的列

<property key="sheet.name" value="abc"> <property key="column.name" value="chart,axis">

XSSFSheet sheet;
XSSFFont font = setFont(workBook.createFont(), bold, italic);
CellStyle cellStyle = workBook.createCellStyle();
cellStyle.setWrapText(true);
try {
        sheet = workBook.createSheet(sheetName);
        sheet.setDisplayGridlines(false);
        if (props.containsKey("sheet.name")) {
          sheetName = props.getProperty("sheet.name");
        }
        if (props.containsKey("colName.name")) {
          colName = props.getProperty("sheet.name");
          colName =  Arrays.asList( colName.split(","));
        }
        XSSFRow currentRow = sheet.createRow(rowNum);

        for (i = 0; i < headerData.size(); i++) {

            cellStyle = setAlign(cellStyle);
            cellStyle.setFont(font);
            cellStyle.setFillForegroundColor(IndexedColors.valueOf(color).getIndex());
            cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

            cellStyle = addCellBorder(cellStyle);

            Cell cel = currentRow.createCell(cell++);
            cel.setCellStyle(cellStyle);
            cel.setCellValue(camelCase(headerData.get(i)));
        }

        for (Object[] resultArray : resultList) {
            currentRow = sheet.createRow(++rowNum);
            cell = colNum;

            for (i = 0; i < resultArray.length; i++) {
                sheet.setColumnWidth(cell, 6500);
                if (resultArray[i] != null) {

                    CellStyle cs = addCellBorder(workBook.createCellStyle());
                    cs.setWrapText(true);
                    cs = setAlign(cs);

                    Cell cel = currentRow.createCell(cell++);
                    cel.setCellStyle(cs);

                    if (resultArray[i] instanceof Integer) {
                        cel.setCellValue(Integer.parseInt(resultArray[i].toString()));
                    } else if (resultArray[i] instanceof Long) {
                        cel.setCellValue(Long.parseLong(resultArray[i].toString()));
                    } else if (resultArray[i] instanceof Float) {
                        cel.setCellValue(Float.parseFloat(resultArray[i].toString()));
                    } else if (resultArray[i] instanceof Double) {
                        cel.setCellValue(Double.parseDouble(resultArray[i].toString()));
                    } else {
                        cel.setCellValue(resultArray[i].toString());
                    }
                } else {

                    CellStyle cs = addCellBorder(workBook.createCellStyle());
                    Cell cel = currentRow.createCell(cell++);
                    cel.setCellValue(""); //$NON-NLS-1$
                    cel.setCellStyle(cs);
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

将财产命名为:

<property key="skip.sheetname" value="column name(s) to delete" /> <property key="skip.sheetname" value="column name(s) to delete" />

然后试试这个:

if (props.containsKey("skip." + getSheetName())) {
     colName = props.getProperty("skip." + getSheetName());
     newList = Arrays.asList(colName.split(","));

     for (String skipHeader : newList) {
            Boolean flag = true;
            for (i = 0; i < headerData.size() && flag; i++) {
                if (headerData.get(i).equalsIgnoreCase(skipHeader)) {
                    headerData.set(i, "qwe");
                    flag = false;
                    skipColIndex.add(i);
                }

            }
    }
}