码迷,mamicode.com
首页 > Web开发 > 详细

.net excel 转换成datatable,创建文件夹

时间:2016-01-18 10:34:14      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

  protected void Button9_Click(object sender, EventArgs e)
        {
            string path = "";
            path = FileUpload3.PostedFile.FileName;
            if (path == "")
            {
                string jss = "<script language=‘javascript‘ type=‘text/javascript‘>  alert(‘先选择文件‘)</script>";
                Response.Write(jss);

                return;
            }

            //保存到服务器的路径,没有excel创建 
            string serverpath = Server.MapPath(@"/excel");
            if (!Directory.Exists(serverpath))
            {
                Directory.CreateDirectory(serverpath);
            }
            serverpath += "/" + Guid.NewGuid().ToString() + FileUpload3.FileName;
            //确定上传文件
                FileUpload3.PostedFile.SaveAs(serverpath);
         

                DataTable dt11 = ExcelSheetName(serverpath);
                //string[] qa = GetExcelSheetNames(serverpath);
                List<string> qa = new List<string>();
                foreach (DataRow dr in dt11.Rows)
                {
                    if (dr[2].ToString().Contains("_")) {
                        continue;
                    }
                    qa.Add(dr[2].ToString().Replace("$",""));
                }
               
                for (int w = 0; w < qa.Count; w++)
                {
                    DataTable dt = ExcelDataSource(serverpath, qa[w]);
                    //ExcelEdit1.ExcelEdit1 m = new ExcelEdit1.ExcelEdit1();
                    //m.Opens(serverpath);
                    for (int i = 1; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["F2"].ToString() != "")
                        {
                            string sql = @"insert into ss_yb(YBID,PACK_CARD_CODE,PACK_NAME,SUM_INSURED,PREMIUM,PACK_SALE_PRICE,ADH_PERIOD,EW_PERIOD,REGIST_DATE,
VALID_DATE_FROM,VALID_DATE_TO,ADH_DATE_FROM,ADH_DATE_TO,EW_DATE_FROM,EW_DATE_TO,STATUS,REMARK,SERIAL_NO,MODEL_CODE,PRO_COLOR,SALE_PROVINCE,SALE_CITY,BUY_DATE,WARRANTY_PERIOD,
STA,BATCH,time1,CW,IMEIMEID,GUID,SNO,PAGE,CWZW,LOOK)";
                            sql += "values(EMP_SEQUENCE1.NEXTVAL,‘" + dt.Rows[i]["F2"].ToString() + "‘,‘" + dt.Rows[i]["F3"].ToString() + "‘,‘" + dt.Rows[i]["F4"].ToString() + "‘,‘" + dt.Rows[i]["F5"].ToString() + "‘,‘" + dt.Rows[i]["F6"].ToString() + "‘,‘" + dt.Rows[i]["F7"].ToString() + "‘,‘" + dt.Rows[i]["F8"].ToString() + "‘,‘" + dt.Rows[i]["F9"].ToString() + "‘,‘" + dt.Rows[i]["F10"].ToString() + "‘,‘" + dt.Rows[i]["F11"].ToString() + "‘,‘" + dt.Rows[i]["F12"].ToString() + "‘,‘" + dt.Rows[i]["F13"].ToString() + "‘,‘" + dt.Rows[i]["F14"].ToString() + "‘,‘" + dt.Rows[i]["F15"].ToString() + "‘,0,‘" + dt.Rows[i]["F17"].ToString() + "‘,‘" + dt.Rows[i]["F18"].ToString() + "‘,‘" + dt.Rows[i]["F19"].ToString() + "‘,‘" + dt.Rows[i]["F20"].ToString() + "‘,‘" + dt.Rows[i]["F21"].ToString() + "‘,‘" + dt.Rows[i]["F22"].ToString() + "‘,‘" + dt.Rows[i]["F23"].ToString() + "‘,‘" + dt.Rows[i]["F24"].ToString() + "‘,0,‘" + dt.Rows[i]["F26"].ToString() + "‘,sysdate,‘" + dt.Rows[i]["F28"].ToString() + "‘,‘" + dt.Rows[i]["F29"].ToString() + "‘,‘" + dt.Rows[i]["F30"].ToString() + "‘,‘" + dt.Rows[i]["F31"].ToString() + "‘,‘" + dt.Rows[i]["F32"].ToString() + "‘,‘" + dt.Rows[i]["F33"].ToString() + "‘,0)";
                            int c = DBHelper.ExecuteCommand(sql);
                        }
                    }
                }
           

            string jsss = "<script language=‘javascript‘ type=‘text/javascript‘>  alert(‘执行完成‘)</script>";
            Response.Write(jsss);
        }

        //将excel转换成datatable
        public DataTable ExcelDataSource(string filepath, string sheetname)//filepath(路径)   sheetname sheet(名称)
        {
            DataSet ds = new DataSet();
            string strConn;
            strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filepath + ";Extended Properties=‘Excel 12.0; HDR=No; IMEX=1‘";
            OleDbConnection conn = new OleDbConnection(strConn);
            OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "$]", strConn);
            oada.Fill(ds);
            DataTable dt = ds.Tables[0];
            conn.Close();
            conn.Dispose();
            return dt;
        }

 

.net excel 转换成datatable,创建文件夹

标签:

原文地址:http://www.cnblogs.com/110abcd/p/5138417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!