Hello_word_Baby 2016-02-16 08:26 采纳率: 0%
浏览 2853

java读取excel2013版的内容并把读取出来的内容插入到数据库中

想用java代码读取excel(2013)表格里的内容,但是excel里面有好几个sheet,还有好几个表,该怎么办,而且还要把读取出来的内容储存到数据库中去,求大神帮助

  • 写回答

3条回答 默认 最新

  • 咚咚swwadaw 2016-02-16 09:01
    关注
     public static List<String[]> readExcel(String filePath) {
            try {
                List<String[]> list = new ArrayList<String[]>();
                InputStream in = new FileInputStream(filePath);
                Workbook book = null;
                if (filePath.endsWith(".xls")) {
                    book = new HSSFWorkbook(in);
                } else if (filePath.endsWith(".xlsx")) {
                    book = new XSSFWorkbook(in);
                }
    
                // HSSFWorkbook hssfWorkbook = new HSSFWorkbook(in);
                // 循环工作表Sheet
                for (int i = 0; i < book.getNumberOfSheets(); i++) {
                    Sheet sheet = book.getSheetAt(i);
                    if (sheet == null) {
                        continue;
                    }
                    // 循环行Row
                    for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                        Row row = sheet.getRow(j);
                        if (row == null) {
                            continue;
                        }
                        String[] values = new String[row.getLastCellNum()];
                        for (int k = 0; k < row.getLastCellNum(); k++) {
                            Cell xh = row.getCell(k);
                            if (xh == null) {
                                continue;
                            }
                            values[k] = getValue(xh);
                        }
                        list.add(values);
                    }
                }
                return list;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算