小弟初学者要错一个 C#winForm程序做一个导入,导出Excel表,有幸看到您的帖子,424662508请教您,谢谢啦
不知道你解决了没有!我最近刚做了一个关于Excel导入和导出的小程序。
下面是我代码:
#region
/// <summary>
/// Excel导出数据
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private DataSet ImportFormExcel(string path)
{
DataSet oDataSet = new DataSet();
try
{
//string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0";
string strConn = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(oDataSet, "table1");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return oDataSet;
}
#endregion
下面是读取dataSet中的数据
private void btnOutPutGroup_Click(object sender, EventArgs e)
{
DataSet dataSet = new DataSet();
if (this.txtGroupPath.Text != string.Empty)
{
string path = this.txtGroupPath.Text;
dataSet = ImportFormExcel(path);
}
else
{
MessageBox.Show("Please select Output group Excel");
return;
}
List<ReadExcel.ProductGroup> groupList = new List<ReadExcel.ProductGroup>();
if (dataSet != null)
{
for (int i = 0; i < dataSet.Tables[0].Rows.Count; i++)
{
ProductGroup groups = new ProductGroup();
//取出Excel表中的数据
groups.ProductGroupCode = dataSet.Tables[0].Rows[i][0].ToString();
groups.zh_GroupName = dataSet.Tables[0].Rows[i][1] is DBNull ? string.Empty : (string)dataSet.Tables[0].Rows[i][1];
groups.en_GroupName = dataSet.Tables[0].Rows[i][2] is DBNull ? string.Empty : (string)dataSet.Tables[0].Rows[i][2];
groups.zh_GroupDescription = dataSet.Tables[0].Rows[i][3] is DBNull ? string.Empty : (string)dataSet.Tables[0].Rows[i][3];
groups.en_GroupDescription = dataSet.Tables[0].Rows[i][4] is DBNull ? string.Empty : (string)dataSet.Tables[0].Rows[i][4];
groups.zh_GroupWebLabel = dataSet.Tables[0].Rows[i][5] is DBNull ? string.Empty : (string)dataSet.Tables[0].Rows[i][5];
groupList.Add(groups);
}
//下面你可以对数据进行操作了 如果还不明白 联系我:645683132.说明你加我的意图
温馨提示:内容为网友见解,仅供参考
Warning: Invalid argument supplied for foreach() in /www/wwwroot/aolonic.com/skin/templets/default/contents.html on line 45
相似回答