![](/img/trans.png)
[英]Xquery to get the value of a specific element from an XML which is stored as [XML Nullable column] in DB2
[英]Get each cells data from Excel converted to XML to stored to 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();
}
但是,在调用parser.parse(sheetSource)
,如何从每一行和每一列获取每个数据? 因为我想对每个单元格上的每个数据进行验证,然后将其存储到数据库中。
更新2我已经尝试使用此答案, https://stackoverflow.com/a/51818500/10454516 。 我可以获取数据,我尝试插入myObjectRepo.save(result)或myObjectRepo.save(myObject),我都将代码放置在void endRow方法内,并且还尝试将其放置在切换后的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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.