菜鸟中的菜

导航

Excle的读取

Excle的读取
 string filename = Filefiel.PostedFile.FileName;
string fileType = Filefiel.FileName.Substring(Filefiel.FileName.LastIndexOf(".") + 1);
if (fileType.ToString().Trim() != "xls")
{
MessageBox.Show(Page, "请选择正确的格式!");
return;
}
try
{
OleDbConnection conn = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";Data Source=" + filename);
OleDbDataAdapter oda = new OleDbDataAdapter("select * from [sheet1$]", conn);
DataSet ds = new DataSet();
oda.Fill(ds);
list.Clear();
int j = 0;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
j++;
Temporary tp = new Temporary();
tp.id = j.ToString();
tp.txtYWMC = ds.Tables[0].Rows[i][0].ToString();
tp.txtYHXQ = ds.Tables[0].Rows[i][1].ToString();
tp.txtCheckMan = ds.Tables[0].Rows[i][2].ToString();
tp.txtCheckTime = ds.Tables[0].Rows[i][3].ToString();
tp.DDLBM = ds.Tables[0].Rows[i][4].ToString();
tp.txtWCSJ = ds.Tables[0].Rows[i][5].ToString();
tp.txtRemark = ds.Tables[0].Rows[i][6].ToString();
list.Add(tp);
}

GridViewAdd.DataSource = list;
GridViewAdd.DataBind();

}
catch (Exception ex)
{
//throw;

MessageBox.Show(Page, "请确定上传文件的格式!");
}

 

posted on 2012-04-02 17:48  菜中菜  阅读(202)  评论(0)    收藏  举报