通過讀取excel數據生成sql

最近公司好多計算邏輯通過ognl表達式操作,需要將ongl配置到表中,每天寫入很多,深感眼睛的疲憊,所以想到以前做的導入excel數據的功能,利用這個實現了一個自動生成相應sql的程序

首先引入相應的jar包

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.2</version>
        </dependency>

通過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<Map<String, Object>> 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個積分,如果感覺不合理,可以私信我.

資源地址在此

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章