Java 读取Excel文件并跳过空行,但不跳过空列
我希望读取Excel文件并跳过空行。我的代码跳过空单元格,但也跳过空列。如何跳过空行但保持空列?我正在使用jxljavaJava 读取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();
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(因为它至少包含一个非空单元格)
- 仅当行不为空时才打印该行
- 循环通过列
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允许对您的问题进行更多控制