使用WinForm测试
![](https://img-blog.csdnimg.cn/img_convert/8fdd30a4ffda64c21003a3c769c467b0.png)
//打标选择文件对话框
public string path = "";//文件路径
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
fd.Title = "选择文件";//选择框名称
fd.Filter = "xls files (*.xls)|*.xls";//选择文件的类型Xls
if (fd.ShowDialog() == DialogResult.OK)//文件路径
{
path = fd.FileName.Trim();
path = path.Replace("\\","/");
}
}
导入Excel表
string strExcel = "select * from [信息表$]";
信息表对应excel的sheet名称
public DataSet LoadDataExcel(string Path)
{
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Path + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "select * from [信息表$]";
OleDbDataAdapter myDa = new OleDbDataAdapter(strExcel,conn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
return myDs;
}
WinForm按钮
private void button2_Click(object sender, EventArgs e)
{
int i;
i = 0;
string temp=null;
DataSet ds = LoadDataExcel(path);
temp= ds.Tables[0].Rows[1].ItemArray[1].ToString();
//for (i = 0; i < 4; i++)
//{
// string a = ds.Tables[0].Rows[0].ItemArray[i].ToString();
// temp = a + "," + temp;
//}
MessageBox.Show(temp);
}
Rows[1]、ItemArray[1]为第二行、第二列数据
![](https://img-blog.csdnimg.cn/img_convert/f7ac46c8954e76c46e39c155cc307d90.png)
![](https://img-blog.csdnimg.cn/img_convert/7653bc05b838888d7225c132207f7171.png)