復制代碼 代碼如下:
if (this.FileUpload1.PostedFile != null)
{
string filename = this.FileUpload1.FileName.ToString();
string path = @Server.MapPath("../File/") + filename;
this.FileUpload1.PostedFile.SaveAs(path);
//讀取用戶上傳的Excle文件
string conn="Provider = Microsoft.Jet.OLEDB.4.0 ;Data Source ='"+path+"';Extended Properties=Excel 8.0";
OleDbConnection olecon = new OleDbConnection(conn);
olecon.Open();
//注意表名,打開Excel文件後,最底部分頁的Excle名字,
//默認是$Sheet1,$Sheet2,$Sheet3
string sql = "select * from [$Sheet1]";
OleDbDataAdapter oleda = new OleDbDataAdapter(sql,conn);
oleda.Fill(ds);
olecon.Close();
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
string file_name = "20091126002.xml";
string xml_path = @Server.MapPath("../Reports/xml/") + file_name;
ds.WriteXml(xml_path);
ds.Dispose();
}