c#如何关联excel里面的数据. 想把excel数据内容加载到 程序里. 应该怎么弄
2个回答
展开全部
可以使用Oledb连接,将excel文件数据读入到DataSet中以进行下一步操作。
代码如下:
public DataSet ExcelToDS(string Path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0;"; //读取2003版本使用这个连接
//string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=Excel 12.0;HDR=YES"; //读取2007以上版本使用这个连接
string strExcel = "";
strExcel = "select * from [Sheet1$]"; //这里假设读取Sheet1的内容
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet ds = new DataSet(); //实例化一个DataSet
myCommand.Fill(ds, "table1"); //将读取到的数据放到table1表中
return ds;
}
展开全部
C#导入Excel中的数据,可以编写个引用,程序如下:
openFileDialog1.Filter = "Execl files (*.xls)|*.xls";
openFileDialog1.FilterIndex = 0;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Title = "导入文件保存路径";
openFileDialog1.FileName = null;
openFileDialog1.ShowDialog();
string FileName = openFileDialog1.FileName;
if (FileName != "")
{
String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + "; Extended Properties=Excel 8.0;";
String sql = "SELECT * FROM [Sheet1$]"; OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
string[] total = dr[0].ToString().Split('-');
string server = total[0];
string ah = total[1].Substring(0, 1);
string strSql = "select * from JMFServername_list where Servername_realm=@server and AH=@ah";
SqlConnection sqlconn = new SqlConnection(strsql);
sqlconn.Open();
SqlCommand sqlcmd = new SqlCommand(strSql, sqlconn);
sqlcmd.Parameters.AddWithValue("@server", server);
sqlcmd.Parameters.AddWithValue("@ah", ah);
SqlDataReader dtr = sqlcmd.ExecuteReader();
if (dtr.Read())
{
continue;
}
string sqlQuery = "insert into JMFServername_list(Servername_realm,AH)values(@server,@AH)"; sqlcmd = new SqlCommand(sqlQuery, sqlconn);
sqlcmd.Parameters.AddWithValue("@server",server);
sqlcmd.Parameters.AddWithValue("@AH", ah);
sqlcmd.ExecuteNonQuery();
}
myDGV.DataSource = ds.Tables[0];
}
openFileDialog1.Filter = "Execl files (*.xls)|*.xls";
openFileDialog1.FilterIndex = 0;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Title = "导入文件保存路径";
openFileDialog1.FileName = null;
openFileDialog1.ShowDialog();
string FileName = openFileDialog1.FileName;
if (FileName != "")
{
String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + "; Extended Properties=Excel 8.0;";
String sql = "SELECT * FROM [Sheet1$]"; OleDbDataAdapter da = new OleDbDataAdapter(sql, connStr);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
string[] total = dr[0].ToString().Split('-');
string server = total[0];
string ah = total[1].Substring(0, 1);
string strSql = "select * from JMFServername_list where Servername_realm=@server and AH=@ah";
SqlConnection sqlconn = new SqlConnection(strsql);
sqlconn.Open();
SqlCommand sqlcmd = new SqlCommand(strSql, sqlconn);
sqlcmd.Parameters.AddWithValue("@server", server);
sqlcmd.Parameters.AddWithValue("@ah", ah);
SqlDataReader dtr = sqlcmd.ExecuteReader();
if (dtr.Read())
{
continue;
}
string sqlQuery = "insert into JMFServername_list(Servername_realm,AH)values(@server,@AH)"; sqlcmd = new SqlCommand(sqlQuery, sqlconn);
sqlcmd.Parameters.AddWithValue("@server",server);
sqlcmd.Parameters.AddWithValue("@AH", ah);
sqlcmd.ExecuteNonQuery();
}
myDGV.DataSource = ds.Tables[0];
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询