java - 将 Excel 中的每个单元格数据转换为 XML 以存储到 DB
问题描述
在寻找一些资源后,我可以加载一个包含 1.000.000 行数据的 Excel 文件。但是,我不知道如何获取每个数据。到目前为止,这是我的代码...
public void create(MultipartFile file) throws Exception {
try {
InputStream fileStream = new BufferedInputStream(file.getInputStream());
OPCPackage opc = OPCPackage.open(fileStream);
XSSFReader xssf = new XSSFReader(opc);
SharedStringsTable sst = xssf.getSharedStringsTable();
XSSFReader.SheetIterator itr = (XSSFReader.SheetIterator)xssf.getSheetData();
// I just realize, if I running below for-loop,
// this only print strings and in random order, not in the same order as the excel file.
// 20 is just an example
for (int i = 0; i < 20; i++) {
System.out.println(sst.getEntryAt(i).getT().toString());
}
while (itr.hasNext()) {
InputStream is = itr.next();
if (itr.getSheetName().equals("MY_SHEET_NAME")) {
while ("data is avaiable, this is just example, I'll use something like hasNext() for the row in the sheet, but I dont know how to do it" != null) {
// Want to process and get all data in each cells, then store to DB
// What I did not know, is how to get data in each cells
}
} else {
throw new Exception("Sheet not found");
}
}
} catch (Exception e) {
throw new Exception("Error is: " + e.getMessage());
} finally {
if (is != null) {
is.close();
}
if (opc != null){
opc.close();
}
if (fileStream != null) {
fileStream.close();
}
}
}
我试图在这里查看处理工作表,但我没有得到如何获取每个单元格中的数据。任何帮助都会真正帮助我..
更新
如果我从链接中阅读了 apache POI 的文档,这里将处理我的 excel 的代码部分:
public void processOneSheet(String filename) throws Exception {
OPCPackage pkg = OPCPackage.open(filename);
XSSFReader r = new XSSFReader( pkg );
SharedStringsTable sst = r.getSharedStringsTable();
XMLReader parser = fetchSheetParser(sst);
// To look up the Sheet Name / Sheet Order / rID,
// you need to process the core Workbook stream.
// Normally it's of the form rId# or rSheet#
InputStream sheet2 = r.getSheet("rId2");
InputSource sheetSource = new InputSource(sheet2);
parser.parse(sheetSource);
sheet2.close();
}
但是,在 call 之后parser.parse(sheetSource)
,我如何从每一行和每一列中获取每个数据?因为我想对每个单元格上的每个数据进行验证,然后将其存储到数据库中。
更新 2 我尝试使用这个答案,https://stackoverflow.com/a/51818500/10454516。我可以获取数据,我尝试插入 myObjectRepo.save(result) 或 myObjectRepo.save(myObject),我都将代码放在 void endRow 方法中,并且我也尝试将它放在 switch 之后但在 if( lineNumber > 0),但它总是返回 NullPointerException。但是如果我没有调用 save 方法,我尝试在控制台中打印结果,结果被打印出来。
解决方案
您可以获取 excel 数据的一种方法是:
try {
InputStream excelFile = new FileInputStream(mFileName);
XSSFWorkbook wb = new XSSFWorkbook(excelFile);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
Iterator<Row> rows = sheet.rowIterator();
int col = 0, colPR = 1;
int pageRank = 0;
String url = null;
while (rows.hasNext()) {
row = (XSSFRow) rows.next();
url = row.getCell(col).getStringCellValue();
System.out.println("--------------------------");
}
FileOutputStream out = new FileOutputStream(mFileName);
wb.write(out);
out.flush();
out.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
推荐阅读
- ssas - 使用 AMO 创建 Analysis Services 数据库的精确副本
- python - 张量流中的3D矩阵乘法:AAB和AAB矩阵得到一个新的AAB矩阵
- keycloak - 无法从 Keycloak 中发送电子邮件
- flutter - 如何将 json 列表传递给 Flutter 中的 http Post 请求正文?
- r - R中的几个返回绘图
- flutter - VSCode:使用 Flutter Flavor 运行调试?
- html - 如何使用 HTML 和 CSS 使我的图像框响应
- c# - 我想从 ms-access 数据库中检索名为“pid”的列名中的自动编号值,并希望将该自动编号值插入到另一个表中
- firebase - 如何在flutter中删除firestore中的多个文档?
- node.js - 使用 Multer 和 Node js 在 React 上的本地存储中未显示图像