简单的java的导出excel的方法
来源:互联网 发布:mac安装adobe 编辑:程序博客网 时间:2024/05/16 14:28
@RequestMapping(params="exportExcel")
public void exportExcel(HttpServletRequest request,HttpServletResponse response){
OutputStream ouputStream = null;
try{
response.setContentType("application/vnd.ms-excel");
String bankCardId = request.getParameter("bankCardId");
String month = request.getParameter("month");
String sy = request.getParameter("sy");
String fileName = sy+month;
if(StringUtil.isEmpty(fileName)){
fileName="allData";
}
// 根据浏览器进行转码
if (BrowserUtils.isIE(request))
{
response.setHeader("content-disposition",
"attachment;filename="+ java.net.URLEncoder.encode(fileName,"UTF-8") + ".xls");
} else {
String newtitle = new String(fileName.getBytes("ISO8859-1"),
"UTF-8");
response.setHeader("content-disposition","attachment;filename=" + newtitle + ".xls");
}
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("编号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("日期");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("项目");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("金额");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("内容");
cell.setCellStyle(style);
List<Map<String,Object>> lists=dailyTotalAccountService.getFundList(bankCardId, month, sy);
if(lists.size()>0&&lists!=null){
BigDecimal money = null;
for(int i = 0; i < lists.size(); i++){
row = sheet.createRow((int) i + 1);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue((String)lists.get(i).get("number"));
row.createCell((short) 1).setCellValue((String)lists.get(i).get("submitTime"));
row.createCell((short) 2).setCellValue((String)lists.get(i).get("projectName"));
row.createCell((short) 3).setCellValue(lists.get(i).get("money").toString());
row.createCell((short) 4).setCellValue((String)lists.get(i).get("description"));
}
}
//写到输出流
ouputStream = response.getOutputStream();
wb.write(ouputStream);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(ouputStream !=null){
ouputStream.flush();
ouputStream.close();
}
}catch(Exception e){
e.printStackTrace();
}
public void exportExcel(HttpServletRequest request,HttpServletResponse response){
OutputStream ouputStream = null;
try{
response.setContentType("application/vnd.ms-excel");
String bankCardId = request.getParameter("bankCardId");
String month = request.getParameter("month");
String sy = request.getParameter("sy");
String fileName = sy+month;
if(StringUtil.isEmpty(fileName)){
fileName="allData";
}
// 根据浏览器进行转码
if (BrowserUtils.isIE(request))
{
response.setHeader("content-disposition",
"attachment;filename="+ java.net.URLEncoder.encode(fileName,"UTF-8") + ".xls");
} else {
String newtitle = new String(fileName.getBytes("ISO8859-1"),
"UTF-8");
response.setHeader("content-disposition","attachment;filename=" + newtitle + ".xls");
}
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(fileName);
//设置列宽
this.setSheetColumnWidth(sheet, 100);
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("编号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("日期");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("项目");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("金额");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("内容");
cell.setCellStyle(style);
List<Map<String,Object>> lists=dailyTotalAccountService.getFundList(bankCardId, month, sy);
if(lists.size()>0&&lists!=null){
BigDecimal money = null;
for(int i = 0; i < lists.size(); i++){
row = sheet.createRow((int) i + 1);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue((String)lists.get(i).get("number"));
row.createCell((short) 1).setCellValue((String)lists.get(i).get("submitTime"));
row.createCell((short) 2).setCellValue((String)lists.get(i).get("projectName"));
row.createCell((short) 3).setCellValue(lists.get(i).get("money").toString());
row.createCell((short) 4).setCellValue((String)lists.get(i).get("description"));
}
}
//写到输出流
ouputStream = response.getOutputStream();
wb.write(ouputStream);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(ouputStream !=null){
ouputStream.flush();
ouputStream.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 设置列宽
*
* @param sheet
*/
private void setSheetColumnWidth(HSSFSheet sheet, int count) {
for (int i = 0; i < count; i++) {
sheet.setColumnWidth(i, 25*256);
}
}
0 0
- java 导出excel封装了一个简单的导出方法。
- 简单的java的导出excel的方法
- java导出excel的方法
- JAVA导出EXCEL的方法
- java的excel导出的简单示例
- vfp导出excel的简单方法
- .NET导入导出excel的简单方法
- PHP导出Excel最简单的方法
- 简单的Excel导出公共方法类
- ASP 最简单导出excel的方法
- java导出excel,简单的excel样式编写,poi导出
- 导出excel的方法
- 导出excel的方法
- 导出excel的方法
- 导出excel的方法
- ExcelUtils -- excel模板引擎,导出excel最简单的方法
- 非常简单方便的导出Excel的方法
- 最简单的Excel导出
- 如何在IntelliJ IDEA中使用.ignore插件忽略不必要提交的文件
- 【C语言简单说】二十一:双重指针基础 (完结)
- Excel转XML
- NXP PN7150高性能全兼容NFC控制器解决方案
- SSL/TLS协议运行机制的概述
- 简单的java的导出excel的方法
- rk3288的固件刷写
- POJ 3302 Subsequence G++
- iOS沙盒机制
- idea 使用maven搭建SpringMVC报错
- Git 常用命令
- 指定位置下搜索文件(Python)
- 卷积神经网络(CNNs / ConvNets) 二
- 双远心镜头原理及选型方法(二)