使用起来很简单,就不多说了,只把代码贴出来
package com.yinbo.satisfy.web.struts;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
//import com.lowagie.text.List;
import java.util.List;
import com.yinbo.satisfy.service.satisfy.SatisfyManage;
import com.yinbo.satisfy.service.sysmanage.BranchManage;
import com.yinbo.satisfy.service.sysmanage.ParamManage;
import com.yinbo.satisfy.util.CommonUtils;
import com.yinbo.satisfy.vo.Branch;
import com.yinbo.satisfy.vo.Suggestion;
import com.yinbo.satisfy.vo.SysParam;
import java.io.*;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class QuestionAction extends Action {
private SatisfyManage satisfyManage;
private BranchManage branchManage;
private ParamManage paramManage;
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String excelFileName = "c:\testaaa.xls";
//////////////////////
String branchId = request.getParameter("branchId");
String level = request.getParameter("level");
List list = satisfyManage.getQuestionData(branchId, level);
String result[][] = new String[list.size()][5];
for(int i=0; i<list.size(); i++) {
Suggestion sug = (Suggestion)list.get(i);
result[i][0]=sug.getSugContent();
///System.out.println(sug.getSugContent());
}
//添加所在单位
Branch branch = branchManage.getBranchById(branchId);
String branchName = branch.getBranchName();
if(CommonUtils.isEmpty(branchName)) branchName = "所有机构";
//添加级别
SysParam param = paramManage.getParam("3", level);
String paramName = param.getParamName();
if(CommonUtils.isEmpty(paramName)) paramName = "所有级别";
try {
String strFileName = "question.xls";
strFileName = java.net.URLEncoder.encode(strFileName, "UTF-8");//处理中文文件名的问题
strFileName = new String(strFileName.getBytes("UTF-8"),"GBK");//处理中文文件名的问题
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment; filename=" + strFileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//创建可写入的Excel工作薄
//WritableWorkbook book = Workbook.createWorkbook(new File(excelFileName));
WritableWorkbook book = Workbook.createWorkbook(baos);
// 写工作表名字
WritableSheet sheet = book.createSheet("XXX建议", 0);
//此处可以设置列的宽度
int column = 0;
sheet.setColumnView(column++, 30);
sheet.setColumnView(column++, 30);
sheet.setColumnView(column++, 30);
sheet.setColumnView(column++, 30);
sheet.addCell(new Label(0, 0, "建议(" + branchName + ")(" + paramName + ")")); // 增加excel单元格
//sheet.addCell(new Label(1, 0, "部门名称")); // 增加excel单元格
//sheet.addCell(new Label(2, 0, "部门类型")); // 增加excel单元格
//sheet.addCell(new Label(3, 0, "综合成绩")); // 增加excel单元格
// result = getSelectResultWithStringArray(ds, sql);
int rows = result.length;
if (rows != 0) {
int cols = result[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sheet.addCell(new Label(j, i + 1, result[i][j]));
}
}
}
book.write(); // 写入byte输出流数组
book.close(); // jxl对象关闭
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
return null;
}
public SatisfyManage getSatisfyManage() {
return satisfyManage;
}
public void setSatisfyManage(SatisfyManage satisfyManage) {
this.satisfyManage = satisfyManage;
}
public BranchManage getBranchManage() {
return branchManage;
}
public void setBranchManage(BranchManage branchManage) {
this.branchManage = branchManage;
}
public ParamManage getParamManage() {
return paramManage;
}
public void setParamManage(ParamManage paramManage) {
this.paramManage = paramManage;
}
}