读取excel数据生成sql脚本

package com.interact.util;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 读取excel生成sql脚本
 * qcq0807
 */
public class InsertDataUtil {

    public static void main(String[] args) throws Exception {

        InsertDataUtil in = new InsertDataUtil();
       // String path = "C:\\Users\\Administrator\\Desktop\\8088需求记录\\user_qcq.xls";
        String path = "C:\\Users\\Administrator\\Desktop\\8088需求记录\\org_qcq.xls";  //表格的地址
        //String tabaleName = "t_user";
        String tabaleName = "t_org";  //表名
        in.insert(path, tabaleName);

    }

    /**
     * @param path      要解析的excel文件路径
     * @param dataTable 要写入到数据库中的表名
     * @throws BiffException
     * @throws IOException
     */
    public void insert(String path, String dataTable) throws BiffException, IOException {

        File file = new File(path);
        // 创建新的Excel 工作簿
        Workbook rwb = null;
        rwb = Workbook.getWorkbook(file);
        String toFileName = "D:/org_qcq.sql";  //写出的文件地址和名称
        //String toFileName = "D:/user_qcq.sql";
        // 得到工作簿中的第一个表索引即为excel下的sheet1,sheet2,sheet3...
        Sheet sheet = rwb.getSheets()[0];
        int rsColumns = sheet.getColumns();// 列数
        int rsRows = sheet.getRows();// 行数
        String simNumber = "";//每个单元格中的数据
        String sqlFinel = "";

        String str = "";//拼接要插入的列
        for (int j = 0; j < rsColumns ; j++) {
            Cell cell = sheet.getCell(j, 0);
            simNumber = cell.getContents();
            if (j == rsColumns - 1) {
                str += simNumber;
            } else {
                str += simNumber + ",";
            }

        }
        for (int i = 1; i < rsRows; i++) {
            String id = IdGenerator.getId();
            String sql = "insert into " + dataTable + "(" + str + ") values(";//拼接sql
            sql += "'" + id + "'";

            //添加用户时 需要添加角色 默认都是管理员
           //String sql1 = "insert into  user_role_relation (user_id,role_id) values(";
           //sql1 += "'" + id + "','" + "0158b84de66a0002');";  //管理员id去现场重新编写 切记切记!!!
            //System.out.println(sql1);
            for (int j = 0; j < rsColumns; j++) {
                Cell cell = sheet.getCell(j, i);
                simNumber = cell.getContents();
                if (j == rsColumns - 1) {
                    sql += "'" + simNumber + "'";
                } else {
                    if(j == 0){  //id需要进行处理
                        sql +=  ",";
                    }else{
                        sql += "'" + simNumber + "',";
                    }
                }
            }
            sql += " );";
            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter(toFileName,true);//创建文本文件  true属性不覆盖
                fileWriter.write(sql + "\r\n");//写入 \r\n换行
                //fileWriter.write(sql1 + "\r\n");//写入 \r\n换行
                fileWriter.flush();
                fileWriter.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(sql);
        }
    }

}

表格需要有一定的格式(其实格式主要是第一行,要跟数据库字段对应起来

读取excel数据生成sql脚本_第1张图片

 

 

没有做太多处理,参考链接:https://shenhaocric.iteye.com/blog/663802

转载于:https://www.cnblogs.com/qcq0703/p/11315465.html

你可能感兴趣的:(读取excel数据生成sql脚本)