JAVA-实现导入excel替换html模板并写出到对应文件夹

现有需求:实现通过导入excel文件,动态替换html模板并生成相应html到对应文件夹中

具体实现如下

excel文件中的内容如下

html模板如下

 

导入jar包  pom添加如下

<!--读取excel文件-->
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.12</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.12</version>
</dependency>
<dependency>
  <groupId>net.sourceforge.jexcelapi</groupId>
  <artifactId>jxl</artifactId>
  <version>2.6.10</version>
</dependency>

实现代码如下

package com.zj.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;


public class Test_mall {
    public static void main(String[] args){
        //本地模板html文件
        String templatePath = "F:\\TestMall\\info_template.html";
        //本地html输出路径
        String outFile = "F:\\TestMall";
        //本地excel文件路径
        String templateProduct = "F:\\TestMall\\产品详情导入.xls";
        makeHtml(templatePath,outFile,templateProduct);
    }

    //判断execel表行是否为空
    public static boolean isRowEmpty(Row row) {
        for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
            Cell cell = row.getCell(c);
            if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
                return false;
            }
        }
        return true;
    }

    public static void makeHtml(String templatePath,String outFile,String templateProduct){
        try {
            //1.读取excel文件
            FileInputStream inputStream = new FileInputStream(templateProduct);// 读取本地excel文件路径
            // 兼容xlsx xls
            Workbook wb = WorkbookFactory.create(inputStream);
            // 2.读取页脚sheet
            Sheet sheetAt = wb.getSheetAt(0);
            // 3.总行数 从0开始
            int trLength = sheetAt.getLastRowNum();
            // 4.得到Excel工作表的行
            Row row = sheetAt.getRow(0);
            // 5.总列数
            int tdLength = row.getLastCellNum();

            /***************************** 获取字段名 *********************************/
            List nameArry = new ArrayList();
            Row paramName = sheetAt.getRow(1);// 获取第二行数据 即字段名
            for (int j = 0; j < tdLength; j++) {
                // 得到Excel工作表指定行的单元格
                Cell cell1 = paramName.getCell(j);
                if (cell1 != null) {
                    cell1.setCellType(Cell.CELL_TYPE_STRING);
                }
                nameArry.add(cell1.getStringCellValue());
            }

            /***************************** 获取数值 *********************************/
            JSONArray valueTotal = new JSONArray();
            for (int i = 2; i <= trLength; i++) {// 第2行开始才是想要的数据
                JSONObject valueOne = new JSONObject();
                // 得到Excel工作表的行
                Row row1 = sheetAt.getRow(i);
                if (isRowEmpty(row1)) {
                    continue;
                }
                for (int j = 0; j < tdLength; j++) {
                    // 得到Excel工作表指定行的单元格
                    Cell cell1 = row1.getCell(j);
                    cell1.setCellType(Cell.CELL_TYPE_STRING);// 将所有列中的内容都设置成String类型格式
                    if (StringUtils.isBlank(cell1.getStringCellValue())) {
                        return ;
                    }
                    valueOne.put(nameArry.get(j).toString(), cell1.getStringCellValue());
                }
                valueTotal.add(valueOne);
            }

            File file;
            String newPath = "";
            //2.获取excel文件字段后 按照产品ID新建文件夹 将内容替换进模板中 并生成写出
            for (int i = 0; i < valueTotal.size(); i++) {
                if (valueTotal.getJSONObject(i).size() == 0) {
                    continue;
                }
                String templateContent = "";
                FileInputStream fileinputstream = new FileInputStream(templatePath);// 读取模板文件
                int lenght = fileinputstream.available();
                byte bytes[] = new byte[lenght];
                fileinputstream.read(bytes);
                fileinputstream.close();
                templateContent = new String(bytes);

                templateContent = templateContent.replaceAll("productDescription", valueTotal.getJSONObject(i).get("productDescription").toString());

                newPath = outFile+"/"+valueTotal.getJSONObject(i).get("productId").toString();
                file = new File(newPath);
                if(!file.exists()){//如果文件夹不存在
                    file.mkdir();//创建文件夹
                }

                String fileame = newPath+"/" + "info.html";// 生成的html文件保存路径。
                FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
                byte tag_bytes[] = templateContent.getBytes();
                fileoutputstream.write(tag_bytes);
                fileoutputstream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

实现结果如下

打开1文件夹  发现html已生成

访问html

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值