jxl怎么导出excel2007

如题所述

public String exportxm() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
// String wid=request.getParameter("wid");
String[] headers = { "项目名称", "填报人", "填报时间", "合同额", "预计毛利", "项目计划完成时间", "所属部门"};
String[] columns = { "sernum", "personer.name", "times","xsjobes", "xsb", "plannedate", "subdept.name"};
Map<String, Object> values = new HashMap<String, Object>();
//Page<Worktype> page1 = new Page<Worktype>(40);
// 设置默认排序方式
if (!page.isOrderBySetted()) {
page.setOrderBy("id");
page.setOrder(Page.DESC);
}
page.setPageSize(9999);
page = manager.searchWork(page, values);
String title="项目管理表";
// 生成Excel文件.
Workbook wb = new ExcelExport(title, headers,columns, page)
.exportExcelWorkbook();
// 输出Excel文件.
HttpServletResponse response = Struts2Utils.getResponse();
response.setContentType(ServletUtils.EXCEL_TYPE);
ServletUtils.setFileDownloadHeader(response, "项目管理-"+accountManager.getcurrentUser().getName()+".xls");
wb.write(response.getOutputStream());
response.getOutputStream().flush();
return null;
}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
/**
* 设置让浏览器弹出下载对话框的Header.
*
* @param fileName 下载后的文件名.
*/
public static void setFileDownloadHeader(HttpServletResponse response, String fileName) {
try {
//中文文件名支持
String encodedfileName = new String(fileName.getBytes(), "ISO8859-1");
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\"");
} catch (UnsupportedEncodingException e) {
}
}
public static final String EXCEL_TYPE = "application/vnd.ms-excel";
、、、、、、、、、、、、、、、
/**
* 导出到excel
* @param title 标题
* @param headers 表格头
* @param result 数据
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public ExcelExport(String title,String[] headers,String[]columns, Page page){
this.title=title;
this.headers=headers;
this.result=transhashmap(columns, page.getResult());
}
/**
* 将listObject 转换为List<HashMap<Integer, Object>>
* @param columns
* @param olist
* @return
*/
private List<HashMap<Integer, Object>> transhashmap(String[] columns,List<Object> olist){
List<HashMap<Integer, Object>> result=Lists.newArrayList();
for(Object o:olist){
HashMap<Integer, Object> ht=this.transhashmap(columns, o);
result.add(ht);
}

return result;
}
/**
* 将object 转换为hashmap
* @param columns
* @param olist
* @return
*/
private HashMap<Integer, Object> transhashmap(String[] columns,Object o){
int col=0;
HashMap<Integer, Object> ht=new HashMap<Integer, Object>();
for(String column:columns){
//根据属性名称直接获取值
Object value=this.invokeGetterMethod(o, column);

ht.put(col,value);
col=col+1;
}
return ht;
}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网