如何使用Java中的POI HSSF库删除excel中行数据之间的空行
我需要帮助删除创建的记录列表之间的空行。我已经编写了删除空行的代码,但是它只能删除第一个空行。以下是我完成的代码:如何使用Java中的POI HSSF库删除excel中行数据之间的空行,java,excel,Java,Excel,我需要帮助删除创建的记录列表之间的空行。我已经编写了删除空行的代码,但是它只能删除第一个空行。以下是我完成的代码: private static void writeExcelFile(String[] keyValue, String fileName, String contents, ArrayList<String> listProperties, ArrayList<String> listPropertiesDescription,
private static void writeExcelFile(String[] keyValue, String fileName, String contents,
ArrayList<String> listProperties, ArrayList<String> listPropertiesDescription,
ArrayList<String> listPropertiesFileName) {
int rownum = 2;
HSSFSheet firstSheet;
HSSFWorkbook workbook = null;
workbook = new HSSFWorkbook();
firstSheet = workbook.createSheet("Resourcebundle");
Row headerRow = firstSheet.createRow((short) 1);
headerRow.setHeightInPoints(30);
headerRow.createCell((short) 0).setCellValue("Properties Name");
headerRow.createCell((short) 1).setCellValue("Properties Description");
headerRow.createCell((short) 2).setCellValue("Properties File Name");
System.out.println("listPropertiesDescription :: " + listPropertiesDescription.size());
System.out.println("listPropertiesFileName :: " + listPropertiesFileName.size());
System.out.println("listProperties all list :: " + listProperties.toString());
System.out.println("listPropertiesDescription all list :: "
+ listPropertiesDescription.toString());
int indexProperties = 0;
for (int i = rownum; i < listProperties.size(); i++) {
// Row row = firstSheet.getRow(i + 1);
Row row = firstSheet.getRow(i);
// System.out.println("row :: " + row);
if (row == null) {
// row = firstSheet.createRow(i + 1);
row = firstSheet.createRow(i);
}
System.out.println("check index :: " + indexProperties);
for (int j = 0; j < 1; j++) {
Cell cell = row.getCell(j);
System.out.println("cell :: " + cell);
if (cell == null) {
row.createCell(j).setCellValue(
listProperties.get(indexProperties + 1).toString().trim());
row.createCell(j + 1).setCellValue(
listPropertiesDescription.get(indexProperties + 1).toString().trim());
row.createCell(j + 2).setCellValue(
listPropertiesFileName.get(indexProperties + 1).toString().trim());
}
j++;
}
indexProperties++;
System.out.println("check index below :: " + indexProperties);
i++;
}
int lastRowCount = firstSheet.getLastRowNum();
for (int i = rownum; i < lastRowCount; i++) {
HSSFRow row = firstSheet.getRow(i);
if (row == null) {
removeRow(firstSheet, i);
// firstSheet.shiftRows(i + 1, lastRowCount, -1);
// i--; // Since you move row at i+1 to i
}
}
FileOutputStream fos = null;
try {
File file = new File("OnlineHelp Master Excel.xls");
//if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
String fileRB = outputLocation.concat("\\" + file);
fos = new FileOutputStream(new File(fileRB));
workbook.write(fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void removeRow(HSSFSheet sheet, int rowIndex) {
int lastRowNum = sheet.getLastRowNum();
if (rowIndex >= 0 && rowIndex < lastRowNum) {
sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
}
if (rowIndex == lastRowNum) {
HSSFRow removingRow = sheet.getRow(rowIndex);
if (removingRow != null) {
sheet.removeRow(removingRow);
}
}
}
它只能删除结果a下面的空行,其余的仍在那里
我真的需要帮助。谢谢
ema我知道这是一个非常旧的线程,但今天早上遇到了这个问题。不希望HSSF库获得空行。因此,发布此答案,以便遇到此答案的其他成员都能找到答案 这是我的解决方案——基本上是复制/粘贴周围的零件
HSSFSheet sheet = new HSSFWorkbook(new ByteArrayInputStream(content)).getSheetAt(0);
int headerRowIndex = sheet.getFirstRowNum();
List<String> columnNames = getColumnNames(sheet);
List<Map<String, String>> sheetData = new ArrayList<>();
sheet.forEach(row -> {
if (row.getRowNum() != headerRowIndex && !isRowEmpty(row)) {
sheetData.add(getRowData(row, columnNames));
}
});
HSSFSheet sheet=new HSSFWorkbook(new ByteArrayInputStream(content)).getSheetAt(0);
int headerRowIndex=sheet.getFirstRowNum();
列表columnNames=getColumnNames(表);
List sheetData=new ArrayList();
表.forEach(第->{
if(row.getRowNum()!=headerRowIndex&!isRowEmpty(row)){
添加(getRowData(行、列名称));
}
});
。。。方法是:
private boolean isRowEmpty(Row row) {
for (int cellIndex = row.getFirstCellNum(); cellIndex < row.getLastCellNum(); cellIndex++) {
Cell cell = row.getCell(cellIndex);
if (cell != null && cell.getCellTypeEnum() != CellType.BLANK) {
return false;
}
}
return true;
}
private boolean isRowEmpty(行){
对于(int-cellIndex=row.getFirstCellNum();cellIndex
我的测试文件以前有6个空行。他们现在都走了:)享受吧
private boolean isRowEmpty(Row row) {
for (int cellIndex = row.getFirstCellNum(); cellIndex < row.getLastCellNum(); cellIndex++) {
Cell cell = row.getCell(cellIndex);
if (cell != null && cell.getCellTypeEnum() != CellType.BLANK) {
return false;
}
}
return true;
}