独自幸福快乐关注开始发胖的五征兆男人减肚腩用什么方法依依不舍孤独
写一个javaBean,利用JExcelApi来动态生成excel文档,我这里写一个最简单的,示意性的。复杂的你可能还要查询数据库什么的。
///////////////////////////va///////////////////////////////////////////
st;
.*;
importjxl方脸:见家长要有亲和力啊.*;
ite.*;
rmat.*;
il.*;
lor;
publicclassTest{
publicstaticvoidwriteExcel(OutputStreamos)throwsException{
itableWorkbookwwb=eateWorkbook(os);
itableSheetws=eateSheet("TestSheet1",0);
bellabelC=bel(0,0,"我爱中国");
dCell(labelC);
itableFontwfc=itableFont(IAL,
20,LD,false如果量化宽松结束使股票回购活动受抑制,
_UNDERLINE,EEN);
itableCellFormatwcfFC=itableCellFormat(wfc);
tBackground(D);
labelC=bel(6,0,"中国爱我",wcfFC);
dCell(labelC);
//写入Exel工作表
ite();
//关闭Excel工作薄对象
ose();
}
//最好写一个这样的main方法来测试一下你的这个class是否写好了。
publicstaticvoidmain(String[]args)throwsException{
Filef=newFile("s");
eateNewFile();
writeExcel(newFileOutputStream(f));
}
}
写一个jsp,来利用Test这个javabean输出excel文档。
///////////////////////////test_p//////////////////////////
这样就大功告成了,你用ie访问test_p就能在ie里面打开动态生成的excel文档了。一点乱码也没有。
也许有人会问:set();可不可以不要这一句,我的建议是一定要写,除非你能保证response的buffer里面没有别的东西。
还有人也许会问:我在jsp开头加上这一句,去掉tContentType("application/-excel");行不行?回答这个问题很简单,就是查看jsp服务器编译jsp后生成的java代码,如果改成这样,我的welogic7编译test_p后生成的java文件的示意性代码是这样的:
publicvoid_jspService(tpServletRequestrequest,
tpServletResponseresponse)Exception,
rvletException{
//declareandsetwell-knownvariables:
rvletConfigconfig=getServletConfig();
rvletContextapplication=tServletContext();
g_activeTag=null;
//variablesforTagextensionprotocol
Objectpage=this;
pWriterout;
geContextpageContext=
tDefaultFactory().getPageContext(this,
request,response,null,true,8192,true);
tHeader("Content-Type",
"application/-excel;charset=GBK");
out=tOut();
JspWriter_originalOut=out;
tpSessionsession=tSession(true);
try{//errorpagetryblock
tContentType("application/-excel;charset=GBK");
int("\r\n\r\n\r\n\r\n");
int("\r\n");
//[/test_p;Line:6]
set();//[/test_p;Line:7]
//tContentType("application/-excel");
//[/test_p;Line:8]
iteExcel(tOutputStream());//[/test_p;Line:9]
}catch(Throwable__ee){
while(out!=nullout!=_originalOut)out=pBody();
((geContextImpl)pageContext).handlePageException((Throwable)__ee);
}
//beforefinalclosebrace...
}
很明显,屏蔽tContentType("application/-excel");后,在iteExcel(tOutputStream());之前,set();之后没有设置responsecontenttype的正确类型,当然输出为乱码了。而正确输出excel的jsp的编译后源码是这样的:
publicvoid_jspService(tpServletRequestrequest,
tpServletResponseresponse)Exception,
rvletException
{
//declareandsetwell-knownvariables:
rvletConfigconfig=getServletConfig();
rvletContextapplication=tServletContext();
g_activeTag=null;
//variablesforTagextensionprotocol
Objectpage=this;
pWriterout;
geContextpageContext=
tDefaultFactory().getPageContext(
this,request,response,null,true,8192,true);
out=tOut();
JspWriter_originalOut=out;
tpSessionsession=tSession(true);
try{//errorpagetryblock
int("\r\n");
//[/test_p;Line:2]
set();//[/test_p;Line:3]
tContentType("application/-excel");//[/test_p;Line:4]
iteExcel(tOutputStream());//[/test_p;Line:5]
}catch(Throwable__ee){
while(out!=nullout!=_originalOut)out=pBody();
((geContextImpl)pageContext).handlePageException((Throwable)__ee);
}
//beforefinalclosebrace...
}
大家可以看到在set();之后,iteExcel(tOutputStream());之前正确的设置了response的输出内容。所以输出就正常了。
href="http://java.ccidnet.com/art/3559//_1.html" target=_blank>查看本文来源
来宾白癜病医院百色白癜风治疗费用辽阳白癜病医院- 上一页:独自幸福快乐卫生间装修要多少钱卫生间吊顶贵吗依依不舍孤独
- 下一页:泥鳅汤0
-
你曾经羡慕的人,在起初都貌似完美无缺
你以前喜欢的人,在先前都貌似完美无缺。即便当他逐渐四分五裂转化成一堆...
2023-11-02
-
用于哮喘脊髓损伤治疗的分子化合物
脑部伤害都会所致永久性老年人,从而受到限制现实生活活动,其主要原因是...
2023-10-12
-
英国央行助理经济学家:货币政策紧缩还没到头
英国国际货币基金组织助理经济学家Huw Pill回应,英国的汇率再加还有进一步...
2023-10-11
-
今后工业互联网产业规模迈过万亿大关
4月底19日,中华人民共和国财政部开幕新闻发布会,工业生产和智能化部简述...
2023-10-08
-
一鲸鱼搁浅慈溪象山海域!初步判断是抹香鲸,救援正在进行
19日凌晨8时许,三门县石浦镇铜瓦门大桥除此以外,右边山海域不远处,渔船...
2023-10-06
-
通威股份(600438.SH)2022-2024年员工持股计划已完成投资者购买 斥资约54.88
通威持股(600438.SH)发布日前,截至本日前披露日,新公司2022-2024年员工持股计...
2023-10-04