傳統 excel 導入 與 導出代碼

  /// <summary>
        /// 導出
        /// </summary>
        /// <param name="ds"></param>
        protected void OracelInPutExcel(DataSet ds)
        {
            string DataTiem = System.DateTime.Now.Date.ToString("yyyy-MM-dd");
            string outputFileName = "INF_HGPRICE_" + DataTiem + ".xls";
            int i = ds.Tables[0].Rows.Count;
            DataTable DT = ds.Tables[0];
            //生成將要存放結果的Excel文件的名稱
            string NewFileName = DataTiem + ".xls";
            //轉換爲物理路徑
            NewFileName = Server.MapPath("config/" + NewFileName);
            //根據模板正式生成該Excel文件
            File.Copy(Server.MapPath("config/INF_HGPRICE.xls"), NewFileName, true);
            //建立指向該Excel文件的數據庫連接
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NewFileName + ";Extended Properties='Excel 8.0;'";
            OleDbConnection Conn = new OleDbConnection(strConn);
            //打開連接,爲操作該文件做準備
            Conn.Open();
            OleDbCommand Cmd = new OleDbCommand("", Conn);
            foreach (DataRow DR in DT.Rows)
            {
                string XSqlString = "insert into [INF_HGPRICE$]";
                XSqlString += "([公司編碼],[公司名稱],[互供料名稱],[同一地區互供價],[不同地區互供價],[發佈日期]) values(";
                XSqlString += "'" + DR["COMPCODE"] + "',";
                XSqlString += "'" + DR["COMPANYNAME"] + "',";
                XSqlString += "'" + DR["PRODUCT_NAME"] + "',";
                XSqlString += "'" + DR["PRICE1"] + "',";
                XSqlString += "'" + DR["PRICE2"] + "',";
                XSqlString += "'" + DR["PUBLISHDATE"] + "')";
                Cmd.CommandText = XSqlString;
                Cmd.ExecuteNonQuery();
            }
            //操作結束,關閉連接
            Conn.Close();
            //打開要下載的文件,並把該文件存放在FileStream中
            System.IO.FileStream Reader = System.IO.File.OpenRead(NewFileName);
            //文件傳送的剩餘字節數:初始值爲文件的總大小
            long Length = Reader.Length;

            Response.Buffer = false;
            Response.AddHeader("Connection", "Keep-Alive");
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(outputFileName));
            Response.AddHeader("Content-Length", Length.ToString());

            byte[] Buffer = new Byte[10000];  //存放欲發送數據的緩衝區
            int ByteToRead;  //每次實際讀取的字節數

            while (Length > 0)
            {
                //剩餘字節數不爲零,繼續傳送
                if (Response.IsClientConnected)
                {
                    //客戶端瀏覽器還打開着,繼續傳送
                    ByteToRead = Reader.Read(Buffer, 0, 10000);  //往緩衝區讀入數據
                    Response.OutputStream.Write(Buffer, 0, ByteToRead); //把緩衝區的數據寫入客戶端瀏覽器
                    Response.Flush();  //立即寫入客戶端
                    Length -= ByteToRead;  //剩餘字節數減少
                }
                else
                {
                    //客戶端瀏覽器已經斷開,阻止繼續循環
                    Length = -1;
                }
            }

            //關閉該文件
            Reader.Close();
            //刪除該Excel文件
            File.Delete(NewFileName);
        }

 

 

 

 

 

//////----------------------------------導入---------------------------------------

 

 /// <summary>
        /// 導入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInPut_Click(object sender, EventArgs e)
        {
            InputExcel(gvINF_HgPrice);
            BindData();
        }
        private void InputExcel(GridView gridview)
        {
            try
            {
                string strConn;
                string filename = this.txtFileName.Text.Replace("&", "//");
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= '" + filename + "';" + "Extended Properties=/"Excel 8.0;IMEX=1;/"";

                string sqlselect = " SELECT 公司編碼 as COMPCODE,公司名稱 as COMPANYNAME,互供料名稱 as PRODUCT_NAME,同一地區互供價 as PRICE1," +
                                          " 不同地區互供價 as PRICE2,發佈日期 as PUBLISHDATE " +
                                          " FROM [INF_HGPRICE$]";
                OleDbDataAdapter myCommand = new OleDbDataAdapter(sqlselect, strConn);
                DataSet myDataSet = new DataSet();

                myCommand.Fill(myDataSet);
                int j = myDataSet.Tables[0].Rows.Count;
                List<string> sqlList = new List<string>();
                string sql = string.Empty;

                DataSet ds = (DataSet)ViewState["ds"];
                Decimal Price1 = 0;
                Decimal Price2 = 0;

                for (int i = 0; i < myDataSet.Tables[0].Rows.Count; i++)
                {
                    string compcode = myDataSet.Tables[0].Rows[i]["COMPCODE"].ToString();
                    string companyname = myDataSet.Tables[0].Rows[i]["COMPANYNAME"].ToString();
                    string product_name = myDataSet.Tables[0].Rows[i]["PRODUCT_NAME"].ToString();
                    if (myDataSet.Tables[0].Rows[i]["PRICE1"].ToString() != "")
                    {
                        Price1 = Convert.ToDecimal(myDataSet.Tables[0].Rows[i]["PRICE1"]);
                    }
                    else
                    {
                        Price1 = 0;
                    }
                    if (myDataSet.Tables[0].Rows[i]["PRICE2"].ToString() != "")
                    {
                        Price2 = Convert.ToDecimal(myDataSet.Tables[0].Rows[i]["PRICE2"]);
                    }
                    else
                    {
                        Price2 = 0;
                    }
                    string publishdate = myDataSet.Tables[0].Rows[i]["PUBLISHDATE"].ToString();

                    if (ds.Tables[0].Select("COMPCODE ='" + compcode + "' and  PUBLISHDATE = '" + publishdate + "'").Length > 0)
                    {
                        sql = "update PPMS_INF_HGPRICE set PRICE1 =" + Price1 + ",PRICE2 =" + Price2 + " " +
                              "  where COMPCODE ='" + compcode + "' and  PUBLISHDATE = to_date('" + publishdate + "','yyyy-mm-dd hh24:mi:ss')";

                    }
                    else
                    {
                        sql = "insert into PPMS_INF_HGPRICE(COMPCODE,PRODUCT_ID,PRODUCT_NAME,PRICE1,PRICE2,PUBLISHDATE) " +
                          " values ('" + compcode + "','P_SNY','"+product_name+"," + Price1 + "', " +
                          " " + Price2 + ",to_date('" + publishdate + "','yyyy-mm-dd hh24:mi:ss'))";
                    }
                    sqlList.Add(sql);
                }

                logic.CrudepriceExcel(sqlList);
                this.ClientScript.RegisterStartupScript(this.GetType(), "input", "<script>alert('導入成功')</script>");

            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "input", "<script>alert('" + ex.Message + "')</script>");
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章