本来在服务器端看到是生成好好的,可是一到客户端就莫名其妙地少个括号少个逗号的,郁闷之极。
1 public void ProcessRequest(HttpContext context) 2 { 3 4 string FileName = context.Request["FileName"] ?? ""; 5 string FileDir = context.Request["FileDir"] ?? ""; 6 string BuildUpLoadFilePath = HttpContext.Current.Server.MapPath("SH_ZDFPXX.xls"); 7 string StrRuturn = DataSetFromExcelFile(BuildUpLoadFilePath, "sheet1", "MyTable"); 8 context.Response.Clear(); 9 context.Response.Charset = "utf-8";10 context.Response.Buffer = true;11 context.Response.ContentType = "text/plain";12 context.Response.Write(StrRuturn);13 context.Response.Flush();14 context.Response.Close();// close15 context.Response.End();// end16 }17 18 19 public bool IsReusable20 {21 get22 {23 return false;24 }25 }26 ///27 /// 从Excel文件中读数据到DataSet 28 /// 29 /// 文件名 30 /// 表单名 31 /// 读取到DataSet中,数据表名称 32 ///表单内容 33 public string DataSetFromExcelFile(string strFileName, string strSheetName, string strReturnTableName)34 {35 try36 {37 System.Collections.Generic.DictionaryDicFPLX = new System.Collections.Generic.Dictionary ();38 DicFPLX.Add("增值税发票", "1");39 DicFPLX.Add("增值税普票", "2");40 DicFPLX.Add("普通发票", "3");41 System.Collections.Generic.Dictionary DicBZ = new System.Collections.Generic.Dictionary ();42 DicBZ.Add("人民币", "1");43 System.Text.StringBuilder StrBuilder = new System.Text.StringBuilder("");44 //****打开数据库路径45 string strConn = @"provider=microsoft.jet.oledb.4.0;data source ='" + strFileName +46 "';Extended Properties='Excel 8.0;;HDR=Yes;IMEX=1;' ";47 string strSQL = String.Format("SELECT * FROM [{0}$] ", strSheetName);48 System.Data.OleDb.OleDbDataAdapter ExcelDA = new System.Data.OleDb.OleDbDataAdapter(strSQL, strConn);49 System.Data.DataSet Daset = new System.Data.DataSet();50 ExcelDA.Fill(Daset, strReturnTableName);51 ExcelDA.SelectCommand.Connection.Close();52 53 if (Daset != null && Daset.Tables[0].Rows.Count > 0)54 {55 56 StrBuilder.Append("");57 foreach (System.Data.DataRow Row in Daset.Tables[0].Rows)58 {59 string F_FPLX = "";60 string F_BZID = "";61 string F_FPLXMC = Row["发票类型"].ToString();62 string F_BZMC = Row["币种"].ToString();63 DicFPLX.TryGetValue(Row["发票类型"].ToString(), out F_FPLX);64 DicBZ.TryGetValue(Row["币种"].ToString(), out F_BZID);65 if (F_FPLX == null || F_FPLX == "") F_FPLXMC = "";66 if (F_BZID == null || F_BZID == "") F_BZMC = "";67 StrBuilder.Append(" ");70 }71 StrBuilder.Append("");72 }73 return StrBuilder.ToString();74 }75 catch (Exception e)76 {77 return "";78 }79 80 }81 }
1 public void ProcessRequest(HttpContext context) 2 { 3 4 string FileName = context.Request["FileName"] ?? ""; 5 string FileDir = context.Request["FileDir"] ?? ""; 6 string BuildUpLoadFilePath = HttpContext.Current.Server.MapPath("../" + FileDir + FileName); 7 string StrRuturn = DataSetFromExcelFile(BuildUpLoadFilePath, "sheet1", "MyTable"); 8 context.Response.Clear(); 9 context.Response.Charset = "utf-8";10 context.Response.ContentType = "text/plain";11 context.Response.Write(StrRuturn);12 context.Response.Flush();13 context.Response.End();14 context.Response.Close();15 16 }
先Response.End(),后再Response.Close(). 问题解决。
再加一些查找的资料:
来自于ideasky
1、
context.Response.Write("test");context.Response.End();2、context.Response.Clear();context.Response.Write("test");context.Response.Flush();context.Response.Close();写程序的人在编写由页面生成静态页面html的时候,如果同时生成大量页面,一定遇到过浏览器下方的进度条上显示着3%,6%,10%等缓慢增长的漫长等待过程。如果你知道Response.Flush和Response.Clear,那你就可以不用这样的等待了。每生成一个Html页面,就用Response.write立即返回一条信息,提示该条记录已经生成html。
context.Response.Flush();将当前缓冲的所有输出强制发送到客户端。
context.Response.Clear();调用 Clear 方法以移除可能附加到响应的其他内容,然后将 BufferOutput 属性设置为 true,这样就可以在将任何内容发送到请求客户端之前先处理整个页。context.Response.End();方法,并且所有页处理都将终止。