您的位置:首页 > 数据库

将Excel内容导入数据库(ASP.NET/C#)

2008-05-28 12:45 507 查看
思路:先上传XLS文件,然后用OleDB连接Excel文件,将Excel数据导入Dataset,再将Dataset数据导入数据库。

以下是主调入过程

string filename = string.Empty;

try

catch(Exception ex)

finally

private void ImportXlsToData(string fileName)

//上传Excel文件

private string UpdateXls(System.Web.UI.HtmlControls.HtmlInputFile inputfile)

//将Dataset的内容导入SQL

private bool AddDatasetToSQL(DataSet pds)

{

int ic,ir;

ic = pds.Tables[0].Columns.Count;

if (pds.Tables[0].Columns.Count < 7)

{

throw new Exception("导入Excel格式错误!Excel只有" + ic.ToString() + "列");

}

ir = pds.Tables[0].Rows.Count;

if (pds != null && pds.Tables[0].Rows.Count > 0)

{

for (int i = 0;i < pds.Tables[0].Rows.Count;i++)

{

Save(pds.Tables[0].Rows[i][0].ToString(),pds.Tables[0].Rows[i][1].ToString(),

pds.Tables[0].Rows[i][2].ToString(),pds.Tables[0].Rows[i][3].ToString(),

pds.Tables[0].Rows[i][4].ToString(),pds.Tables[0].Rows[i][5].ToString(),

pds.Tables[0].Rows[i][6].ToString());

}

}

else

{

throw new Exception("导入数据为空!");

}

return true;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: