java

位置:IT落伍者 >> java >> 浏览文章

使用java生成excel功能实现


发布日期:2018年02月14日
 
使用java生成excel功能实现

jsp页面

发送请求地址sp_createExceldo

public String createExcel() {

try {

HttpServletResponse response = ServletActionContextgetResponse();

OutputStream os = responsegetOutputStream();

responsereset();

responsesetContentType(application/msexcel;charset=gbk); //设置响应格式

//设置响应头即报表头

responsesetHeader(Contentdisposition attachment;filename= + new String(业务受理量统计报表getBytes() iso) + xls);

HSSFWorkbook hwb = new HSSFWorkbook();

HSSFSheet sheet = hwbcreateSheet(new Sheet);

hwbsetSheetName( 业务受理量统计);

HSSFRow row = sheetcreateRow((short) );

sheetcreateFreezePane( );//设置第一行显示内容

createCell(hwb row (short) 业务类型);

createCell(hwb row (short) 操作名称);

createCell(hwb row (short) 数量);

createCell(hwb row (short) 收取金额);

createCell(hwb row (short) 工号);

//busBeans列表为要显示的数据

for (int i = ; i <= busBeanssize(); i++) {

HSSFRow row = sheetcreateRow((short) i);

for (int j = ; j < ; j++) {

if (j == ) {

createCell(hwb row (short) j busBeansget(i )getStatName());

} else if (j == ) {

createCell(hwb row (short) j busBeansget(i )getFunName());

} else if (j == ) {

createCell(hwb row (short) j busBeansget(i )getCountNum());

} else if (j == ) {

createCell(hwb row (short) j busBeansget(i )getMoney());

} else if (j == ) {

createCell(hwb row (short) j busBeansget(i )getLoginNo());

}

}

hwbwrite(os);

osclose();

return createSuccess;

} catch (Throwable e) {

eprintStackTrace();

thisforwardError(egetMessage());

}

return fail;

}

public void createCell(HSSFWorkbook wb HSSFRow row short col String val) {

HSSFCell cell = rowcreateCell(col);

cellsetCellValue(val);

HSSFCellStyle cellStyle = wbcreateCellStyle();

cellStylesetAlignment(HSSFCellStyleALIGN_CENTER_SELECTION);

cellsetCellStyle(cellStyle);

}

上一篇:Java高级编程之displayTag学习摘要

下一篇:Java高级日期概念二