using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; using System.Data.OleDb; using System.Data.SqlClient; namespace DSWeb { public partial class ImportDemo : System.Web.UI.Page { private HttpFileCollection files; private int fileCount; protected void Page_Load(object sender, EventArgs e) { } protected void btn_import_Click(object sender, EventArgs e) { string Extension = ""; string SavedName = ""; string upname = ""; string savedpath = ""; string realname = ""; string virtualname = ""; string path = ""; string OppPath = "TempFiles"; OppPath += "/"; string AbsPath = Server.MapPath(OppPath); files = HttpContext.Current.Request.Files; if (!Directory.Exists(Server.MapPath(OppPath))) { Directory.CreateDirectory(Server.MapPath(OppPath)); } for (fileCount = 0; fileCount < files.Count; fileCount++) { HttpPostedFile postedFile = files[fileCount]; string fileName; fileName = System.IO.Path.GetFileName(postedFile.FileName); if (fileName != string.Empty) { Extension = System.IO.Path.GetExtension(fileName); ////取上传文件的文件名 upname = System.IO.Path.GetFileName(fileName); //系统自动产生的日期型文件名 SavedName = string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now); ////把文件保存到UploadFile文件中 dvResult.InnerHtml += AbsPath + SavedName + upname; postedFile.SaveAs(AbsPath + SavedName + upname); dvResult.InnerHtml += " true"; virtualname += OppPath + SavedName + upname + ","; realname += upname + ","; path = savedpath; string strOldDb = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="; dvResult.InnerHtml += strOldDb + AbsPath + SavedName + upname; OleDbConnection OleCon = new OleDbConnection(strOldDb+AbsPath + SavedName + upname); OleDbDataAdapter OleDAp = new OleDbDataAdapter("SELECT * FROM [Sheet1$] ", OleCon); DataSet ds = new DataSet(); OleDAp.Fill(ds); dvResult.InnerHtml += " true"; this.GridView1.DataSource = ds; this.GridView1.DataBind(); } } } protected void btn_in_Click(object sender, EventArgs e) { //若此处未加逻辑判断,如果excel文件不存在则程序将会报异常 if (System.IO.File.Exists(MapPath("TempFile"))) { //OleDbConnection OleCon = new OleDbConnection(ExcelStr + MapPath("TempFile")); //OleDbDataAdapter OleDAp = new OleDbDataAdapter("SELECT prgName,PlayTime,prgColumn FROM [Sheet1$] ", OleCon); //DataSet ds = new DataSet(); //OleDAp.Fill(ds); //string prgName, playTime, prgColumn, sSQL; //SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlString"]); //conn.Open(); //SqlCommand cmd; //try //{ // foreach (DataRow dr in ds.Tables[0].Rows) // { // prgName = dr["prgName"].ToString(); // playTime = dr["PlayTime"].ToString(); // prgColumn = dr["prgColumn"].ToString(); // //sSQL = "Insert Into Ax_Program (prgName,PlayTime,prgColumn,prgComment) Values ('" + prgName + "','" + playTime + "','" + prgColumn + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "Excel导入')"; // cmd = new SqlCommand(sSQL, conn); // cmd.CommandType = CommandType.Text; // cmd.ExecuteNonQuery(); // cmd.Dispose(); // } //} //catch (Exception) //{ // Response.Write(";"); // return; //} //finally //{ // conn.Close(); // conn.Dispose(); //} //Response.Write(";"); //ExlDataGrid.Visible = false; //OpenAndBindNew(); //lbWarning.Visible = false; //lbWarningS.Visible = true; //lbWarningS.Text = "本此操作导入的节目信息"; } else { Response.Write(";"); } } } }