gpt4 book ai didi

java - 在 POI 中过滤 Excel 中的列时删除考虑空白

转载 作者:行者123 更新时间:2023-11-30 10:32:45 24 4
gpt4 key购买 nike

您好,我正在使用 autoFilter 过滤 Excell 中的所有列,它在过滤时会考虑所有空白行(超出具有数据的行总数的行)。但是,我想在尝试过滤任何列时避免考虑空白行。

我正在使用下面的代码,sheet.setAutoFilter(CellRangeAddress.valueOf("A8:L13"));

我现在是这样的 I am currently getting like this

我需要这样 I need like this

最佳答案

AutoFilter [ ] (Blanks) 选项仅在筛选范围内没有空白单元格时才会消失。所以我们只需要将自动筛选单元格范围设置为已使用的范围。

例子:

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;

import java.util.Random;
import java.io.*;

class AutoFilterTest {

private static void setCellData(Sheet sheet) {

Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Name");
cell = row.createCell(1);
cell.setCellValue("Value1");
cell = row.createCell(2);
cell.setCellValue("Value2");
cell = row.createCell(3);
cell.setCellValue("City");

Random rnd = new Random();

for (int r = 1; r < 10 + rnd.nextInt(100); r++) {
row = sheet.createRow(r);
cell = row.createCell(0);
cell.setCellValue("Name " + ((r-1) % 4 + 1));
cell = row.createCell(1);
cell.setCellValue(r * rnd.nextDouble());
cell = row.createCell(2);
cell.setCellValue(r * rnd.nextDouble());
cell = row.createCell(3);
cell.setCellValue("City " + ((r-1) % 3 + 1));
}

}

public static void main(String[] args) {
try {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();

//create random rows of data
setCellData(sheet);

for (int c = 0; c < 4; c++) sheet.autoSizeColumn(c);

int lastRow = sheet.getLastRowNum();
sheet.setAutoFilter(new CellRangeAddress(0, lastRow, 0, 3));

wb.write(new FileOutputStream("AutoFilterTest.xlsx"));
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

关于java - 在 POI 中过滤 Excel 中的列时删除考虑空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553162/

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