选中想要生成的列,套用表格格式,选中表包含标题的选项确定,然后在最右边的一列第二行处,点击函数功能,选择CONCATENATE,在文本里输入想要的结构即可

 代码如下 复制代码
,=CONCATENATE("('",[@id],"','",[@name],"'),")

这样生成的之后的语句可以写为

 代码如下 复制代码
insert table1 (id,name) values ('1', "测试1"),('2', "测试2")...

如果你会C#我们可以使用OleDb读取Excel并生成SQL语句

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace ReadXlsxData
{
    static class ParseXlsx
    {
        public static readonly int COMMENT_INDEX=4;   //字段说明行下标
        public static readonly int KEY_INDEX = 5;    //主键行下标
        public static readonly int TYPE_INDEX = 6;   //字段类型行下标
        public static readonly int SQLNAME_INDEX = 7;      //数据库字段名行下标
        public static readonly int VALUE_INDEX = 8;      //value 行下标
        public static StringBuilder objectData = new StringBuilder();
        public static DataTable ToDataSet(string filePath)
        {
            string connStr = "";
          
            string fileType = System.IO.Path.GetExtension(filePath);
            if (string.IsNullOrEmpty(fileType)) return null;
            if (fileType == ".xls")
                connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties="Excel 8.0;HDR=NO;IMEX=1"";
            else
                connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + ";Extended Properties="Excel 12.0;HDR=NO;IMEX=1"";
            string sql_F = "Select * FROM [{0}]";
            OleDbConnection conn = null;
            OleDbDataAdapter da = null;
            DataTable dataTable = new DataTable();
            try
            {
                // 初始化连接,并打开   www.111cn.net              
                conn = new OleDbConnection(connStr);
                conn.Open();
                da = new OleDbDataAdapter();
                da.SelectCommand = new OleDbCommand(String.Format(sql_F, "Sheet1$"), conn);
                da.Fill(dataTable);
            }
            catch (Exception ex)
            {

}
            finally
            {                  // 关闭连接                 
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    da.Dispose();
                    conn.Dispose();
                }
            }
            conn.Close();
            da.Dispose();
            conn.Dispose();
            return dataTable;
        }
        public static string ReadExcelFile(string namef, string sqlfile, string sqlcomment)
        {
            objectData.Clear();
            DataTable dt = ToDataSet(namef);
            string temp, key,temp1,temp2;
            List<int> index = new List<int>();
        
            //创建表头
            objectData.Append("DROP TABLE IF EXISTS `" + sqlfile + "`;n");
            objectData.Append("CREATE TABLE `" + sqlfile + "` (n");
            int columnSize = dt.Columns.Count;
            int rowSize = dt.Rows.Count;
            DataColumn dc;
            DataRow dr;
            temp = string.Empty;
            key = string.Empty;
            temp1 = string.Empty;
            temp2 = string.Empty;
            DataRow dr5 = dt.Rows[COMMENT_INDEX],dr9=dt.Rows[SQLNAME_INDEX],dr8=dt.Rows[TYPE_INDEX];
            for (int i = 1; i < columnSize; i++)
            {
                dc = dt.Columns[i];
                temp2 = dr5[dc].ToString();
                temp1 = dr9[dc].ToString();
                if (temp2 == string.Empty)//空列判断
                    break;
                else if (temp1.ToString() != string.Empty)  //数据库字段
                {
                    index.Add(i);
                    temp = dr8[dc].ToString();
                    if (temp.Contains("vachar"))
                        objectData.Append("t`" + temp1 + "` " + temp + " NOT NULL DEFAULT '' COMMENT '" + temp2 + "',n");
                    else
                        objectData.Append("t`" + temp1 + "` " + temp + " NOT NULL DEFAULT '0' COMMENT '" + temp2 + "',n");
                    temp = dt.Rows[KEY_INDEX][dc].ToString();
                    if (temp != null && temp.Contains("key"))
                    {
                        key += "`" + temp1 + "` ";

}

}
            }
            if(key!=string.Empty)
                objectData.Append("tPRIMARY KEY (" + key + ")n");
            objectData.Append(") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='" + sqlcomment + "';n");
            for (int i = VALUE_INDEX; i < rowSize; i++)   //读取数据记录
            {
                objectData.Append("INSERT INTO `" + sqlfile + "` VALUES ('");
                dr = dt.Rows[i];
                int length = index.Count;
                for (int j = 0; j < length; j++)
                {
                    objectData.Append(dr[index[j]] + "','");
                }
                objectData.Remove(objectData.Length - 3, 2);
                objectData.Append(");n");
            } 
            return objectData.ToString();
        }
    }

}

最终导出结果如下


 

 

Excel数据生成Sql语句的方法的更多相关文章

  1. 把excel数据生成sql insert语句

    excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age . 在你的excel表格中增加一列,利用excel的公式自动生成sql语句,方法如下: ...

  2. excel数据生成sql insert语句

    excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age . 在你的excel表格中增加一列,利用excel的公式自动生成sql语句,方法如下: ...

  3. excel数据生成sql insert语句

    excel数据生成sql insert语句 excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age . 在你的excel表格中增加一列,利用ex ...

  4. Excel表格生成sql语句

    假如excel表格中有A.B.C三列数据,希望导入到数据库users表中,对应的字段分别是name,sex,age ,在你的excel表格中增加一列,利用excel的公式自动生成sql语句,方法如下: ...

  5. 两种查看EFCore生成Sql语句的方法

    一.利用反射生成查询语句 该方法转载自:https://jhrs.com/2019/28488.html (略有修改) using Microsoft.EntityFrameworkCore.Quer ...

  6. 使用Excel自动生成sql语句

    在近一段日子里,进入了新的项目组,由于项目需要,经常要将一些Excel表中的数据导入数据库中,以前并没有过多的接触过数据导入与数据处理,对于我来说比较痛苦,今天下午花了几个小时处理数据,但是同事给我提 ...

  7. 将表中数据生成SQL语句

    在开发过程中,经常需要我们对表中的数据进行转移,如果在同台机器,可以使用SQL自带的导入数据,但是如果想让所有的数据生成可执行的SQL语句,它的移植性最强了.首先要设计一个存储过程.具体如下: CRE ...

  8. 读取excel数据生成sql脚本

    package com.interact.util; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.b ...

  9. python读取excel表格生成sql语句 第一版

    由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦  作为程序猿当然要想办法解决,用Python写一个程序解决 需要用到 xlrd li ...

随机推荐

  1. Spring Batch Concepts Chapter

    Spring Batch Concepts Chapter The below figure shows two kinds of Spring Batch components:infrastruc ...

  2. 【灵感】wifi通过wifi发送优惠信息

    1.[灵感]wifi通过wifi发送优惠信息 http://content.businessvalue.com.cn/post/15362.html 2.手机彩票大爆发 http://content. ...

  3. 教你50招提升ASP.NET性能(二十):7条便利的ViewState技巧

    (32)Seven handy ViewState tips 招数32: 7条便利的ViewState技巧 Every time I have to deal with a classic ASP.N ...

  4. Codeforces Round #277 (Div. 2) E. LIS of Sequence DP

    E. LIS of Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/pr ...

  5. MYSQL分页limit速度太慢优化方法

    http://www.fienda.com/archives/110 在mysql中limit可以实现快速分页,但是如果数据到了几百万时我们的limit必须优化才能有效的合理的实现分页了,否则可能卡死 ...

  6. MySQL并发复制系列二:多线程复制

     http://blog.itpub.net/28218939/viewspace-1975822/ 并发复制(Parallel Replication) 系列二: Enhanced Multi-th ...

  7. Spring MVC 接收Json格式参数

    今天做了一个关于表格排序的功能,可以通过右边的箭头做排序操作,每次操作需要通过Ajax将每条记录的Id数组作为参数去发送请求, 后台Spring MVC接到参数后作更改序号操作. 前端页面发送请求的代 ...

  8. Web开发接口测试工具——Postman插件的使用(chrome浏览器)

    Postman是chrome浏览器的一款插件.Postman 可以模拟 http 请求的发送,并自动解析 JSON 和 XML 的返回数据. 可以手动的去配置各类 parameter,还支持 Basi ...

  9. HTML转换成word文档

    1工具类保存word文件 public class WordAction { public static void SaveAsWord(string fileName, string pFileNa ...

  10. Line Search and Quasi-Newton Methods

    Gradient Descent 机器学习中很多模型的参数估计都要用到优化算法,梯度下降是其中最简单也用得最多的优化算法之一.梯度下降(Gradient Descent)[3]也被称之为最快梯度(St ...