要把excel 导入到SQL server中,试了几种方法都没有成功,求助 一次性全部导入SQL然后再判断哪些需要删除这样方便一点好像 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.OleDb;using System.Data.SqlClient;using System.Web;namespace WindowsApplication1{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid dataGrid1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.dataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(16, 32); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(664, 464); this.dataGrid1.TabIndex = 0; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(688, 494); this.Controls.Add(this.dataGrid1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void wirteSql() { DataTable tb; tb =OLEEXCEL().Tables[0]; string strCreateTable = " create table table1 ("; //strCreateTable = tb.Columns[0].ColumnName for(int col=0;col<tb.Columns.Count;col++) { if(col == tb.Columns.Count - 1) { strCreateTable = strCreateTable +" [" +tb.Columns[col].ColumnName + "]"+" varchar(50))"; } else { strCreateTable = strCreateTable +" [" +tb.Columns[col].ColumnName + "]"+" varchar(50),"; } } MessageBox.Show(strCreateTable); string sqlInsert=""; for(int i=0;i<tb.Rows.Count;i++) { string colvalue=""; for(int j =0;j<tb.Columns.Count;j++) { if(tb.Rows[i][j] == null) colvalue = "null"; if(j == tb.Columns.Count -1) { colvalue =colvalue+"'"+tb.Rows[i][j].ToString()+"'" ; } else { colvalue = colvalue+"'"+tb.Rows[i][j].ToString()+"'" + ","; } } sqlInsert = sqlInsert+ "insert into table1 values("+colvalue+")"; } MessageBox.Show(sqlInsert); SqlConnection conn = new SqlConnection ("server=(local);database=pubs;uid=sa;pwd = sa;"); conn.Open(); try { SqlCommand cmd = new SqlCommand (); cmd.Connection = conn; cmd.CommandText = strCreateTable+sqlInsert; cmd.ExecuteNonQuery(); } catch(Exception e) { MessageBox.Show(e.Message); } conn.Close(); } private DataSet OLEEXCEL() { OleDbConnection oleDbConnXls = new OleDbConnection (); string str; str="Provider=Microsoft.Jet.OLEDB.4.0;"; str+="Data Source=E:\\001.xls;"; str+="Extended Properties=Excel 8.0;"; //oleDbConnXls已申明; oleDbConnXls.ConnectionString= str; try { oleDbConnXls.Open(); } catch { MessageBox.Show("打开数据库失败!\n请检查数据库服务器后重新运行!"); } OleDbDataAdapter oleda = new OleDbDataAdapter ("select * from [aaa$]",oleDbConnXls); DataSet ds = new DataSet (); oleda.Fill(ds); if(oleDbConnXls!=null && oleDbConnXls.State == ConnectionState.Open) { oleDbConnXls.Close(); } dataGrid1.DataSource = ds.Tables[0]; return ds; } private void Form1_Load(object sender, System.EventArgs e) { OLEEXCEL(); wirteSql(); } }} insert into abc(aa,bb,cc) select aa,bb,cc from opendatasource('Microsoft.Jet.OLEDB.4.0','data source="(Excel在服务器上的绝对路径)";Extended Properties=Excel 8.0 ')...[Sheet1$]此方法中的Excel文件必须和数据库在同一台服务器上,如果Excel跟数据库不在同一台服务器上,可以先将Excel文件上传到服务器。两个红色的地方必须一样而且Excel文件的第一行必须是和数据库的字段名一致。也就是说数据库里的字段是aa,bb,cc,那么Excel里的第一行必须是aa,bb,cc,从第二行开始是真正要导入的数据 谢谢大家,好像leon520qqq0 的比较好,我也是这种思想,做一下先,有问题在请教大家 TO:leon520qqq0 OLEEXCEL()在 oleda.Fill(ds) 这里提示错误:“用户代码未处理 Oledbexception 找不到可安装的ISAM。” --------javascript与IE8兼容的问题 请教百度移动转码的 IP asp.net将数据导出到excel或word时为什么会是乱码 JAVA与C# 怎么把日期2004-5-6变成2004-05-06(c#) 一个关于连接超时的问题 datagrid控件中可以放checkbox控件吗? Cache移除事件的奇怪问题 电脑里面没有.net软件,怎么运行.net程序啊! 求VS.NET2003和SQL SERVER 个人版下载 |M| 如何让框架打印的时候点击预览为"仅打印选定打印框"而不是"按屏幕所列布局打印" ajaxpro 验证出现的一点问题,比较奇怪,请各位高手帮忙解决
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Web;namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 32);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(664, 464);
this.dataGrid1.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(688, 494);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void wirteSql()
{
DataTable tb;
tb =OLEEXCEL().Tables[0];
string strCreateTable = " create table table1 (";
//strCreateTable = tb.Columns[0].ColumnName
for(int col=0;col<tb.Columns.Count;col++)
{
if(col == tb.Columns.Count - 1)
{
strCreateTable = strCreateTable +" [" +tb.Columns[col].ColumnName + "]"+" varchar(50))";
}
else
{
strCreateTable = strCreateTable +" [" +tb.Columns[col].ColumnName + "]"+" varchar(50),";
}
}
MessageBox.Show(strCreateTable);
string sqlInsert="";
for(int i=0;i<tb.Rows.Count;i++)
{
string colvalue="";
for(int j =0;j<tb.Columns.Count;j++)
{
if(tb.Rows[i][j] == null)
colvalue = "null";
if(j == tb.Columns.Count -1)
{
colvalue =colvalue+"'"+tb.Rows[i][j].ToString()+"'" ;
}
else
{
colvalue = colvalue+"'"+tb.Rows[i][j].ToString()+"'" + ",";
}
}
sqlInsert = sqlInsert+ "insert into table1 values("+colvalue+")";
}
MessageBox.Show(sqlInsert);
SqlConnection conn = new SqlConnection ("server=(local);database=pubs;uid=sa;pwd = sa;");
conn.Open();
try
{
SqlCommand cmd = new SqlCommand ();
cmd.Connection = conn;
cmd.CommandText = strCreateTable+sqlInsert;
cmd.ExecuteNonQuery();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
conn.Close();
}
private DataSet OLEEXCEL()
{
OleDbConnection oleDbConnXls = new OleDbConnection ();
string str;
str="Provider=Microsoft.Jet.OLEDB.4.0;";
str+="Data Source=E:\\001.xls;";
str+="Extended Properties=Excel 8.0;";
//oleDbConnXls已申明;
oleDbConnXls.ConnectionString= str;
try
{
oleDbConnXls.Open();
}
catch
{
MessageBox.Show("打开数据库失败!\n请检查数据库服务器后重新运行!");
}
OleDbDataAdapter oleda = new OleDbDataAdapter ("select * from [aaa$]",oleDbConnXls);
DataSet ds = new DataSet ();
oleda.Fill(ds);
if(oleDbConnXls!=null && oleDbConnXls.State == ConnectionState.Open)
{
oleDbConnXls.Close();
} dataGrid1.DataSource = ds.Tables[0];
return ds;
} private void Form1_Load(object sender, System.EventArgs e)
{
OLEEXCEL();
wirteSql();
}
}
}
此方法中的Excel文件必须和数据库在同一台服务器上,如果Excel跟数据库不在同一台服务器上,可以先将Excel文件上传到服务器。
两个红色的地方必须一样
而且Excel文件的第一行必须是和数据库的字段名一致。也就是说数据库里的字段是aa,bb,cc,那么Excel里的第一行必须是aa,bb,cc,从第二行开始是真正要导入的数据
在 oleda.Fill(ds) 这里提示错误:“用户代码未处理 Oledbexception 找不到可安装的ISAM。”