从excel导入数据到数据库
建立Connection对象的数据源连接字符串:
"Provider=Microsoft.Jet.Oledb.4.0;Data Source=Excel 文件物理路径 + ";Extended Properties=Excel 8.0";
DataAdapter对象中的SQL语句应为:"Select 字段列表 From [工作表名$]"
具体示例:
<%@ Import NameSpace="System.Data"%>
<%@ Import NameSpace="System.Data.OleDb"%>
<script Language="C#" runat="server">
void Page_Load(Object sender,EventArgs e)
{
string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Server.MapPath("Book1.xls") + ";Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [Sheet1$]",conn);
DataSet ds = new DataSet();
adp.Fill(ds,"Book1");
ExlDataGrid.DataSource = ds.Tables["Book1"].DefaultView;
ExlDataGrid.DataBind();
}
</script>
<html>
<body>
<h4 align="center">读取Excel文件</h4>
<asp:DataGrid id="ExlDataGrid" width="100%" HeaderStyle-BackColor="#999999" ForeColor="#333333" runat="server"></asp:DataGrid>
</body>
</html>
给你一个我昨天改别人的一个导excel入sql数据库的我正用的程序,是button1按钮一点就开始导入,我导的是excel是第二三colum..如下:
private void Button1_Click(object sender, System.EventArgs e)
{
string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = 'D:/ExportToExcel/excel/test.xls';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection (mystring);
OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",cnnxls);
DataSet myDs =new DataSet();
myDa.Fill(myDs);
if(myDs.Tables[0].Rows.Count > 0)
{
string strSql = "";
string CnnString="Provider=SQLOLEDB;database=testnews;server=(local);uid=sa;pwd=";
OleDbConnection conn =new OleDbConnection(CnnString);
conn.Open ();
OleDbCommand myCmd =null;
for(int i=0; i<myDs.Tables[0].Rows.Count; i++)
{
strSql="insert into news(title,body) values ('";
strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "')";
try
{
myCmd=new OleDbCommand(strSql,conn);
myCmd.ExecuteNonQuery();
Label8.Text = "<script language=javascript>alert('数据导入成功.');</script>";
}
catch
{
Label8.Text = "<script language=javascript>alert('数据导入失败.');</script>";
}
}
conn.Close();
}
}
}
"Provider=Microsoft.Jet.Oledb.4.0;Data Source=Excel 文件物理路径 + ";Extended Properties=Excel 8.0";
DataAdapter对象中的SQL语句应为:"Select 字段列表 From [工作表名$]"
具体示例:
<%@ Import NameSpace="System.Data"%>
<%@ Import NameSpace="System.Data.OleDb"%>
<script Language="C#" runat="server">
void Page_Load(Object sender,EventArgs e)
{
string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Server.MapPath("Book1.xls") + ";Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter adp = new OleDbDataAdapter("Select * from [Sheet1$]",conn);
DataSet ds = new DataSet();
adp.Fill(ds,"Book1");
ExlDataGrid.DataSource = ds.Tables["Book1"].DefaultView;
ExlDataGrid.DataBind();
}
</script>
<html>
<body>
<h4 align="center">读取Excel文件</h4>
<asp:DataGrid id="ExlDataGrid" width="100%" HeaderStyle-BackColor="#999999" ForeColor="#333333" runat="server"></asp:DataGrid>
</body>
</html>
给你一个我昨天改别人的一个导excel入sql数据库的我正用的程序,是button1按钮一点就开始导入,我导的是excel是第二三colum..如下:
private void Button1_Click(object sender, System.EventArgs e)
{
string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = 'D:/ExportToExcel/excel/test.xls';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection (mystring);
OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",cnnxls);
DataSet myDs =new DataSet();
myDa.Fill(myDs);
if(myDs.Tables[0].Rows.Count > 0)
{
string strSql = "";
string CnnString="Provider=SQLOLEDB;database=testnews;server=(local);uid=sa;pwd=";
OleDbConnection conn =new OleDbConnection(CnnString);
conn.Open ();
OleDbCommand myCmd =null;
for(int i=0; i<myDs.Tables[0].Rows.Count; i++)
{
strSql="insert into news(title,body) values ('";
strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "', '";
strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "')";
try
{
myCmd=new OleDbCommand(strSql,conn);
myCmd.ExecuteNonQuery();
Label8.Text = "<script language=javascript>alert('数据导入成功.');</script>";
}
catch
{
Label8.Text = "<script language=javascript>alert('数据导入失败.');</script>";
}
}
conn.Close();
}
}
}