导出DBF,并且提供下载 .

原文: 导出DBF,并且提供下载 .

导出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/

你可能感兴趣的:(下载)