栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

POI组件execel文件写入

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

POI组件execel文件写入

第一步:maven依赖导入:


   org.apache.poi
   poi
   4.1.1


   org.apache.poi
   poi-ooxml
   4.1.1


   net.sf.saxon
   saxon-dom
   8.7

第二步:创建实体类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;

    
    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);
    }

    
    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;
    }

    
    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);
                }

            }

        }

    }
    
    public static void cycleWrite(String path,List list){

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

    }
    
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956775.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号