使用Poi读取和修改excel数据

节省时间直接上代码了

可以直接使用
先导入依赖
		<dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
            <version>3.10-FINALversion>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
            <version>3.10-FINALversion>
        dependency>
package com.indfintech.cf.biz.loan.controller;


import org.apache.commons.lang3.StringUtils;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;

@RestController
public class PoiExcelUtil {



    public static XSSFWorkbook workbook; // 工作簿
    public static XSSFSheet sheet; // 工作表
    public static XSSFRow row; // 行
    public static XSSFCell cell; // 列

    @RequestMapping("/test-excel")
    public void main() throws Exception {
        String fielName = "D:\\test.xlsx";
        String sheetName = "Sheet1";
        //读取某一格的数据,并返回需要写入的数据
        String creditCode = readExcelData(fielName, sheetName, i, 1);
        //将数据写入某个单元格中
        writeSpecifiedCell(fielName, sheetName, i, 2, creditCode);
    }

    /**
     * 读取excel数据
     *
     * @param fielName  文件名
     * @param sheetName sheet名
     * @param rownum    列
     * @param cellnum   行
     * @return 根据需要可修改返回值
     * @throws Exception
     */
    public String readExcelData(String fielName, String sheetName, int rownum, int cellnum) throws Exception {
        InputStream in = new FileInputStream(fielName);
        workbook = new XSSFWorkbook(in);
        sheet = workbook.getSheet(sheetName);
        row = sheet.getRow(rownum);
        cell = row.getCell(cellnum);
        String creditCode = "";
       //判断单元格类型
        switch (cell.getCellType()) {
            case XSSFCell.CELL_TYPE_NUMERIC:
                System.out.println("第" + (rownum + 1) + "行" + "第" + (cellnum + 1) + "列的值: " + String.valueOf(cell.getNumericCellValue()));
                creditCode = cell.getNumericCellValue();
                break;
            case XSSFCell.CELL_TYPE_STRING:
                System.out.println("第" + (rownum + 1) + "行" + "第" + (cellnum + 1) + "列的值: " + cell.getStringCellValue());
                creditCode = cell.getStringCellValue();
                break;
            default:
                System.out.println("第" + (rownum + 1) + "行" + "第" + (cellnum + 1) + "列的值: " + cell.getStringCellValue());
                creditCode = cell.getStringCellValue();
                break;
        }
        in.close();
        if (creditCode == null) {
            return "没有数据";
        } else {
            return creditCode;
        }
    }

    /**
     * 写入excel
     *
     * @param path      文件路径
     * @param sheetName sheet名称
     * @param rows      列号
     * @param line      行号
     * @param value     要写入的值
     */
    public static void writeSpecifiedCell(String path, String sheetName, int rows, int line, String value) {
        //根据路径获取文件
        File file = new File(path);
        //定义输入流对象
        FileInputStream excelFileInputStream = null;
        try {
            excelFileInputStream = new FileInputStream(file);
            // 拿到文件转化为JavaPoi可操纵类型
            Workbook workbook = WorkbookFactory.create(excelFileInputStream);
            excelFileInputStream.close();
            ////获取excel表格
            Sheet sheet = workbook.getSheet(sheetName);
            //获取单元格的row和cell
            // 获取行
            Row row = sheet.getRow(rows);
            // 获取列
            Cell cell = row.getCell(line);
            //设置单元的值
            cell.setCellValue(value);
            //写入数据
            FileOutputStream excelFileOutPutStream = new FileOutputStream(file);
            workbook.write(excelFileOutPutStream);
            excelFileOutPutStream.flush();
            excelFileOutPutStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            excelFileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

简单总结,到此结束!

你可能感兴趣的:(使用Poi读取和修改excel数据)