jxlexcel:java excel读写组件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 QuestionAction extends Action {

private SatisfyManage satisfyManage;
private BranchManage branchManage;
private ParamManage paramManage;

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @ 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 = String[list.size][5];
for( i=0; i<list.size; i) {
Suggestion sug = (Suggestion)list.get(i);
result[i][0]=sug.getSugContent;
///.out.prln(sug.getSugContent);
}

//添加所在单位
Branch branch = branchManage.getBranchById(branchId);
String branchName = branch.getBranchName;
(CommonUtils.isEmpty(branchName)) branchName = "所有机构";

//添加级别
SysParam param = paramManage.getParam("3", level);
String paramName = param.getParamName;
(CommonUtils.isEmpty(paramName)) paramName = "所有级别";

try {
String strFileName = "question.xls";
strFileName = java.net.URLEncoder.encode(strFileName, "UTF-8");//处理中文文件名问题
strFileName = String(strFileName.getBytes("UTF-8"),"GBK");//处理中文文件名问题
response.re;
response.CharacterEncoding("UTF-8");
response.ContentType("application/vnd.ms-excel");
response.Header("Content-Disposition","attachment; filename=" + strFileName);
ByteArrayOutputStream baos = ByteArrayOutputStream;

//创建可写入Excel工作薄
//WritableWorkbook book = Workbook.createWorkbook( File(excelFileName));
WritableWorkbook book = Workbook.createWorkbook(baos);

// 写工作表名字
WritableSheet sheet = book.createSheet("XXX建议", 0);

//此处可以设置列宽度
column = 0;
sheet.ColumnView(column, 30);
sheet.ColumnView(column, 30);
sheet.ColumnView(column, 30);
sheet.ColumnView(column, 30);

sheet.addCell( Label(0, 0, "建议(" + branchName + ")(" + paramName + ")")); // 增加excel单元格
//sheet.addCell( Label(1, 0, "部门名称")); // 增加excel单元格
//sheet.addCell( Label(2, 0, "部门类型")); // 增加excel单元格
//sheet.addCell( Label(3, 0, "综合成绩")); // 增加excel单元格

// result = getSelectResultWithStringArray(ds, sql);
rows = result.length;
(rows != 0) {
cols = result[0].length;
for ( i = 0; i < rows; i) {
for ( j = 0; j < cols; j) {
sheet.addCell( Label(j, i + 1, result[i][j]));
}
}
}

book.write; // 写入输出流
book.close; // jxl对象关闭

ServletOutputStream out = response.getOutputStream;
baos.writeTo(out);
out.flush;

} catch (IOException e) {
e.prStackTrace;
} catch (WriteException e) {
e.prStackTrace;
}

null;
}

public SatisfyManage getSatisfyManage {
satisfyManage;
}

public void SatisfyManage(SatisfyManage satisfyManage) {
this.satisfyManage = satisfyManage;
}

public BranchManage getBranchManage {
branchManage;
}

public void BranchManage(BranchManage branchManage) {
this.branchManage = branchManage;


}

public ParamManage getParamManage {
paramManage;
}

public void ParamManage(ParamManage paramManage) {
this.paramManage = paramManage;
}

}
Tags:  excel组件 javajxl java读写excel jxlexcel

延伸阅读

最新评论

发表评论