java - java中从excel中提取数据到mysql

标签 java spring-boot

我想将数据从 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 张纸,只是为了测试代码,但是当我运行应用程序时,我复制了第一行,并且不读取第二行。

这是我的文件的数据

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

请问有什么想法吗?

最佳答案

我认为这是 < sheet.getLastRowNum() 的用法为您的第一次for循环:

getLastRowNum() --> last row contained n this sheet (0-based)

(参见 https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFSheet.html#getLastRowNum() )

因此这不是行数,而是最后一行的索引!

因此,如果有 2 行,则将返回 1,因为这是包含数据的最后一行。因此你的 for 循环必须达到 <= sheet.getLastRowNum()

关于java - java中从excel中提取数据到mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30865734/

相关文章:

java - 使用 simpleDateFormat 将字符串转换为 Util Date 不起作用

java - 访问被拒绝 ("java.io.FilePermission" "execute")

java - Spring RedisMap初始化时返回null

java - 如何调试错误 : java. lang.AssertionError:JSON 路径 "$.name"处没有值

java - 非servlet JSP

java - 当我使用 akka 流在现有消费者组中创建新消费者时,如何寻求 kafka 主题的结束?

elasticsearch - Spring 数据 Elasticsearch 中的可配置索引名称

tomcat - @EnableAutoConfiguration - excludeName

maven - jib-maven spring boot 配置文件

java - Spring :没有@Component 的@Autowired