高效率导入Excel问题

Kevin龙 2010-11-19 05:10:29
由于工作需要,我需要
①将十多万的数据从第一个Excel(客户提供)导入到第二个Excel,主要是对客户提供的数据进行格式化及组合其它信息
②再将第二个Excel的数据导入到SQL Server数据库
我把这些工作做成了一个工具,用的C#,但是结果发现第①步效率非常低,在使用OleDb往Excel导数据的时候只能一条一条Insert,好像是Excel OleDb只能一次插入一条,我试过批量插入,但始终没有成功,只能一条一条循环插入:

cmd.CommandText = " insert into [" + strSheetName + "$](" + columnNames + ") select '" + model.cityId + "','" + model.countyId + "','" + model.schoolId + "','" + model.classId + "','" + model.jiebId + "','" + model.id + "','" + model.name + "','" + model.zbm + "' ";
cmd.ExecuteNonQuery();

第②步我用的是循环组合SQL语句,每500条执行一次,效率倒也不会太低,但是也是很低的。
请问各位,有没有高效率的方法推荐一下,万分感谢。
...全文
478 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
CAINIAOXIANGFEI 2010-11-22
  • 打赏
  • 举报
回复
提供个思路 先将EXCEL的数据按要求放入到DATASET里面然后在输出到一个新的EXCEL文件中 应该就能达到你说的要求了这个就是批量的了无论多少都能很快完成
Kevin龙 2010-11-22
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 crackdung 的回复:]
用它
http://www.spreadsheetgear.com/









my blog
http://ufo-crackerx.blog.163.com/
[/Quote]

不要搞这么复杂吧?
Kevin龙 2010-11-22
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 pxh360395296 的回复:]
你的硬件是什么配制?可否例出来.
[/Quote]

CPU:i5 460 算个伪四核吧
内存:2G 就这个有点低
Win 7评分:5.5
鲁大师评分:2696
Kevin龙 2010-11-22
  • 打赏
  • 举报
回复
抱歉,周末不在。。
我想大家都有点偏题了,还是这个问题,我现在要将数据从Excel导入Excel这个问题,不讨论导SQL Server了,就是从一个Excel读取数据,然后加工后存到另一个Excel中,如何批量操作,如何提高效率?谢谢。
还有,不要怀疑我的硬件,目前我使用的是I5 460 2G,我想足够了!谢谢。
Kevin龙 2010-11-22
  • 打赏
  • 举报
回复
没有别的办法了么???
pxh360395296 2010-11-20
  • 打赏
  • 举报
回复
你的硬件是什么配制?可否例出来.
pxh360395296 2010-11-20
  • 打赏
  • 举报
回复
你的导入工具和数据库不在同一个地方.肯定慢啦.
xy_wolf 2010-11-20
  • 打赏
  • 举报
回复
openDataSource
crackdung 2010-11-20
  • 打赏
  • 举报
回复
用它
http://www.spreadsheetgear.com/









my blog
http://ufo-crackerx.blog.163.com/
wbxiaozhong 2010-11-20
  • 打赏
  • 举报
回复
....这么多
yangquanlaohou 2010-11-19
  • 打赏
  • 举报
回复
可以考虑用SQL的导入导出功能
luozhongxian001 2010-11-19
  • 打赏
  • 举报
回复
Datagridview控件将一次性读取全部的数据!
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "表格|*.xls";

if(ofd.ShowDialog()==DialogResult.OK)
{
string strPath1= ofd.FileName;
strPath.Text = strPath1;
string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + strPath1 + "';Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection Con = new OleDbConnection(strCon);//通过接口,建立连接
Con. Open();
string strSql = "select * from [Sheet1$]";//表名的写法也应注意不同,对应的excel表为sheet1,在这里要在其后加美元符号$,并用中括号
OleDbCommand Cmd = new OleDbCommand(strSql, Con);//建立要执行的命令
OleDbDataAdapter da = new OleDbDataAdapter(Cmd);//建立数据适配器
DataSet ds = new DataSet();//新建数据集,用来装读取的Excel表格里面的内容。
da.Fill(ds, "ldy");//把数据适配器中的数据读到数据集中的一个表中此处表名为ldy,可以任取表名)
//指定datagridview1的数据源为数据集ds的第一张表(也就是ldy表),也可以写ds.Table["ldy"]
dataGridView1.DataSource = ds.Tables[0]; #region
//在这里面设置处理读到的excel表中的内容。

#endregion
Con.Dispose();
ds.Dispose();
da.Dispose();
half_bucket 2010-11-19
  • 打赏
  • 举报
回复
SQL Server的功能如此强大了,不需要开发也很方便,可以考虑用SQL的导入导出功能
D505515079 2010-11-19
  • 打赏
  • 举报
回复
是不是弄得有点复杂
sweetjian 2010-11-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xh_mlj 的回复:]
SQL中大批量数据插入问题 利用SQL 的SqlBulkCopy方法
[/Quote]
SqlBulkCopy很强大!
wulechang 2010-11-19
  • 打赏
  • 举报
回复
使用 数据库 DTS
xh_mlj 2010-11-19
  • 打赏
  • 举报
回复
SqlStr = "select * from TmpA "
If Not Sql.ExecAdapter(SqlStr, strCN1) Then
Exit Sub
End If

Ds = New DataSet
Sql.Adapter.Fill(Ds, "Tmp")
Sql.Adapter.Dispose() : Sql.Close()


Dim DesBulkOp As SqlClient.SqlBulkCopy


DesBulkOp = New SqlClient.SqlBulkCopy(strCN2, SqlClient.SqlBulkCopyOptions.UseInternalTransaction)

DesBulkOp.BulkCopyTimeout = 500000000

DesBulkOp.NotifyAfter = Ds.Tables("Tmp").Rows.Count

DesBulkOp.DestinationTableName = "Tmp" & ComID

'可用下面的语句进行列名转换
'下面对两列进行了转换,则只插入此两列的值,其他列为空
' DesBulkOp.ColumnMappings.Add("CustomID", "ID")
' DesBulkOp.ColumnMappings.Add("CustomType", "Type")

DesBulkOp.WriteToServer(Ds.Tables("Tmp"))
xh_mlj 2010-11-19
  • 打赏
  • 举报
回复
SQL中大批量数据插入问题 利用SQL 的SqlBulkCopy方法
Kevin龙 2010-11-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ly_longyue 的回复:]
C# code
protected void btnImport_Click(object sender, EventArgs e)

  {

  int j = 1;

  DateTime str_date;

  DataSet ds;

  string IsXls = System.IO.Path.GetExtension(FileUpload.FileName)……
[/Quote]

bll.Add(model);
bll是什么东东,Add方法的内容,好像这才是重点!
而且你也是循环一条一条插入吧?有没有高效一点的?
龍月 2010-11-19
  • 打赏
  • 举报
回复
protected void btnImport_Click(object sender, EventArgs e)

  {

  int j = 1;

  DateTime str_date;

  DataSet ds;

  string IsXls = System.IO.Path.GetExtension(FileUpload.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名

  if (FileUpload.HasFile == false)//HasFile用来检查FileUpload是否有指定文件

  {

  uploadErrorTxt = "请您选择Excel文件!<br/>";

  }

  else if (IsXls != ".xls" && IsXls != ".xlsx")

  {

  uploadErrorTxt += "上传的文件必须为.xls后缀!<br/>";

  }

  else

  {

  string filename = DateTime.Now.ToString("yyyyMMddhhMMss") + FileUpload.FileName;

  string savePath = Server.MapPath(("~\\upfiles\\") + filename);

  FileUpload.SaveAs(savePath);

  try

  {

  ds = ExecleDs(savePath, filename);

  }

  catch

  {

  uploadErrorTxt = "导入失败,Excel工作表标签名错误,标签名必须是Sheet1,请查证后再导入!";

  gotoError();

  return;

  }

  DataRow[] dr = ds.Tables[0].Select();

  int rowsnum = ds.Tables[0].Rows.Count;

  if (rowsnum == 0)

  {

  uploadErrorTxt += "Excel表为空表,无数据!<br/>";

  }

  else

  {

  for (int i = 0; i < dr.Length; i++)

  {

  j = j + 1;

  string str_id = dr[i]["ID"].ToString();

  string str_name = dr[i]["名称"].ToString();

  try

  {

  str_date = DateTime.Parse(dr[i]["时间"].ToString());

  }

  catch

  {

  uploadErrorTxt = "第" + j + "行时间格式有误,停止导入!";

  gotoError();

  return;

  }

  bool b = bll.Exists(str_id); //id 不存在

  if (b == false)

  {

  try

  {

  model.id = int.Parse(str_id;);

  }

  catch

  {

  uploadErrorTxt = "第" + j + "行id格式错误,停止导入!";

  gotoError();

  return;

  }

  model.date = str_date;

  model.name=str_name;

  try

  {

  bll.Add(model);

  uploadSucceedTxt = "Excle表导入成功!";

  }

catch (MembershipCreateUserException ex)

  {

  uploadErrorTxt = "导入内容:" + ex.Message;

  }

  }

  else

  {

  uploadErrorTxt += "id:" + str_id + "id重复禁止导入!<br/>";

  }

  }

  }

  }

  if (uploadErrorTxt != "")

  {

  string txtPrint = "";

  txtPrint += "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#BBC7CE' class='tab01'>";

  txtPrint += "<tr>";

  txtPrint += "<td bgcolor='#ffffff'>";

  txtPrint += uploadErrorTxt;

  txtPrint += "</td>";

  txtPrint += "</tr>";

  txtPrint += "</table>";

  //输出错误提示

  this.labErrorTxt.Text = txtPrint;

  }

  else

  {

  string txtPrint = "";

  txtPrint += "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#BBC7CE' class='tab01'>";

  txtPrint += "<tr>";

  txtPrint += "<td bgcolor='#ffffff'>";

  txtPrint += uploadSucceedTxt;

  txtPrint += "</td>";

  txtPrint += "</tr>";

  txtPrint += "</table>";

  //输出成功提示

  this.labErrorTxt.Text = txtPrint;

  InitDateBind();

  }

  }

  void gotoError()

  {

  if (uploadErrorTxt != "")

  {

  string txtPrint = "";

  txtPrint += "<table width='100%' border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='#BBC7CE' class='tab01'>";

  txtPrint += "<tr>";

  txtPrint += "<td bgcolor='#ffffff'>";

  txtPrint += uploadErrorTxt;

  txtPrint += "</td>";

  txtPrint += "</tr>";

  txtPrint += "</table>";

  //输出错误提示

  this.labErrorTxt.Text = txtPrint;

  InitDateBind();

  }

  }

  public DataSet ExecleDs(string filenameurl, string table)

  {

  DataSet ds = new DataSet();

  string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + filenameurl + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'";

  OleDbConnection conn = new OleDbConnection(strConn);

  conn.Open();

  OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);

  odda.Fill(ds, table);

  return ds;

  }

加载更多回复(4)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧