从 excel 中提取数据到 java 中的 mysql
Extract data from excel to mysql in java
我想从 excel 提取数据到 mysql 但一开始我只想读取文件所以我使用下面的代码 :
@RestController
public class ExtractDataController {
protected String[] celluls;
@RequestMapping(value = "/readExcel", method = RequestMethod.GET)
public String processExcel()
{
try {
InputStream ExcelFileToRead = new FileInputStream("Test.xlsx");
//définir l'objet workbook
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
//Itérer sur le nombre de sheets
for(int i=0;i<wb.getNumberOfSheets();i++)
{
XSSFSheet sheet = wb.getSheetAt(i);
//Itérer sur les lignes
for(int k=0;k<sheet.getLastRowNum();k++)
{
XSSFRow ligne = sheet.getRow(k);
celluls = new String[ligne.getLastCellNum()];
for(int j=0;j<ligne.getLastCellNum();j++){
if(ligne.getCell(j).getCellType() == 0)
celluls[j] = ""+ligne.getCell(j).getNumericCellValue();
else if(ligne.getCell(j).getStringCellValue().equals("/0"))
celluls[j] = ""+0;
else if(!ligne.getCell(j).getStringCellValue().equals("NIL") && !ligne.getCell(j).getStringCellValue().equals("NULL") && !ligne.getCell(j).getStringCellValue().equals("/0"))
{celluls[j] = ligne.getCell(j).getStringCellValue();
System.out.println(celluls[j]);
}
else celluls[j] = null;
System.out.println(celluls[j]);
}
}
}} catch (Exception e) {
System.err.println("Erreur");
// TODO Auto-generated catch block
e.printStackTrace();
}
return "data extracted successfully";
}}
现在,文件 Test.xsls 只有 2 行和 1 sheet 只是为了测试代码,但是当我 运行 应用程序时,我复制了第一行并且它没有'没看第二本。
这是我的文件的数据
Country cc ndc RS
France 36 123 roaming international
Maroc 12 145 roaming international
这就是我得到的
Country
Country
cc
cc
ndc
ndc
RS
RS
France
France
36.0
123.0
roaming international
roaming international
Country
Country
cc
cc
ndc
ndc
RS
RS
France
France
36.0
123.0
roaming international
roaming international
有什么想法吗?
我假设您的第一个 for
循环使用了 < sheet.getLastRowNum()
:
getLastRowNum() --> last row contained n this sheet (0-based)
(参见 https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#getLastRowNum())
因此这不是行数,而是最后一行的索引!
因此,如果您有 2 行,这将是 return 1,因为这是包含数据的最后一行。因此,您的 for 循环必须达到 <= sheet.getLastRowNum()
我想从 excel 提取数据到 mysql 但一开始我只想读取文件所以我使用下面的代码 :
@RestController
public class ExtractDataController {
protected String[] celluls;
@RequestMapping(value = "/readExcel", method = RequestMethod.GET)
public String processExcel()
{
try {
InputStream ExcelFileToRead = new FileInputStream("Test.xlsx");
//définir l'objet workbook
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
//Itérer sur le nombre de sheets
for(int i=0;i<wb.getNumberOfSheets();i++)
{
XSSFSheet sheet = wb.getSheetAt(i);
//Itérer sur les lignes
for(int k=0;k<sheet.getLastRowNum();k++)
{
XSSFRow ligne = sheet.getRow(k);
celluls = new String[ligne.getLastCellNum()];
for(int j=0;j<ligne.getLastCellNum();j++){
if(ligne.getCell(j).getCellType() == 0)
celluls[j] = ""+ligne.getCell(j).getNumericCellValue();
else if(ligne.getCell(j).getStringCellValue().equals("/0"))
celluls[j] = ""+0;
else if(!ligne.getCell(j).getStringCellValue().equals("NIL") && !ligne.getCell(j).getStringCellValue().equals("NULL") && !ligne.getCell(j).getStringCellValue().equals("/0"))
{celluls[j] = ligne.getCell(j).getStringCellValue();
System.out.println(celluls[j]);
}
else celluls[j] = null;
System.out.println(celluls[j]);
}
}
}} catch (Exception e) {
System.err.println("Erreur");
// TODO Auto-generated catch block
e.printStackTrace();
}
return "data extracted successfully";
}}
现在,文件 Test.xsls 只有 2 行和 1 sheet 只是为了测试代码,但是当我 运行 应用程序时,我复制了第一行并且它没有'没看第二本。
这是我的文件的数据
Country cc ndc RS
France 36 123 roaming international
Maroc 12 145 roaming international
这就是我得到的
Country
Country
cc
cc
ndc
ndc
RS
RS
France
France
36.0
123.0
roaming international
roaming international
Country
Country
cc
cc
ndc
ndc
RS
RS
France
France
36.0
123.0
roaming international
roaming international
有什么想法吗?
我假设您的第一个 for
循环使用了 < sheet.getLastRowNum()
:
getLastRowNum() --> last row contained n this sheet (0-based)
(参见 https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#getLastRowNum())
因此这不是行数,而是最后一行的索引!
因此,如果您有 2 行,这将是 return 1,因为这是包含数据的最后一行。因此,您的 for 循环必须达到 <= sheet.getLastRowNum()