将 Excel 中的每个单元格数据转换为 XML 以存储到数据库
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();
}
}
}
我试图查看 here 来处理 sheet,但我不知道如何获取每个单元格中的数据。任何帮助都会真正帮助我..
更新
如果我阅读了来自 link 的 apache POI 文档 here,将处理我的 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
我尝试使用这个答案,。我可以获取数据,我尝试插入 myObjectRepo.save(result) 或 myObjectRepo.save(myObject),我都将代码放在 void endRow 方法中,我也尝试在切换后立即放置它但是在 if(lineNumber > 0) 中,但它总是 return NullPointerException。但是如果我没有调用保存方法,我尝试在控制台中打印结果,打印出结果。
获取 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();
}
好吧,我想回答我自己的问题。几经experiments/trials又以此为基础answer, I've made it to upload excel file which has at least 1.000.000 rows data and stored into db (postgresql). For upload, read and insert 1.000.000 rows data needs 5 minutes. Here is the link to the project。希望能帮到有需要的人。
在寻找一些资源后,我可以加载一个包含 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();
}
}
}
我试图查看 here 来处理 sheet,但我不知道如何获取每个单元格中的数据。任何帮助都会真正帮助我..
更新
如果我阅读了来自 link 的 apache POI 文档 here,将处理我的 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 我尝试使用这个答案,。我可以获取数据,我尝试插入 myObjectRepo.save(result) 或 myObjectRepo.save(myObject),我都将代码放在 void endRow 方法中,我也尝试在切换后立即放置它但是在 if(lineNumber > 0) 中,但它总是 return NullPointerException。但是如果我没有调用保存方法,我尝试在控制台中打印结果,打印出结果。
获取 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();
}
好吧,我想回答我自己的问题。几经experiments/trials又以此为基础answer, I've made it to upload excel file which has at least 1.000.000 rows data and stored into db (postgresql). For upload, read and insert 1.000.000 rows data needs 5 minutes. Here is the link to the project。希望能帮到有需要的人。