自定义模板批量导入sql脚本

场景:

从文件里面读取一列的数据,作为插入sql的参数,批量生成sql脚本。

package com.yecc.fileRead;


import cn.hutool.core.io.file.FileReader;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;

/**
 * Created by yecc on 2020/11/20 15:56
 */
public class ScriptToImport {
    public static void main(String[] args) {

        File file = new File("C:\\Users\\Administrator\\Desktop\\派出所.xls");
        try {
            System.out.println("正在读取书名...");
            readColumn(file, 0);//读取第一列
            System.out.println("读取完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取excel表格中特定的列
     *
     * @param file
     *            文件
     * @param index
     *            第index列(0开始)
     * @throws Exception
     */
    public static void readColumn(File file, int index) throws Exception {
        InputStream inputStream = new FileInputStream(file.getAbsoluteFile());
        Workbook workbook = Workbook.getWorkbook(inputStream);
        Sheet sheet = workbook.getSheet(0);
        int rows = sheet.getRows();
        int columns = sheet.getColumns();
        for (int i = 1; i < rows; i++) {
            Cell cell = sheet.getCell(index, i);
            String contents = cell.getContents();
            System.out.println(String.format("INSERT INTO \"public\".\"t_md_stand_code_val\" (\"id\", \"name\", \"version\", \"type\", \"memo\", \"code\", \"owner_id\", \"map_key\", \"extended_type\", \"operate_time\", \"operate_user_id\", \"mb_id\", \"originally_val\", \"filed_id\") VALUES ('%s', '%s', NULL, 'StandCodeVal', '%s', '%s', NULL, NULL, NULL, '2020-11-23 08:44:54', NULL, '8080a79005eb5853a8a0e5d18f83a330', NULL, NULL);",UUID.randomUUID().toString().replaceAll("-",""),contents,contents,contents));

        }
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值