回复 3楼 yimaoqian198
//把excel数据读入dataset返回l数据集
private DataSet xsldata()
{
OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。
openFile.Filter = ("Excel 文件(*.xls)|*.xls");//后缀名。
if (openFile.ShowDialog() == DialogResult.OK)
{
string filename = openFile.FileName;
int index = filename.LastIndexOf("\\");//截取文件的名字
filename = filename.Substring(index + 1);
//这是读取excel链接,目前只支持2003
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFile.FileName + ";Extended Properties='Excel 8.0;IMEX=1'";
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
string strCom = "SELECT * FROM [sheet1$]";
Conn.Open();
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[sheet1$]");
Conn.Close();
return ds;
}
else
return null;
}
#region 上传客户资料
/// <summary>
/// 上传客户资料
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 上传客户信息表ToolStripMenuItem_Click(object sender, EventArgs e)
{
//将读取excel后的数据转换成dataset
DataSet ds = xsldata();
if (ds != null)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
using (SqlConnection conn = new SqlConnection(DBhelper.constring))
{
StringBuilder bu = new StringBuilder();
bu.AppendLine("insert LenovoKeHuInfo (number,C_name,L_area,L_name,zhiwei,phone1,phone2,QQorE_mail,zhuyingleirong,hezuoyixiang,yonghudengji,beizhu)");
bu.AppendLine("values");
bu.AppendFormat("('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')",
ds.Tables[0].Rows[i][0],
ds.Tables[0].Rows[i][1],
ds.Tables[0].Rows[i][2],
ds.Tables[0].Rows[i][3],
ds.Tables[0].Rows[i][4],
ds.Tables[0].Rows[i][5],
ds.Tables[0].Rows[i][6],
ds.Tables[0].Rows[i][7],
ds.Tables[0].Rows[i][8],
ds.Tables[0].Rows[i][9],
ds.Tables[0].Rows[i][10],
ds.Tables[0].Rows[i][11]
);
SqlCommand comm = new SqlCommand(bu.ToString(), conn);
conn.Open();
comm.ExecuteNonQuery();
}
}
}
}
#endregion
麻烦帮忙看一下,,谢谢