本来在服务器端看到是生成好好的,可是一到客户端就莫名其妙地少个括号少个逗号的,郁闷之极。
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();// close 15 context.Response.End();// end 16 } 17 18 19 public bool IsReusable 20 { 21 get 22 { 23 return false; 24 } 25 } 26 /// <summary> 27 /// 从Excel文件中读数据到DataSet 28 /// </summary> 29 /// <param name="strFileName">文件名</param> 30 /// <param name="strSheetName">表单名</param> 31 /// <param name="strReturnTableName">读取到DataSet中,数据表名称</param> 32 /// <returns>表单内容</returns> 33 public string DataSetFromExcelFile(string strFileName, string strSheetName, string strReturnTableName) 34 { 35 try 36 { 37 System.Collections.Generic.Dictionary<string, string> DicFPLX = new System.Collections.Generic.Dictionary<string, string>(); 38 DicFPLX.Add("增值税发票", "1"); 39 DicFPLX.Add("增值税普票", "2"); 40 DicFPLX.Add("普通发票", "3"); 41 System.Collections.Generic.Dictionary<string, string> DicBZ = new System.Collections.Generic.Dictionary<string, string>(); 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(" <row \"F_FPH\"=\"" + Row["发票号"].ToString() + "\",\"F_FPLX\"=\"" + F_FPLX + "\",\"F_FPLXMC\"=\"" + F_FPLXMC + "\"\""); 68 StrBuilder.Append(" \"F_BZID\"=\"" + F_BZID + "\",\"F_BZMC\"=\"" + F_BZMC + "\",\"F_SL\"=\"" + Row["税率"].ToString() + "\"\""); 69 StrBuilder.Append(" \"F_FPJE\"=\"" + Row["发票不含税金额"].ToString() + "\",\"F_JE\"=\"" + Row["发票含税金额"].ToString() + "\">"); 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();
写程序的人在编写由asp页面生成静态页面html的时候,如果同时生成大量页面,一定遇到过浏览器下方的进度条上显示着3%,6%,10%等缓慢增长的漫长等待过程。如果你知道Response.Flush和Response.Clear,那你就可以不用这样的等待了。每生成一个Html页面,就用Response.write立即返回一条信息,提示该条数据库记录已经生成html。
context.Response.Flush();将当前缓冲的所有输出强制发送到客户端。
context.Response.Clear();调用 Clear 方法以移除可能附加到响应的其他内容,然后将 BufferOutput 属性设置为 true,这样就可以在将任何内容发送到请求客户端之前先处理整个页。
context.Response.End();方法,并且所有页处理都将终止。