string filepath = FileUploadExcel.PostedFile.FileName;//取得路径
if (filepath != "")
{
string filename = filepath.Substring(filepath.LastIndexOf("//") + 1);//文件名称带后缀名
string name = filename.Substring(0, filename.Length - 4);//文件名不带后缀名
string fileEx = filepath.Substring(filepath.LastIndexOf(".") + 1);//后缀名
if (string.Equals(fileEx, "xls"))
{
string newName = Server.MapPath("App_Data//") + DateTime.Now.ToString("hhmmss") + ".xls";
FileUploadExcel.SaveAs(newName);
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + newName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1';";
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
OleDbDataAdapter oda = new OleDbDataAdapter("select F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11 from [Sheet1$]", conn);
DataSet ds = new DataSet();
oda.Fill(ds, "aa");
conn.Close();
ds.Tables[0].Rows.RemoveAt(0);//移除标题
ds.Tables[0].Rows.RemoveAt(ds.Tables[0].Rows.Count - 3);
ds.Tables[0].Rows.RemoveAt(ds.Tables[0].Rows.Count - 2);
ds.Tables[0].Rows.RemoveAt(ds.Tables[0].Rows.Count - 1);
gvInfor.DataSource = ds.Tables[0];
gvInfor.DataBind();
rsn.FillgvInfo(ds,"1",gvInfor);
File.Delete(newName);
}
else
{
rsn.showmessage("File type is not correct!", this.Page);
return;
}
}
else
{
rsn.showmessage("please select file to import!", this.Page);
return;
}
导入Excel到GridView
最新推荐文章于 2019-08-03 10:01:04 发布