注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

古城风~~~

竹密无妨溪水过,天高不碍白云飞。这天下总有一份是属于我古城的天地!

 
 
 

日志

 
 

Java Excel读写组件jxl使用  

2008-12-30 12:34:50|  分类: java/jsp |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

资料来源:http://java.chinaitlab.com/code/771422.html

 

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;
}

}

  评论这张
 
阅读(423)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017