将Excel表格数据写入到word的指定位置

将Excel表格数据转为word的指定位置

想要的结果是excel的一行数据,转换为一个word,并且更改为指定的位置。

首先使用的excel和word 是xlsx和docx格式的哦,其他格式的请自行修改哦。
我们使用Maven进行jar包的管理,
使用了两个poi的jar poi-ooxml,poi-scratchpad

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

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

首先先看excel的格式,
在这里插入图片描述

第一行是需要的数据的名称,从第二行开始就是需要的数据了。

首先在代码中指定excel地址以及实用的word模板是不现实的我们使用config.properties进行配置

//模板位置
fileSrc=D:\\MyProject\\008.docx
//生成word文件夹
fileDest=D:\\MyProject\\
//excel位置
excelSrc=D:\\MyProject\\007.xlsx
//Excel表名字
SheetName=Sheet1

并且在项目中将数据读取出来。

   /**
     * 读取配置文件
     *
     * @return
     */
    private Properties readP() {
   
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.properties");
        Properties p = new Properties();
        try {
   
            p.load(inputStream);
        } catch (IOException e1) {
   
            e1.printStackTrace();
        }
        return p;
    }

在java中读取需要数据的excel文档

 public static void main(String[] args) throws Exception{
   
        Excel_Tool excel_tool=new Excel_Tool();
        Properties p = excel_tool.readP();// 配置文件读取的参数
        String fielName = p.getProperty("excelSrc");
        String sheetName =  p.getProperty("SheetName");


       // readExcelData(fielName,sheetName,2,2);
        readExcelData(fielName,sheetName,p);
    }
public static void readExcelData(String fielName,String sheetName,Properties p) throws Exception{
   

        InputStream in = new FileInputStream(fielName);
        workbook = new XSSFWorkbook(in);
        sheet = workbook.getSheet(sheetName);
        //row = sheet.getRow(0);
        //一行总共的列数。
        int columnNum = sheet.getRow(0).getLastCellNum()
                - sheet.getRow(0).getFirstCellNum();
        //总共的行数
        int rowNum=sheet.getLastRowNum();
        ArrayList columnList=new ArrayList();
        row =sheet.getRow(0);
        for (int i = 0; i <columnNum ; i++) {
   
            cell=row.getCell(i);
            String column=getCell(cell);
			
            columnList.add(column);
            //判断需要哪一列数据,将所有的列名都保存在list中
        }
        getData(columnList,rowNum,p);
    }
   public static String getData(ArrayList arrayList,int rowNum
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值