I want to extract data from excel to mysql but at the beginning i want just to read the file so i use the code below :
@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";
}}
Now , the file Test.xsls has just 2 rows and 1 sheet just to test the code but when i run the application i have the first row duplicated and it doesn't read the seconde one .
Here's the data of my file
Country cc ndc RS
France 36 123 roaming international
Maroc 12 145 roaming international
And here's what i get
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
Any ideas please?
System.out.println?