求解答,如何解决服务器无法在发送 HTTP 标头之后设置内容类型问题

嗯哼a 2016-12-19 04:41:48
代码如下。。。额额。。。求大神指教。。。我想实现同时导出多个excel文件功能(不打包)然后就报错了



[PostAuthorityAttribute]
public string btnExcel(FormCollection form, string Tname)
{
string a = "";
try
{
if (HttpContext.Request["year"] == null || HttpContext.Request["year"] == "")
return null;
int depid = Convert.ToInt32(Session["DepartId"]);
int userid = Convert.ToInt32(Session["userid"]);
int year = Convert.ToInt32(HttpContext.Request["year"]);
string sql = "";
string modelPath = "";
int index = 0;
string[] arr = Tname.Split(',');
for (int i = 0; i < arr.Length; i++)
{
switch (arr[i])
{
case "利润预算表":
sql = " SELECT [year],[month],[yysr],[zyywsr],[qtywsr] ,[yycb] ,[zyywcb],[qtywcb] ,[yysjjfj] ,[xsfy],[glfy] ,[cwfy] ,[zcjzss],[gyjzbdsy],[tzsy] ,[lhtzsy] ,[yylr],[yywsr],[yywzc],[lrze] ,[sdsfy],[jlr] FROM V_Lrys_2016 where depid=" + depid + " and ( (years=" + year + " and months not in (1,2,3,-4)) or (years=" + Convert.ToInt32(year + 1) + " and months in (1,2,3,-4)) )";
modelPath = "Sclrysb.xls";
index = 1;
break;
case "现金流量预算表":
sql = " SELECT [year],[month],[tglwj],[sfhf],[jyhdxj],[jynbxjlr] ,[jyxjlr],[sjlrlr],[qtlr],[jyxjlrxj],[jslwj],[zgj],[zfsf],[zfqt],[jynbxjlc],[jylc],[sjlc],[qtlc],[jyxjlcxj],[jyhdje],[shxj],[qdxj],[czje],[czzgsje],[sdxj],[tznbxjlr],[tzxjlrxj],[gjxj],[tzxj],[qdzgsxj],[zfqtxj],[tznbxjlc],[tzxjlcxj],[tzje],[xstzj],[jkj],[qtczj],[cznbxjlr],[czxjlrxj],[chj],[fpj],[czqtj],[cznbxjlc],[czxjlcxj],[czhdje],[djwzje],[qcdjwye],[qmdjwye] FROM [Planning].[dbo].[V_Xjllb_2016] where zt = 0 and depid =" + depid + " and ( year=" + year + " or year=" + Convert.ToInt32(year + 1) + " or year='')";
sql += " AND (([year] = " + year + " AND [month] IN (4,5,6,7,8,9,10,11,12)) OR ([year] = " + year + "+1" + " AND [month] IN(1,2,3)) or ( [month] IN ('第一季度小计','第二季度小计','第三季度小计','第四季度小计','年度合计')))";
modelPath = "TXjllysfb_2016.xls";
index = 2;
break;
case "资产负债预算表":
sql = " SELECT [year],[month] ,[hbzj] ,[jyxjrzc],[yspj],[yszk],[yfkx],[yslx],[ysgl],[qtysk],[ch],[ynndqdfldzc],[ddfy],[qtldzc],[dclldzcsy],[nbwl],[ldzchj],[kgcsjrzc],[cyzdqtz],[cqysk] ,[cqgqtz],[tzxfdc],[gdzcyz],[ljzj],[gdzcjz],[zjgc] ,[gcwz] ,[gdzcql] ,[scxswzc] ,[yqzc] ,[wxzc] ,[ljtx] ,[wxzcjz] ,[kfzc] ,[sy] ,[cqdtfy] ,[dysdszc] ,[qtfldzc] ,[fldzchj] ,[zczj] ,[dqjk] ,[jyxjrfz] ,[yfpj] ,[yfzk] ,[yskx] ,[yfzgxc] ,[yjsf] ,[yflx] ,[yfgl] ,[qtyfk] ,[ytfy] ,[ynndqdfldfz] ,[qtldfz] ,[ldfzhj] ,[cqjk] ,[yfzj] ,[cqyfk] ,[zxyfk] ,[yjfz] ,[dysdsfz] ,[qtfldfz] ,[fldfzhj] ,[fzhj] ,[sszb] ,[zbgj] ,[kcg],[zxcc] ,[yygj],[wfplr] ,[gsymgssyzqyhj] ,[ssgdqy],[syzqyhj] ,[fzhsyzqyhj] ,[zczeyfzhsyzqyzj] ,[zcfzbhxjllb],[zcfzbhlrb] FROM V_Tzcfys_2016 WHERE depid=" + depid + " and ( (years=" + year + " and months not in (1,2,3,-4)) or (years=" + Convert.ToInt32(year + 1) + " and months in (1,2,3,-4)) )";
modelPath = "Tzcfzys_2016.xls";
index = 2;
break;
}
DataTable dt = DbHelperSQL.Query(sql.ToString()).Tables[0];

string excelName =arr[i] + DateTime.Now.ToString("yyyyMMddHHmmssms") + ".xls";
NpoiHelper npoi = new NpoiHelper();
npoi.ToExcel2(dt, modelPath, excelName, index);
a = "Success";
}
}
catch (Exception ex)
{
LogHelper.Log(ex);
}
return a;




字数超过了。。。回复里面贴代码。。。
...全文
612 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
嗯哼a 2016-12-21
  • 打赏
  • 举报
回复
木有人哎。。。
  • 打赏
  • 举报
回复
你是不是多次发送http了。 HttpContext context = HttpContext.Current;是怎么来的,你应该发送后重新开一个连接。
u010194608 2016-12-19
  • 打赏
  • 举报
回复
路过帮顶~~
嗯哼a 2016-12-19
  • 打赏
  • 举报
回复
求大神。。。不要沉啊。。。。
嗯哼a 2016-12-19
  • 打赏
  • 举报
回复
错误图片
嗯哼a 2016-12-19
  • 打赏
  • 举报
回复

npoi类代码

public void ToExcel2(DataTable dt, string ModelName, string ExcelName, int index)
{
try
{
//配置文件获取模板路径
string modelpath = ConfigurationManager.AppSettings["ModelPath"].ToString();
//配置文件中获取下载地址
string downlodpath = ConfigurationManager.AppSettings["DownLodPath"].ToString();
//判断是否存在当天的文件夹,如果不存在则创建
string downpath = HttpContext.Current.Server.MapPath(Path.Combine(downlodpath, DateTime.Now.ToString("yyyyMMdd")));
if (Directory.Exists(downpath) == false)
{
Directory.CreateDirectory(downpath);
}
//模板文件
string TempletFileName = HttpContext.Current.Server.MapPath(Path.Combine(modelpath, ModelName));
//导出文件
string ReportFileName = Path.Combine(downpath, ExcelName);
FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
ISheet ws = hssfworkbook.GetSheet("Sheet1");
//添加WorkSheet里的数据
if (ExcelName.Substring(0, 6).Equals("税金预算附表"))
{
CreateDataMethod2(ws, dt, index);
}
else
{
CreateDataMethod(ws, dt, index);
}
// CreateDataMethod(ws, dt, index);
ws.ForceFormulaRecalculation = true;
using (FileStream filess = System.IO.File.OpenWrite(ReportFileName))
{
hssfworkbook.Write(filess);
}
//导出excel
FileStream fs = new FileStream(ReportFileName, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];

fs.Read(bytes, 0, bytes.Length);
fs.Close();

HttpContext context = HttpContext.Current;
context.Response.Charset = "UTF-8";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(ExcelName));
//context.Response.BufferOutput = true;//设置输出缓冲
// context.Response.Write(fs.Length.ToString());//新加入
//context.Response.AddHeader("Content-Length", bytes.Length.ToString());//新加入
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
// fs.Close();
}
catch (Exception ex)
{
throw ex;
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧