通过读取excel数据生成sql

最近公司好多计算逻辑通过ognl表达式操作,需要将ongl配置到表中,每天写入很多,深感眼睛的疲惫,所以想到以前做的导入excel数据的功能,利用这个实现了一个自动生成相应sql的程序

首先引入相应的jar包


            org.apache.poi
            poi
            3.8
        
        
            org.apache.poi
            poi-ooxml
            3.8
        
        
        
            commons-io
            commons-io
            2.2
        

通过poi实现excel数据读取

package com.mk.sql.contorller;
import com.mk.sql.util.FileUpload;
import com.mk.sql.util.PathUtil;
import com.mk.sql.util.ReadExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xuzn @ClassName: RExcelController @ProjectName generator_sql
 * @date 2020/5/20/10:45 下午
 */
@Controller
@RequestMapping(value = "/excel")
public class RExcelController {

  public static final String FILEPATHFILE = "uploadFiles/file/";
  /** 从EXCEL导入到数据库 */
  @RequestMapping(value = "/readExcel")
  @ResponseBody
  public String readExcel(@RequestParam(value = "excel", required = false) MultipartFile file) {
    String ret = "success";
    if (null != file && !file.isEmpty()) {
      // 判断excel文件是03版本还是07版本
      String filePath = PathUtil.getClasspath() + FILEPATHFILE; // 文件上传路径
      String fileName = FileUpload.fileUp(file, filePath, "excel"); // 执行上传
      List> list = ReadExcelUtil.readExcel(filePath, fileName, 1, 0, 0);
      if (list.size() == 0) {
        ret = "list size is 0";
        return ret;
      }
      Date date = new Date();
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String dateStr = dateFormat.format(date);
      /** */
      for (int i = 0; i < list.size(); i++) {
        try {
          String id = list.get(i).get("var" + 0).toString();
          String no = list.get(i).get("var" + 1).toString();
          String desc = list.get(i).get("var" + 2).toString();
          String name = list.get(i).get("var" + 3).toString();
          Object o = list.get(i).get("var" + 4);
          String value = "";
          if (o != null && !"".equals(o)) {
            value = o.toString();
          }
          String type = list.get(i).get("var" + 5).toString();
          String calculate = list.get(i).get("var" + 6).toString();
          String str =
              "insert into `scdata`.`strategy` ( `pkid`,`id`, `strategy_no`, `channel_id`, `group_no`, `status`, `strategy_name`, `create_time`,"
                  + " `update_time`, `desc`, `direct_key`, `direct_key_type`, `ognl_value`, `strategy_logic`) values "
                  + "( '"
                  + no
                  + "', '"
                  + no
                  + "','"
                  + no
                  + "', '"
                  + id
                  + "', '1', '1', '"
                  + calculate
                  + "', '"
                  + dateStr
                  + "', '"
                  + dateStr
                  + "', "
                  + "'"
                  + desc
                  + "', '"
                  + name
                  + "', '"
                  + type
                  + "', '"
                  + value
                  + "', '"
                  + desc
                  + "');";
          System.out.println(str);
        } catch (Exception e) {
          ret = "fail";
          return ret;
        }
      }
    }
    return ret;
  }
}

其实如果不是经常操作插入sql的话,这个程序没有什么卵用,感兴趣的朋友可以下载下来看看,2个积分,如果感觉不合理,可以私信我.

资源地址在此

你可能感兴趣的:(java)