extjs 导出简单的excel表格
2016-02-29 14:20
591 查看
js代码:
this.returnJnlExportBtn = new Ext.Button({
text : "退单文件导出",
id : "returnJnl_export",
iconCls : "icon-excel",
hidden: false,
scope : this,
handler : this.exportHandler
});
@RequestMapping("/returnJnlExport1")
public void returnJnlExport1(HttpServletRequest request, HttpServletResponse response) throws IOException{
Map<String, Object> params = ServletUtils.getParametersStartingWith(request, null);
OutputStream outStream = null;
try{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet hssfSheet = workbook.createSheet("退单报表");
HSSFRow hssfRow = null;
HSSFCell hssfCell = null;
HSSFRichTextString textString = null;
HSSFCellStyle style = workbook.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
style.setWrapText(true);//自动换行
String columnTitle[] = {"退票流水号","结算划款通道","退单时间","退单原因","原收款户号","原收款账名","原联行行号",
"原开户行名","原账户标识(1对私/0对公)","交易金额","交易备注","商户号","商户名称","签约分公司","新收款户号",
"新收款账名","新联行行号","新开户行名","新账户标识(1对私/0对公)"};
//第一行(头)
hssfRow = hssfSheet.createRow(0);
hssfRow.setHeight((short)500);
for(int i=0; i<columnTitle.length; i++){
hssfSheet.setColumnWidth(i, 6000);
hssfCell = hssfRow.createCell(i);
textString = new HSSFRichTextString(columnTitle[i]);
hssfCell.setCellValue(textString);
//列头的样式
HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
HSSFFont columnHeadFont = workbook.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
columnHeadStyle.setBorderRight((short) 1);// 边框的大小
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
hssfCell.setCellStyle(columnHeadStyle);
}
//第二行(内容)
HSSFFont cfont = workbook.createFont();
cfont.setFontName("宋体");
cfont.setFontHeightInPoints((short) 10);
HSSFCellStyle contentstyle = workbook.createCellStyle();
contentstyle.setFont(cfont);
contentstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
contentstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
contentstyle.setWrapText(true);
contentstyle.setLeftBorderColor(HSSFColor.BLACK.index);
contentstyle.setBorderLeft((short) 1);
contentstyle.setRightBorderColor(HSSFColor.BLACK.index);
contentstyle.setBorderRight((short) 1);
contentstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
contentstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
contentstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
//数据集
List<Object[]> dataList = returnJnlFacade.returnJnlExport(params);
if(!dataList.isEmpty()){
for(int i=0; i<dataList.size(); i++){
Object[] object = dataList.get(i);
hssfRow = hssfSheet.createRow(i+1);
for(int j=0; j<columnTitle.length; j++){
hssfCell = hssfRow.createCell(j);
String v = object[j] == null ? null : object[j].toString();
/*if(column != 2&&column != 3){ //2,3行是表示金额的
hssfCell.setCellValue(v);
hssfCell.setCellStyle(strStyle);
}else{
HSSFDataFormat format = workbook.createDataFormat();
style.setDataFormat(format.getFormat("0.00"));
style.setAlignment((short) 3);//右对齐
hssfCell.setCellValue(Double.valueOf(v));
hssfCell.setCellStyle(style);
}*/
hssfCell.setCellValue(v);
hssfCell.setCellStyle(contentstyle);
}
}
}
String filename = new String("退单报表".getBytes("gb2312"), "ISO8859-1");
response.setContentType("application/vnd.ms-excel");
//当前日期的前一天
response.setHeader("content-disposition", "attachment;filename="+filename+DateUtils.getFrontDay(DateUtils.getCurrDate())+".xls");
outStream = response.getOutputStream();
workbook.write(outStream);
outStream.flush();
outStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
this.returnJnlExportBtn = new Ext.Button({
text : "退单文件导出",
id : "returnJnl_export",
iconCls : "icon-excel",
hidden: false,
scope : this,
handler : this.exportHandler
});
exportHandler : function() { this.returnJnlExportBtn.setDisabled(true); var params = this.form_query.getFormDatas(); formPost(params, "returnJnl/returnJnlExport1"); this.returnJnlExportBtn.setDisabled(false); },
/* 导出excel */ function formPost(jsonObj, url) { var oForm = document.createElement("form"); oForm.id = "sandpany-postForm"; oForm.name = "sandpany-postForm"; oForm.method = "post"; oForm.action = url; oForm.target = "_blank"; oForm.style.display = "none"; for (var prop in jsonObj) { var oInput = document.createElement("input"); oInput.name = prop; oInput.value = jsonObj[prop]; oForm.appendChild(oInput); } document.body.appendChild(oForm); oForm.submit(); }java poi 代码:
@RequestMapping("/returnJnlExport1")
public void returnJnlExport1(HttpServletRequest request, HttpServletResponse response) throws IOException{
Map<String, Object> params = ServletUtils.getParametersStartingWith(request, null);
OutputStream outStream = null;
try{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet hssfSheet = workbook.createSheet("退单报表");
HSSFRow hssfRow = null;
HSSFCell hssfCell = null;
HSSFRichTextString textString = null;
HSSFCellStyle style = workbook.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
style.setWrapText(true);//自动换行
String columnTitle[] = {"退票流水号","结算划款通道","退单时间","退单原因","原收款户号","原收款账名","原联行行号",
"原开户行名","原账户标识(1对私/0对公)","交易金额","交易备注","商户号","商户名称","签约分公司","新收款户号",
"新收款账名","新联行行号","新开户行名","新账户标识(1对私/0对公)"};
//第一行(头)
hssfRow = hssfSheet.createRow(0);
hssfRow.setHeight((short)500);
for(int i=0; i<columnTitle.length; i++){
hssfSheet.setColumnWidth(i, 6000);
hssfCell = hssfRow.createCell(i);
textString = new HSSFRichTextString(columnTitle[i]);
hssfCell.setCellValue(textString);
//列头的样式
HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
HSSFFont columnHeadFont = workbook.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
columnHeadStyle.setBorderRight((short) 1);// 边框的大小
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
hssfCell.setCellStyle(columnHeadStyle);
}
//第二行(内容)
HSSFFont cfont = workbook.createFont();
cfont.setFontName("宋体");
cfont.setFontHeightInPoints((short) 10);
HSSFCellStyle contentstyle = workbook.createCellStyle();
contentstyle.setFont(cfont);
contentstyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
contentstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
contentstyle.setWrapText(true);
contentstyle.setLeftBorderColor(HSSFColor.BLACK.index);
contentstyle.setBorderLeft((short) 1);
contentstyle.setRightBorderColor(HSSFColor.BLACK.index);
contentstyle.setBorderRight((short) 1);
contentstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
contentstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
contentstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
//数据集
List<Object[]> dataList = returnJnlFacade.returnJnlExport(params);
if(!dataList.isEmpty()){
for(int i=0; i<dataList.size(); i++){
Object[] object = dataList.get(i);
hssfRow = hssfSheet.createRow(i+1);
for(int j=0; j<columnTitle.length; j++){
hssfCell = hssfRow.createCell(j);
String v = object[j] == null ? null : object[j].toString();
/*if(column != 2&&column != 3){ //2,3行是表示金额的
hssfCell.setCellValue(v);
hssfCell.setCellStyle(strStyle);
}else{
HSSFDataFormat format = workbook.createDataFormat();
style.setDataFormat(format.getFormat("0.00"));
style.setAlignment((short) 3);//右对齐
hssfCell.setCellValue(Double.valueOf(v));
hssfCell.setCellStyle(style);
}*/
hssfCell.setCellValue(v);
hssfCell.setCellStyle(contentstyle);
}
}
}
String filename = new String("退单报表".getBytes("gb2312"), "ISO8859-1");
response.setContentType("application/vnd.ms-excel");
//当前日期的前一天
response.setHeader("content-disposition", "attachment;filename="+filename+DateUtils.getFrontDay(DateUtils.getCurrDate())+".xls");
outStream = response.getOutputStream();
workbook.write(outStream);
outStream.flush();
outStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
相关文章推荐
- js正则表达式小结
- json作为配置文件
- JSP页面中注释<!-- --> 和 <%-- --%>的区别
- D3 JS 库 - 用来展示数据分析的结果
- JS中typeof与instanceof的区别
- json字符串的封装(jackson框架)
- js实现快捷键绑定按钮点击事件
- js代码如何测试代码运行时间
- js运算符
- JavaScript函数的常见几种写法
- (.net)ajax+json+catch缓存+js控制页面动态生成div
- Javascript权威指南——第一章Javascript概述
- js事件模型与自定义事件
- Angular js 简介
- js遍历数组
- hammer.js 图片拖动
- js事件代理(委托)
- JS中Null与Undefined的区别(转载)
- Js之DOM(一)
- js中单引号和双引号的区别