如何读取excel表格中的数据??? 网上随便一搜,代码一大把各种方式都可以实现...刚刚还做了用ADODB把EXCEL当数据库读的代码... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考: ASP.NET中Excel数据批量导入导出SQL Server protected void btnUpload_Click(object sender, EventArgs e) { DataSet ds = GetExcelData(); InsertDB(ds); } /// <summary> /// 该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表,此用Sheet1; /// </summary> /// <param name="ds">ds</param> private void InsertDB(DataSet ds) { SqlConnection _con = new SqlConnection(@"Data Source=STKWX028\SQLEXPRESS;Initial Catalog=Library;Integrated Security=True"); SqlCommand cmd = new SqlCommand(); cmd.Connection = _con; StringBuilder sb = new StringBuilder(); if (ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sb.Append(" INSERT INTO bookInfo(bookID,bookName,author,publisher,price,readerID,readerType,bookStatus) VALUES('"); sb.Append(ds.Tables[0].Rows[i].ItemArray[0].ToString() + "','"); sb.Append(ds.Tables[0].Rows[i].ItemArray[1].ToString() + "','"); sb.Append(ds.Tables[0].Rows[i].ItemArray[2].ToString() + "','"); sb.Append(ds.Tables[0].Rows[i].ItemArray[3].ToString() + "','"); sb.Append(ds.Tables[0].Rows[i].ItemArray[4].ToString() + "','"); sb.Append(ds.Tables[0].Rows[i].ItemArray[5].ToString() + "','"); sb.Append(ds.Tables[0].Rows[i].ItemArray[6].ToString() + "','"); sb.Append(ds.Tables[0].Rows[i].ItemArray[7].ToString() + "' ) "); cmd.CommandText = sb.ToString(); } } _con.Open(); int j = cmd.ExecuteNonQuery(); _con.Close(); if (j > 0) { lblMessage.Text = "Insert into DB table Sucessfully!"; } } /// <summary> /// get data source from excel file /// </summary> /// <returns>dataset ds</returns> private DataSet GetExcelData() { DataSet ds = new DataSet(); string filePath = inputFile.PostedFile.FileName; string connStr03 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;"; ; string connStr07 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES'"; string queryStr = "SELECT * FROM [Sheet1$]"; OleDbConnection conn03 = new OleDbConnection(connStr03); OleDbConnection conn07 = new OleDbConnection(connStr07); if (inputFile.HasFile) { string fileExt = System.IO.Path.GetExtension(inputFile.FileName); if (fileExt == ".xls") { OleDbDataAdapter myAdapter = new OleDbDataAdapter(queryStr, conn03); myAdapter.Fill(ds); } else if (fileExt == ".xlsx") { OleDbDataAdapter myAdapter = new OleDbDataAdapter(queryStr, conn03); myAdapter.Fill(ds); } else { lblMessage.Text = "The file is not exist!"; } } return ds; } Refer here:http://www.cnblogs.com/insus/archive/2012/06/12/2545801.htmlhttp://www.cnblogs.com/insus/archive/2011/05/05/2037808.htmlhttp://www.cnblogs.com/insus/archive/2009/03/30/1425052.htmlhttp://www.cnblogs.com/insus/p/3814857.htmlhttp://www.cnblogs.com/insus/p/3779879.htmlhttp://www.cnblogs.com/insus/archive/2012/09/22/2698515.html 用二楼的办法在打开excel的链接时估计会遇到 the 'microsoft.ace.oledb.12.0' provider is not registered on the local machine可以尝试用npoi来操作excel的,后面写到excel应该很简单的 SVN的问题 做过http上传下载的人进来看看 请教字符串替换问题 选择LISTBOX1的字段 在LISTBOX2中显示相关的数据 在虚各种拟主机提供商那儿看看,发现SQL Server空间都要单独购买,而且很贵.有没有什么替代方法? VSS的使用问题 问一个关于框架的问题! 紧急问题,求助各位高手! 关于math函数的floor与round的区别 在ASP.NET中如何将空值(NULL)传递给存储过程? 多个Lable标签怎样依次绑定到数据库的同一字段的值并且显示出来 菜单样式
{
DataSet ds = GetExcelData();
InsertDB(ds);
} /// <summary>
/// 该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表,此用Sheet1;
/// </summary>
/// <param name="ds">ds</param>
private void InsertDB(DataSet ds)
{
SqlConnection _con = new SqlConnection(@"Data Source=STKWX028\SQLEXPRESS;Initial Catalog=Library;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = _con;
StringBuilder sb = new StringBuilder();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
sb.Append(" INSERT INTO bookInfo(bookID,bookName,author,publisher,price,readerID,readerType,bookStatus) VALUES('");
sb.Append(ds.Tables[0].Rows[i].ItemArray[0].ToString() + "','");
sb.Append(ds.Tables[0].Rows[i].ItemArray[1].ToString() + "','");
sb.Append(ds.Tables[0].Rows[i].ItemArray[2].ToString() + "','");
sb.Append(ds.Tables[0].Rows[i].ItemArray[3].ToString() + "','");
sb.Append(ds.Tables[0].Rows[i].ItemArray[4].ToString() + "','");
sb.Append(ds.Tables[0].Rows[i].ItemArray[5].ToString() + "','");
sb.Append(ds.Tables[0].Rows[i].ItemArray[6].ToString() + "','");
sb.Append(ds.Tables[0].Rows[i].ItemArray[7].ToString() + "' ) ");
cmd.CommandText = sb.ToString();
}
}
_con.Open();
int j = cmd.ExecuteNonQuery();
_con.Close();
if (j > 0)
{
lblMessage.Text = "Insert into DB table Sucessfully!";
}
} /// <summary>
/// get data source from excel file
/// </summary>
/// <returns>dataset ds</returns>
private DataSet GetExcelData()
{
DataSet ds = new DataSet();
string filePath = inputFile.PostedFile.FileName;
string connStr03 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;"; ;
string connStr07 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES'";
string queryStr = "SELECT * FROM [Sheet1$]";
OleDbConnection conn03 = new OleDbConnection(connStr03);
OleDbConnection conn07 = new OleDbConnection(connStr07);
if (inputFile.HasFile)
{
string fileExt = System.IO.Path.GetExtension(inputFile.FileName);
if (fileExt == ".xls")
{
OleDbDataAdapter myAdapter = new OleDbDataAdapter(queryStr, conn03);
myAdapter.Fill(ds);
}
else if (fileExt == ".xlsx")
{
OleDbDataAdapter myAdapter = new OleDbDataAdapter(queryStr, conn03);
myAdapter.Fill(ds);
}
else
{
lblMessage.Text = "The file is not exist!";
}
}
return ds;
}
http://www.cnblogs.com/insus/archive/2012/06/12/2545801.html
http://www.cnblogs.com/insus/archive/2011/05/05/2037808.html
http://www.cnblogs.com/insus/archive/2009/03/30/1425052.html
http://www.cnblogs.com/insus/p/3814857.html
http://www.cnblogs.com/insus/p/3779879.html
http://www.cnblogs.com/insus/archive/2012/09/22/2698515.html
the 'microsoft.ace.oledb.12.0' provider is not registered on the local machine可以尝试用npoi来操作excel的,后面写到excel应该很简单的