第一步:安装NPOI nuget包
第二步:创建数据库sqlhelper.cs类(数据库sql辅助函数)
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
namespace PeopleInsert
{
public class SqlHelper
{
//这里是数据库连接信息,
public static string connectionString = "server=192.168.1.24;port=3306;user=1;password=1; database=1;Charset=utf8;Pooling=true";
#region ExecuteNonQuery
//执行SQL语句,返回影响的记录数
///
/// 执行SQL语句,返回影响的记录数
///
/// SQL语句
/// 影响的记录数
public int ExecuteNonQuery(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (MySqlException e)
{
connection.Close();
throw e;
}
}
}
}
///
/// 执行SQL语句,返回影响的记录数
///
/// SQL语句
/// 影响的记录数
public int ExecuteNonQuery(string SQLString, params MySqlParameter[] cmdParms)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (MySqlException e)
{
throw e;
}
}
}
}
#endregion
#region ExecuteScalar
///
/// 执行一条计算查询结果语句,返回查询结果(object)。
///
/// 计算查询结果语句
/// 查询结果(object)
public object ExecuteScalar(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (MySqlException e)
{
connection.Close();
throw e;
}
}
}
}
///
/// 执行一条计算查询结果语句,返回查询结果(object)。
///
/// 计算查询结果语句
/// 查询结果(object)
public object ExecuteScalar(string SQLString, params MySqlParameter[] cmdParms)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
using (MySqlCommand cmd = new MySqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (MySqlException e)
{
throw e;
}
}
}
}
#endregion
#region ExecuteReader
///
/// 执行查询语句,返回MySqlDataReader (注意:调用该方法后,一定要对MySqlDataReader进行Close )
///
/// 查询语句
/// MySqlDataReader
public MySqlDataReader ExecuteReader(string strSQL)
{
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand(strSQL, connection);
MySqlDataReader myReader = null;
try
{
connection.Open();
myReader = cmd.ExecuteReader();
return myReader;
}
catch (MySqlException e)
{
throw e;
}
finally
{
myReader.Close();
}
}
///
/// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
///
/// 查询语句
/// MySqlDataReader
public MySqlDataReader ExecuteReader(string SQLString, params MySqlParameter[] cmdParms)
{
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand();
MySqlDataReader myReader = null;
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
myReader = cmd.ExecuteReader();
cmd.Parameters.Clear();
return myReader;
}
catch (MySqlException e)
{
throw e;
}
finally
{
myReader.Close();
cmd.Dispose();
connection.Close();
}
}
#endregion
#region ExecuteDataTable
///
/// 执行查询语句,返回DataTable
///
/// 查询语句
/// DataTable
public DataTable ExecuteDataTable(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (MySqlException ex)
{
throw new Exception(ex.Message);
}
return ds.Tables[0];
}
}
///
/// 执行查询语句,返回DataSet
///
/// 查询语句
/// DataTable
public DataTable ExecuteDataTable(string SQLString, params MySqlParameter[] cmdParms)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
MySqlCommand cmd = new MySqlCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
}
catch (MySqlException ex)
{
throw new Exception(ex.Message);
}
return ds.Tables[0];
}
}
}
//获取起始页码和结束页码
public DataTable ExecuteDataTable(string cmdText, int startResord, int maxRecord)
{
using (MySqlConnection connection = new MySqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
MySqlDataAdapter command = new MySqlDataAdapter(cmdText, connection);
command.Fill(ds, startResord, maxRecord, "ds");
}
catch (MySqlException ex)
{
throw new Exception(ex.Message);
}
return ds.Tables[0];
}
}
#endregion
#region PageList Without Proc
///
/// 获取分页数据 在不用存储过程情况下
///
/// 总记录条数
/// 选择的列逗号隔开,支持top num
/// 表名字
/// 条件字符 必须前加 and
/// 排序 例如 ID
/// 当前索引页
/// 每页记录数
///
public DataTable getPager(out int recordCount, string selectList, string tableName, string whereStr, string orderExpression, int pageIdex, int pageSize)
{
int rows = 0;
DataTable dt = new DataTable();
MatchCollection matchs = Regex.Matches(selectList, @"top\s+\d{1,}", RegexOptions.IgnoreCase);//含有top
string sqlStr = sqlStr = string.Format("select {0} from {1} where 1=1 {2}", selectList, tableName, whereStr);
if (!string.IsNullOrEmpty(orderExpression)) { sqlStr += string.Format(" Order by {0}", orderExpression); }
if (matchs.Count > 0) //含有top的时候
{
DataTable dtTemp = ExecuteDataTable(sqlStr);
rows = dtTemp.Rows.Count;
}
else //不含有top的时候
{
string sqlCount = string.Format("select count(*) from {0} where 1=1 {1} ", tableName, whereStr);
//获取行数
object obj = ExecuteScalar(sqlCount);
if (obj != null)
{
rows = Convert.ToInt32(obj);
}
}
dt = ExecuteDataTable(sqlStr, (pageIdex - 1) * pageSize, pageSize);
recordCount = rows;
return dt;
}
#endregion
#region 创建command
private void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, string cmdText, MySqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (MySqlParameter parameter in cmdParms)
{
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
cmd.Parameters.Add(parameter);
}
}
}
#endregion
}
}
第三步:创建数据库读取类 OfficeHelper.cs
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
namespace PeopleInsert
{
public class OfficeHelper
{
///
/// 将excel文件内容读取到DataTable数据表中
///
/// 文件完整路径名
/// 指定读取excel工作薄sheet的名称
/// 第一行是否是DataTable的列名:true=是,false=否
/// DataTable数据表
public static DataTable ReadExcelToDataTable(string fileName, string sheetName = null, bool isFirstRowColumn = true)
{
//定义要返回的datatable对象
DataTable data = new DataTable();
//excel工作表
ISheet sheet = null;
//数据开始行(排除标题行)
int startRow = 1;
try
{
if (!File.Exists(fileName))
{
return null;
}
//根据指定路径读取文件
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
//根据文件流创建excel数据结构
IWorkbook workbook = WorkbookFactory.Create(fs);
//IWorkbook workbook = new HSSFWorkbook(fs);
//如果有指定工作表名称
if (!string.IsNullOrEmpty(sheetName))
{
sheet = workbook.GetSheet(sheetName);
//如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
if (sheet == null)
{
sheet = workbook.GetSheetAt(0);
}
}
else
{
//如果没有指定的sheetName,则尝试获取第一个sheet
sheet = workbook.GetSheetAt(0);
}
if (sheet != null)
{
IRow firstRow = sheet.GetRow(0);
//一行最后一个cell的编号 即总的列数
int cellCount = firstRow.LastCellNum;
//如果第一行是标题列名
if (isFirstRowColumn)
{
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
ICell cell = firstRow.GetCell(i);
if (cell != null)
{
cell.SetCellType(CellType.String);
string cellValue = cell.StringCellValue;
if (cellValue != null)
{
DataColumn column = new DataColumn(cellValue);
data.Columns.Add(column);
}
}
}
startRow = sheet.FirstRowNum + 1;
}
else
{
startRow = sheet.FirstRowNum;
}
//最后一列的标号
int rowCount = sheet.LastRowNum;
for (int i = startRow; i <= rowCount; ++i)
{
IRow row = sheet.GetRow(i);
if (row == null) continue; //没有数据的行默认是null
DataRow dataRow = data.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
dataRow[j] = row.GetCell(j).ToString();
}
data.Rows.Add(dataRow);
}
}
return data;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 将文件流读取到DataTable数据表中
///
/// 文件流
/// 指定读取excel工作薄sheet的名称
/// 第一行是否是DataTable的列名:true=是,false=否
/// DataTable数据表
public static DataTable ReadStreamToDataTable(Stream fileStream, string sheetName = null, bool isFirstRowColumn = true)
{
//定义要返回的datatable对象
DataTable data = new DataTable();
//excel工作表
ISheet sheet = null;
//数据开始行(排除标题行)
int startRow = 0;
try
{
//根据文件流创建excel数据结构,NPOI的工厂类WorkbookFactory会自动识别excel版本,创建出不同的excel数据结构
IWorkbook workbook = WorkbookFactory.Create(fileStream);
//如果有指定工作表名称
if (!string.IsNullOrEmpty(sheetName))
{
sheet = workbook.GetSheet(sheetName);
//如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
if (sheet == null)
{
sheet = workbook.GetSheetAt(0);
}
}
else
{
//如果没有指定的sheetName,则尝试获取第一个sheet
sheet = workbook.GetSheetAt(0);
}
if (sheet != null)
{
IRow firstRow = sheet.GetRow(0);
//一行最后一个cell的编号 即总的列数
int cellCount = firstRow.LastCellNum;
//如果第一行是标题列名
if (isFirstRowColumn)
{
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
ICell cell = firstRow.GetCell(i);
if (cell != null)
{
string cellValue = cell.StringCellValue;
if (cellValue != null)
{
DataColumn column = new DataColumn(cellValue);
data.Columns.Add(column);
}
}
}
startRow = sheet.FirstRowNum + 1;
}
else
{
startRow = sheet.FirstRowNum;
}
//最后一列的标号
int rowCount = sheet.LastRowNum;
for (int i = startRow; i <= rowCount; ++i)
{
IRow row = sheet.GetRow(i);
if (row == null || row.FirstCellNum < 0) continue; //没有数据的行默认是null
DataRow dataRow = data.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
//同理,没有数据的单元格都默认是null
ICell cell = row.GetCell(j);
if (cell != null)
{
if (cell.CellType == CellType.Numeric)
{
//判断是否日期类型
if (DateUtil.IsCellDateFormatted(cell))
{
dataRow[j] = row.GetCell(j).DateCellValue;
}
else
{
dataRow[j] = row.GetCell(j).ToString().Trim();
}
}
else
{
dataRow[j] = row.GetCell(j).ToString().Trim();
}
}
}
data.Rows.Add(dataRow);
}
}
return data;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
第四步:主函数使用线程安全队列多开50个线程写入数据
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections.Concurrent;
namespace PeopleInsert
{
class Program
{
public static ConcurrentQueue<DataRow> queue = new ConcurrentQueue<DataRow>();
static void Main(string[] args)
{
Console.WriteLine("start");
string filepath = @"副本111.xlsx";
DataTable dataTable = new DataTable();
dataTable = OfficeHelper.ReadExcelToDataTable(filepath,"Sheet1",true);
for(int i = 0; i < 50; i++)
{
Console.WriteLine("start"+i.ToString());
Thread thread = new Thread(chuli);
thread.IsBackground = true;
thread.Start();
}
if (dataTable.Rows.Count > 0)
{
foreach(DataRow dr in dataTable.Rows)
{
queue.Enqueue(dr);
}
}
Console.Read();
}
private static void chuli()
{
SqlHelper sqlHelper = new SqlHelper();
DataRow datarow;
while (true)
{
Thread.Sleep(5);
queue.TryDequeue(out datarow);
if (datarow==null)
{
continue;
}
string insertStr = string.Format("insert into tb_people(biaoname1,biaoname2,name3,name4,name5,name6) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}');SELECT @@Identity ",
datarow[1].ToString(),
datarow[2].ToString(),
datarow[3].ToString(),
datarow[4].ToString(),
datarow[5].ToString(),
datarow[6].ToString());
object id = sqlHelper.ExecuteScalar(insertStr ); //插入数据
//id 通过SELECT @@Identity可得到insert后数据库id
}
}
//通过身份证号验证男女 (男:true,女:false)
public static bool GetGenderByIdCard(string idCard)
{
if (string.IsNullOrWhiteSpace(idCard))
{
return false;
}
return Convert.ToBoolean(int.Parse(idCard.Substring(16, 1)) % 2);
}
}
}
如果遇到问题请私信我,看到后回复