我在每行中都有一个excel文件随机单元格
我使用Apache POI获取工作表,行和单元格,我想找到包含大多数单元格的行,然后我需要该行的单元格数
即根据我的示例文件,期待答案是
第3行包含7个单元格
我的代码
package volumata.web.service;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.io.FileInputStream;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReader {
public static final String SAMPL_XLSX_FILE_PATH = "/home/volumata/Desktop /sample.xlsx";
public static void main(final String[] args) throws IOException, TikaException, Exception {
// READ xslx file
File file = new File(SAMPLE_XLSX_FILE_PATH);
if (file.exists()) {
double bytes = file.length();
System.out.println("bytes : " + bytes);
} else {
System.out.println("File does not exists!");
}
// parameters of parse() method
Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream = new FileInputStream(file);
ParseContext context = new ParseContext();
// Parsing the given file
parser.parse(inputstream, handler, metadata, context);
// list of meta data elements elements
System.out.println(" metadata elements and values of the given file :");
String[] metadataNamesb4 = metadata.names();
for (String name : metadataNamesb4) {
System.out.println(name + ": " + metadata.get(name) + " Size:" + metadata.hashCode());
metadata.size();
}
XSSFWorkbook workbook = new XSSFWorkbook(file);
System.out.println("Workbook name:" + file.getName());
workbook.getNumberOfFonts();
System.out.println("Workbook name:" + file.getName());
System.out.println("Sheet count:" + workbook.getNumberOfSheets());
Iterator itesheet = workbook.sheetIterator();
int j = 1;
XSSFSheet sheet1 = (XSSFSheet) itesheet.next();
Iterator rowIter = sheet1.rowIterator();
Row r = (Row) rowIter.next();
short lastCellNum = r.getLastCellNum();
int[] dataCount = new int[lastCellNum];
System.out.println("sheet lastCellNum:" + lastCellNum);
System.out.println("sheet dataCount:" + dataCount.length);
while (itesheet.hasNext()) {
XSSFSheet sheet = (XSSFSheet) itesheet.next();
sheet.getColumnOutlineLevel(0);
System.out.println("sheet getColumnOutlineLevel:" + sheet.getColumnOutlineLevel(1));
Iterator ite = sheet.rowIterator();
System.out.println();
int i = 1;
while (ite.hasNext()) {
Row row = (Row) ite.next();
System.out.println(row.getOutlineLevel());
System.out.println(i + "-Cell count:" + row.getPhysicalNumberOfCells());
Iterator<Cell> cite = row.cellIterator();
while (cite.hasNext()) {
Cell c = cite.next();
System.out.print(c.toString() + " ");
}
i++; // System.out.println();
}
j++;
}
}
}