Find file History
Latest commit 161a44f Jan 3, 2017 @zhou-hao zhou-hao fix bug #1
Permalink
..
Failed to load latest commit information.
src fix bug #1 Jan 3, 2017
README.md 新增说明 Jul 12, 2016
pom.xml 优化依赖 Sep 13, 2016

README.md

office文档处理工具

引入依赖

    <!--pom.xml-->
     <dependency>
        <groupId>org.hsweb</groupId>
        <artifactId>hsweb-expands-office</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

      <repositories>
        <repository>
            <id>hsweb-nexus</id>
            <name>Nexus Release Repository</name>
            <url>http://nexus.hsweb.me/content/groups/public/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
     </repositories>

读取excel

      //读取为map
      try (InputStream in = FileUtils.getResourceAsStream("User.xlsx")) {
            List<Map<String, Object>> dataList = ExcelIO.read2Map(in);
            dataList.forEach(System.out::println);
        }

        //多sheet读取为map
      try (InputStream in = FileUtils.getResourceAsStream("User.xlsx")) {
             List<List<Map<String, Object>>> dataList = ExcelIO.read2MulMap(in);
             dataList.forEach(System.out::println);
         }

       //读取为bean
       try (InputStream in = Resources.getResourceAsStream("User.xlsx")) {
              //设置表头与字段映射,可通过反射获取
              Map<String, String> mapper = new HashMap<>();
              mapper.put("姓名", "name");
              mapper.put("年龄", "age");
              mapper.put("备注", "remark");
              //解析为User对象集合
              List<User> dataList = ExcelIO.read2Bean(in, mapper, User.class);
          }

写出excel

        List<Header> headers = new LinkedList<>();
        List<Object> datas = new ArrayList<>();
        // 简单粗暴的写出
        try (OutputStream outputStream = new FileOutputStream("target/test_1.xlsx")) {
            ExcelIO.write(outputStream, headers, datas);
            outputStream.flush();
        }

        //按模板写出 
         try (InputStream inputStream = FileUtils.getResourceAsStream("template.xlsx")
             ; OutputStream outputStream = new FileOutputStream("target/test_template.xlsx")) {
            //定义变量
            Map<String, Object> var = new HashMap<>();
            var.put("title", "测试");
            var.put("list", .......);
            //输出模板
            ExcelIO.writeTemplate(inputStream, outputStream, var);
            outputStream.flush();
        }

word模板写出

     try (InputStream in = new FileInputStream(FileUtils.getResourceAsFile("docx/template.docx"));
         OutputStream out = new FileOutputStream("target/test.docx")) {
        //构造 模板所需的变量
        Map<String, Object> vars = new HashMap<>();
        vars.put("name", "测试");
        vars.put("age", "16");
        WordIO.writeTemplate(in, out, vars);
        out.flush();
    }