POI组件execel文件写入

第一步:maven依赖导入:

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>4.1.1</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>4.1.1</version>
</dependency>
<dependency>
   <groupId>net.sf.saxon</groupId>
   <artifactId>saxon-dom</artifactId>
   <version>8.7</version>
</dependency>

第二步:创建实体类ExcelVo

public class ExcelVo {
    //text 写入内容
    public String text;
    //sheetIndex 第几个sheet页
    public int sheetIndex;
    //writeRow 写入横坐标 java从0开始
    public int writeRow;
    //writeCell 写入纵坐标 java从0开始
    public int writeCell;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getSheetIndex() {
        return sheetIndex;
    }

    public void setSheetIndex(int sheetIndex) {
        this.sheetIndex = sheetIndex;
    }

    public int getWriteRow() {
        return writeRow;
    }

    public void setWriteRow(int writeRow) {
        this.writeRow = writeRow;
    }

    public int getWriteCell() {
        return writeCell;
    }

    public void setWriteCell(int writeCell) {
        this.writeCell = writeCell;
    }
}

第三步:

public class PoiWriteExcel {

    public static Workbook wb = null;

    /**
     * 写入workbook
     * @param path 文件路径
     * @param sheetIndex 第几个sheet页
     * @param text 写入内容
     * @param writeRow 写入横坐标 java从0开始
     * @param writeCell 写入纵坐标 java从0开始
     */
    public static void WriteExcel(String path,int sheetIndex,String text,int writeRow,int writeCell ){
//        wb = WorkbookFactory.create(new File(path));
        wb= ExcelFormat(path);
        Sheet sheet = wb.getSheetAt(sheetIndex);
        Row row = sheet.getRow(writeRow);

        if (null == row){
            row = sheet.createRow(writeRow);
        }

        Cell cell = row.getCell(writeCell);

        if (null==cell){
            cell = row.createCell(writeCell);
        }
        cell.setCellValue(text);
        SaveTemplate(path);
    }

    /**
     * 判断文件格式
     * @param path 文件路径
     * @return
     */
    public static Workbook ExcelFormat(String path){
        String suffix = path.substring(path.lastIndexOf("."));
        try{
            //根据后缀名判断
            if(suffix.equals(".xlsx")){
                wb = new XSSFWorkbook(new FileInputStream(path));//Excel 2007
            }else if(suffix.equals(".xls")){
                wb = new HSSFWorkbook(new FileInputStream(path));//Excel 2003
            }
        }catch(Exception e){
            System.out.println("文件异常,异常信息是:"+e);
        }
        return wb;
    }

    /**
     * 文件写入excle
     * @param filePath
     */
    public static void SaveTemplate(String filePath) {
        FileOutputStream fileOutputStream = null;
        try{

            //建立输出流(这里也是你给一个你希望写入的文件路径)
            fileOutputStream = new FileOutputStream(filePath);
            wb.write(fileOutputStream);
            fileOutputStream.flush();

            fileOutputStream.close();
            wb.close();

        }catch(Exception e){
            e.printStackTrace();
            System.out.println("文件输出异常,异常信息:" + e);
        }finally {
            if (fileOutputStream !=null){
                try {
                    fileOutputStream.close();
                    wb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("文件关闭异常,异常信息:" + e);
                }

            }

        }

    }
    /**
     * 循环写入excel表格
     * @param path
     * @param list
     */
    public static void cycleWrite(String path,List<ExcelVo> list){

        if (0 != list.size()){
            for (ExcelVo excel: list) {
                WriteExcel(path,excel.getSheetIndex(),excel.getText(),excel.writeRow,excel.getWriteCell());
            }
        }else {
            System.out.println("写入集合不能为空");
        }

    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值