匯出DBF,並且提供下載 [轉]

來源:互聯網
上載者:User

標籤:

匯出DBF,並且提供下載

                 #region Declare

                string mFilePath = MapPath("../DataTmp/");
                string mTableName = "WYKS";
                string mStrConn = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + mFilePath + @"/;Extended Properties=""dBASE IV;HDR=Yes;"";";

                System.Data.OleDb.OleDbConnection mOLDBConn = new System.Data.OleDb.OleDbConnection(mStrConn);
                if (System.IO.File.Exists(mFilePath + "//" + mTableName + ".DBF") == true)
                {
                    System.IO.File.Delete(mFilePath + "//" + mTableName + ".DBF");
                }
                #endregion Declare

                #region 建立DBF表檔案

                #region CREATE TABLE

                string sqlt = "CREATE TABLE " + mTableName + "(" +
                    "KSH varchar(18)," +
                    "KSCJ varchar(1)," +
                    "KYZK varchar(1)," +
                    "YYYD varchar(1)" +
                    ")";
                System.Data.OleDb.OleDbCommand OLDBComm = new System.Data.OleDb.OleDbCommand(sqlt, mOLDBConn);
                mOLDBConn.Open();
                OLDBComm.ExecuteNonQuery();
                OLDBComm.Dispose();
                mOLDBConn.Close();

                #endregion CREATE TABLE

                #region Delete TableDate

                System.Data.OleDb.OleDbCommand OLDBCommIn = new System.Data.OleDb.OleDbCommand("delete * from " + mTableName, mOLDBConn);
                mOLDBConn.Open();
                OLDBCommIn.ExecuteNonQuery();
                OLDBCommIn.Dispose();
                mOLDBConn.Close();

                #endregion Delete TableDate

                #endregion 建立DBF表檔案

                #region 匯出資料到DBF

                string mWhere = " 1=1";
                if (this.txtExamsPoint.Text.Length > 0)
                {
                    mWhere = mWhere + " And ExamsPoint = ‘" + this.txtExamsPoint.Text.Trim() + "‘";

                    if (this.txtExaminationRoom.Text.Trim().Length > 0)
                    {
                        mWhere = mWhere + " And ExaminationRoom In(Select ExaminationRoom From T_ExaminationRoom Where ExaminationRoomName = ‘" + this.txtExaminationRoom.Text.Trim() + "‘ And ExamsPoint = ‘" + this.txtExamsPoint.Text.Trim() + "‘)";
                    }
                }
                if (this.txtKSHStart.Text.Trim().Length > 0)
                {
                    mWhere = mWhere + " And KSH >= ‘" + this.txtKSHStart.Text.Trim() + "‘";
                }
                if (this.txtKSHEnd.Text.Trim().Length > 0)
                {
                    mWhere = mWhere + " And KSH <= ‘" + this.txtKSHEnd.Text.Trim() + "‘";
                }

                System.Data.DataSet mDSOralAcademic = new System.Data.DataSet();
                OralExam.Entity.T_OralAcademic mEOralAcademic = new OralExam.Entity.T_OralAcademic();

                mEOralAcademic.WhereCondition = mWhere;

                OralExam.Data.T_OralAcademic.pro_T_OralAcademic_SelectDynamic(mEOralAcademic, ref mDSOralAcademic, OralExam.BaseC.GlobeValues.ConnString);

                for (int i = 0; i < mDSOralAcademic.Tables[0].Rows.Count; i++)
                {
                    mEOralAcademic = OralExam.DataToEntity.T_OralAcademic.GetEntity(mDSOralAcademic, i);

                    string mSqlExport = "Insert Into " + mTableName + "(KSH,KSCJ,KYZK,YYYD)";
                    mSqlExport = mSqlExport + " Select ";
                    mSqlExport = mSqlExport + "‘" + mEOralAcademic.KSH + "‘ as KSH,";
                    mSqlExport = mSqlExport + "‘" + mEOralAcademic.AcademicCode + "‘ as KSCJ,";
                    mSqlExport = mSqlExport + "‘" + mEOralAcademic.OralStatus.ToString() + "‘ as KYZK,";
                    mSqlExport = mSqlExport + "‘" + mEOralAcademic.ToneStatus.ToString() + "‘ as YYYD";


                    System.Data.OleDb.OleDbCommand OLDBCommInsert = new System.Data.OleDb.OleDbCommand(mSqlExport, mOLDBConn);
                    mOLDBConn.Open();
                    OLDBCommInsert.ExecuteNonQuery();
                    OLDBCommInsert.Dispose();
                    mOLDBConn.Close();
                }
                #endregion 匯出資料到DBF

                #region 提供下載

                System.IO.FileStream fs = new System.IO.FileStream(mFilePath + "//" + mTableName + ".DBF", System.IO.FileMode.Open, System.IO.FileAccess.Read);

                byte[] b = new Byte[fs.Length];
                fs.Read(b, 0, b.Length);
                fs.Flush();
                fs.Close();

                //System.IO.File.Delete(SavePdfPath); 
                Response.Clear();
                Response.ClearHeaders();
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";      //ContentType;
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(mFilePath + "//" + mTableName + ".DBF", System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", b.Length.ToString());
                fs.Close();
                fs.Close();
                if (b.Length > 0)
                {
                    Response.OutputStream.Write(b, 0, b.Length);
                }

                Response.Flush();
                Response.End();

                #endregion 提供下載

其它相關:
      
轉自:http://www.cnblogs.com/jyshi/

匯出DBF,並且提供下載 [轉]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.