二級考試:javaexcel讀寫組件jxl使用

字號:

使用起來很簡單,就不多說了,只把代碼貼出來
    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    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();
    //創(chuàng)建可寫入的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;