[英]I have an xlxs parser. In the code. I can't change my approach much
public ResponseEntity<Object> importPcpXlsx(MultipartFile xlsx) {
try {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(xlsx.getInputStream());
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
Row row;
Cell cell;
int i = 1;
List<String> number = new ArrayList<>();
while (true) {
if (xssfSheet.getRow(i) != null) {
row = xssfSheet.getRow(i);
cell = row.getCell(0);
if (cell.getSheet()
.getRow(0)
.getCell(0)
.getRichStringCellValue()
.getString()
.equals("Number".trim())) {
number.add(cell.getStringCellValue().trim());
log.info("number: " + i + ". № " + cell.getStringCellValue());
}
} else {
i = 1;
break;
}
i++;
}
List<String> kpgz = new ArrayList<>();
while (true) {
if (xssfSheet.getRow(i) != null) {
row = xssfSheet.getRow(i);
cell = row.getCell(1);
if (cell.getSheet()
.getRow(0)
.getCell(1)
.getRichStringCellValue()
.getString()
.equals("kpgz".trim())) {
kpgz.add(cell.getStringCellValue().trim());
log.info("kpgz: " + i + ". " + cell.getStringCellValue());
}
} else {
i = 1;
break;
}
i++;
}
I go through the columns and read all the lines under each column.我 go 通过列并阅读每列下的所有行。 But what if I have some columns will not come in xlxs file.
但是,如果我有一些列不会出现在 xlxs 文件中怎么办。 That is, I need an option where I do not have to strictly specify columns.
也就是说,我需要一个不必严格指定列的选项。 Please predict.
请预测。 In code Iread by columns, since columns have different types and should be processed differently.
在按列读取的代码中,由于列具有不同的类型,因此应该进行不同的处理。 I need to do this so that I do not specify the number of columns in the column number code, as I did cell = row.getCell(0);
我需要这样做,这样我就不会像我那样在列号代码中指定列数 cell = row.getCell(0); or row.getCell(1) or row.getCell(2)
或 row.getCell(1) 或 row.getCell(2)
Read the columns one by one and bind their type, then check data under these columns.一一读取列并绑定它们的类型,然后检查这些列下的数据。
See example of type checking there: Best language to parse extremely large Excel 2007 files请参阅此处的类型检查示例: Best language to parse extremely large Excel 2007 files
If you can def.netely identify column, it is not a problem.如果你能 def.netely 识别列,那不是问题。 If not, you should think about changing api.
如果不行,你应该考虑换api。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.