使用起来很简单,就不多说了,只把代码贴出来
- 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;
- }
- }