java实现Excel文件读写操作

首先导入Excel操作相关依赖
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.10</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>
Excel文件读取操作的方法
   /**
     * 去读Excel的方法
     * @param filePath 要读取的excel文件路径
     * @return
     */
    public List readExcel(String filePath) {
        try {
            File file = new File(filePath);
            // 创建输入流,读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            // Excel的页签数量
            int sheet_size = wb.getNumberOfSheets();
            List<List<List>> result = new ArrayList<>();
            for (int index = 0; index < sheet_size; index++) {
                List<List> outerList = new ArrayList<>();
                // 每个页签创建一个Sheet对象
                Sheet sheet = wb.getSheet(index);
                // sheet.getRows()返回该页的总行数
                for (int i = 0, m = 0; i < sheet.getRows(); i++, m++) {
                    List<String> innerList = new ArrayList();
                    // sheet.getColumns()返回该页的总列数
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        String cellinfo = sheet.getCell(j, i).getContents();
                        innerList.add(cellinfo);
                    }
                    outerList.add(m, innerList);
                }
                result.add(outerList);
            }
            //返回Excel全部数据的集合
            return result;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
Excel创建并写入数据的方法
   public void writeExcel(List<List<List>> excelAllList, String finalXlsxPath) throws WriteException, IOException {
        WritableWorkbook workbook;
        WritableSheet sheet;
        OutputStream os;
        File finalXlsxFile = new File(finalXlsxPath);
        //文件若不存在就新建一个
        if (!finalXlsxFile.exists()) {
            finalXlsxFile.createNewFile();
        }
        //创建输出流
        os = new FileOutputStream(dataFile);
        //创建工作薄
        workbook = Workbook.createWorkbook(os);
        int num = 1;
        for(List<List> excelList : excelAllList) {
            //创建新的一页
            sheet = workbook.createSheet("Sheet"+num, num-1);
            num++;
            for (int i = 0; i < excelList.size(); i++, r++) {
                List list = excelList.get(i);
                //迭代列,默认从0开始
                for (int j = 0; j < list.size(); j++) {
                    //创建单元格并添加到页中
                    String cellStr = list.get(j).toString();
                    Label label = new Label(j, r,cellStr);
                    sheet.addCell(label);
                }
            }
        }
        //把创建的内容写入到输出流中,并关闭输出流
        workbook.write();
        workbook.close();
        os.close();
    }
读写Excel方法示例
    /**
     *
     * @param inPath 输入的文件路径    如:C:\\Users\\Administrator\\Desktop\\data.xls
     * @param outPath 输出的文件路径
     * @throws IOException
     * @throws WriteException
     */
    public  void domain(String inPath,String outPath) throws IOException, WriteException {
        ReadExcel obj = new ReadExcel();
        List<List<List>> excelAllList = obj.readExcel(inPath);
        /**
         * 这里可以对数据集合进行自定义处理然后再输出
         * dosomthing(excelAllList)
         */
        writeExcel(excelAllList,outPath);
    }

代码已经过本人测试,请放心使用。目前仅支持.xls结尾的excel文件。

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值