武汉北大青鸟鲁广校区java技术
packagecom.yinbo.satisfy.web.struts;
importjavax.servlet.ServletOutputStream;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
//importcom.lowagie.text.List;
importjava.util.List;
importcom.yinbo.satisfy.service.satisfy.SatisfyManage;
importcom.yinbo.satisfy.service.sysmanage.BranchManage;
importcom.yinbo.satisfy.service.sysmanage.ParamManage;
importcom.yinbo.satisfy.util.CommonUtils;
importcom.yinbo.satisfy.vo.Branch;
importcom.yinbo.satisfy.vo.Suggestion;
importcom.yinbo.satisfy.vo.SysParam;
importjava.io.*;
importjxl.Workbook;
importjxl.write.Label;
importjxl.write.WritableSheet;
importjxl.write.WritableWorkbook;
importjxl.write.WriteException;
publicclassQuestionActionextendsAction{
privateSatisfyManagesatisfyManage;
privateBranchManagebranchManage;
privateParamManageparamManage;
/**
*Methodexecute
*@parammapping
*@paramform
*@paramrequest
*@paramresponse
*@returnActionForward
*/
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
StringexcelFileName="c:\\testaaa.xls";
//////////////////////
StringbranchId=request.getParameter("branchId");
Stringlevel=request.getParameter("level");
Listlist=satisfyManage.getQuestionData(branchId,level);
Stringresult[][]=newString[list.size()][5];
for(inti=0;i<list.size();i++){
Suggestionsug=(Suggestion)list.get(i);
result[i][0]=sug.getSugContent();
///System.out.println(sug.getSugContent());
}
//添加所在单位
Branchbranch=branchManage.getBranchById(branchId);
StringbranchName=branch.getBranchName();
if(CommonUtils.isEmpty(branchName))branchName="所有机构";
//添加级别
SysParamparam=paramManage.getParam("3",level);
StringparamName=param.getParamName();
if(CommonUtils.isEmpty(paramName))paramName="所有级别";
try{
StringstrFileName="question.xls";
strFileName=java.net.URLEncoder.encode(strFileName,"UTF-8");//处理中文文件名的问题
strFileName=newString(strFileName.getBytes("UTF-8"),"GBK");//处理中文文件名的问题
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+strFileName);
ByteArrayOutputStreambaos=newByteArrayOutputStream();
//创建可写入的Excel工作薄
//WritableWorkbookbook=Workbook.createWorkbook(newFile(excelFileName));
WritableWorkbookbook=Workbook.createWorkbook(baos);
//写工作表名字
WritableSheetsheet=book.createSheet("XXX建议",0);
//此处可以设置列的宽度
intcolumn=0;
sheet.setColumnView(column++,30);
sheet.setColumnView(column++,30);
sheet.setColumnView(column++,30);
sheet.setColumnView(column++,30);
sheet.addCell(newLabel(0,0,"建议("+branchName+")("+paramName+")"));//增加excel单元格
//sheet.addCell(newLabel(1,0,"部门名称"));//增加excel单元格
//sheet.addCell(newLabel(2,0,"部门类型"));//增加excel单元格
//sheet.addCell(newLabel(3,0,"综合成绩"));//增加excel单元格
//result=getSelectResultWithStringArray(ds,sql);
introws=result.length;
if(rows!=0){
intcols=result[0].length;
for(inti=0;i<rows;i++){
for(intj=0;j<cols;j++){
sheet.addCell(newLabel(j,i+1,result[i][j]));
}
}
}
book.write();//写入byte输出流数组
book.close();//jxl对象关闭
ServletOutputStreamout=response.getOutputStream();
baos.writeTo(out);
out.flush();
}catch(IOExceptione){
e.printStackTrace();
}catch(WriteExceptione){
e.printStackTrace();
}
returnnull;
}
publicSatisfyManagegetSatisfyManage(){
returnsatisfyManage;
}
publicvoidsetSatisfyManage(SatisfyManagesatisfyManage){
this.satisfyManage=satisfyManage;
}
publicBranchManagegetBranchManage(){
returnbranchManage;
}
publicvoidsetBranchManage(BranchManagebranchManage){
this.branchManage=branchManage;
}
publicParamManagegetParamManage(){
returnparamManage;
}
publicvoidsetParamManage(ParamManageparamManage){
this.paramManage=paramManage;
}
}
本文标题:Java教程:Java Excel读写组件jxl使用,责任编辑:宏鹏,来源:武汉北大青鸟鲁广校区栏目,于2015-10-09 10:06:33发布于北大青鸟鲁广校区。武汉北大青鸟鲁广校区java技术 packagecom.yinbo.satisfy.web.struts; importjavax.servlet.ServletOutputStream; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.apache.struts.action
预约将免费领取7天课程体验卡
只为您方便就学
专业老师24小时1对1学习指导
定制专属于你的专属学习方案
微信号:17740513250
微信号:17740513250