Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取Excel文件并跳过空行,但不跳过空列_Java_Excel_Jxl - Fatal编程技术网

Java 读取Excel文件并跳过空行,但不跳过空列

Java 读取Excel文件并跳过空行,但不跳过空列,java,excel,jxl,Java,Excel,Jxl,我希望读取Excel文件并跳过空行。我的代码跳过空单元格,但也跳过空列。如何跳过空行但保持空列?我正在使用jxljava for (int i = 0; i < sheet.getRows(); i++) { for (int j = 0; j < sheet.getColumns(); j++) { Cell cell = sheet.getCell(j, i); String con = cell.getContents();

我希望读取Excel文件并跳过空行。我的代码跳过空单元格,但也跳过空列。如何跳过空行但保持空列?我正在使用jxljava

for (int i = 0; i < sheet.getRows(); i++) {
    for (int j = 0; j < sheet.getColumns(); j++) {
        Cell cell = sheet.getCell(j, i);
        String con = cell.getContents();
        if (con != null && con.length() != 0) {          
            System.out.print(con);
            System.out.print("|");
        }
        else {
            continue;
        }
    }
}
for(int i=0;i
试试这个:

for (int i = 0; i < sheet.getRows(); i++) {
    boolean rowEmpty = true;
    String currentRow = "";
    for (int j = 0; j < sheet.getColumns(); j++) {
        Cell cell = sheet.getCell(j, i);
        String con=cell.getContents();
        if(con !=null && con.length()!=0){
            rowEmpty = false;
        }
        currentRow += con + "|";
    }
    if(!rowEmpty) {
        System.out.println(currentRow);
    }
}
for(int i=0;i
你所做的是:

  • 循环行
    • 循环通过列
      • 如果单元格仅为空,请将其跳过,否则(continue语句不会执行任何操作,因为它是循环中的最后一条指令,break语句在到达空单元格后将停止读取该行)
它的作用是:

  • 循环行
    • 循环通过列
      • 将单元格附加到行的字符串中,如果该行为非空,则将rowEmpty设置为false(因为它至少包含一个非空单元格)
    • 仅当行不为空时才打印该行
在C#的情况下,这对我很有效

private bool CheckIfEmptyRow(ISheet worksheet, int rowIndex)
{
    var worksheetRow = worksheet.GetRow(rowIndex);
    bool isRowEmpty = true;
    for (var columnIndex = worksheetRow.FirstCellNum; columnIndex < worksheetRow.LastCellNum; columnIndex++)
    {
        ICell cell = worksheetRow.GetCell(columnIndex, MissingCellPolicy.RETURN_NULL_AND_BLANK);
        if (!string.IsNullOrEmpty(cell.StringCellValue))
        {
            isRowEmpty = false;
            break;
        }
    }

    return isRowEmpty;
}
private bool CheckIfEmptyRow(ISheet工作表,int行索引)
{
var worksheetRow=worksheet.GetRow(rowIndex);
bool isRowEmpty=true;
对于(var columnIndex=worksheetRow.FirstCellNum;columnIndex
想一想,试试ApachePOI。仍然有支持,不确定jxl是否可以使用jxl?我肯定您可以,但poi允许对您的问题进行更多控制