java导出数据EXCEL的工具类(以spring-webmvc-4.0.4jar为基础)
一、springboot中可以使用
pom文件
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
工具类代码
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;
import java.util.Map; public class ObjectExcel { public static void buildExcelDocument(Map<String, Object> model,String fileName,HttpServletResponse response) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
fileName=fileName+DateUtils.getDateTime("HHmmss")+".xls";
HSSFSheet sheet;
HSSFCell cell;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"ISO-8859-1"));
sheet = workbook.createSheet("sheet1"); List<String> titles = (List<String>) model.get("titles");
int len = titles.size();
HSSFCellStyle headerStyle = workbook.createCellStyle(); //标题样式
headerStyle.setAlignment(HorizontalAlignment.CENTER); headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont headerFont = workbook.createFont(); //标题字体
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short)11);
headerStyle.setFont(headerFont);
short width = 20,height=25*20;
sheet.setDefaultColumnWidth(width);
HSSFRow row = sheet.createRow(0);
for(int i=0; i<len; i++){ //设置标题
String title = titles.get(i);
cell = row.createCell(i);
cell.setCellStyle(headerStyle);
cell.setCellValue(title);
}
sheet.getRow(0).setHeight(height);
HSSFCellStyle contentStyle = workbook.createCellStyle(); //内容样式
contentStyle.setAlignment(HorizontalAlignment.CENTER);
List<Map<String,Object>> varList = (List<Map<String,Object>>) model.get("varList");
int varCount = varList.size();
for(int i=0; i<varCount; i++){
HSSFRow rows = sheet.createRow(i+1);
Map<String,Object> vpd = varList.get(i);
/* int j=0;
for(String key : vpd.keySet()) {
HSSFCell cells = rows.createCell(j);
cells.setCellStyle(contentStyle);
cells.setCellValue(String.valueOf(vpd.get(key)));
j++;
}*/
for(int j=0;j<len;j++){
String varstr = vpd.get("var"+(j+1)) != null ? String.valueOf(vpd.get("var"+(j+1))) : "";
HSSFCell cells = rows.createCell(j);
cells.setCellStyle(contentStyle);
cells.setCellValue(varstr);
} }
OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
workbook.close();
} }
调用案例
二、基于springmvc
1.本工具类继承于 spring-webmvc-4.0.4jar文件心中的一个类 AbstractExcelView
2.代码如下
package com.skjd.util; import java.util.Date;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView; import com.skjd.util.PageData;
import com.skjd.util.Tools;
/**
* 导出到EXCEL
* 类名称:ObjectExcelView.java
* 类描述:
* @author FH
* 作者单位:
* 联系方式:
* @version 1.0
*/
public class ObjectExcelView extends AbstractExcelView{ @Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
Date date = new Date();
String filename = Tools.date2Str(date, "yyyyMMddHHmmss");
HSSFSheet sheet;
HSSFCell cell;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls");
sheet = workbook.createSheet("sheet1"); List<String> titles = (List<String>) model.get("titles");
int len = titles.size();
HSSFCellStyle headerStyle = workbook.createCellStyle(); //标题样式
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont headerFont = workbook.createFont(); //标题字体
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerFont.setFontHeightInPoints((short)11);
headerStyle.setFont(headerFont);
short width = 20,height=25*20;
sheet.setDefaultColumnWidth(width);
for(int i=0; i<len; i++){ //设置标题
String title = titles.get(i);
cell = getCell(sheet, 0, i);
cell.setCellStyle(headerStyle);
setText(cell,title);
}
sheet.getRow(0).setHeight(height); HSSFCellStyle contentStyle = workbook.createCellStyle(); //内容样式
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
List<PageData> varList = (List<PageData>) model.get("varList");
int varCount = varList.size();
for(int i=0; i<varCount; i++){
PageData vpd = varList.get(i);
for(int j=0;j<len;j++){
String varstr = vpd.getString("var"+(j+1)) != null ? vpd.getString("var"+(j+1)) : "";
cell = getCell(sheet, i+1, j);
cell.setCellStyle(contentStyle);
setText(cell,varstr);
} } } }
3.特此送上案例如下
/**
* 导出数据
* @return
* @throws Exception
*/
@RequestMapping(value="/excel")
public ModelAndView excel(Page page) throws Exception{
logBefore(logger, "导出数据");
ModelAndView mv = this.getModelAndView();
PageData pd = new PageData();
pd = this.getPageData();
page.setPd(pd);
Map<String,Object> dataMap = new HashMap<String,Object>();
List<String> titles = new ArrayList<String>();
titles.add("订单号");
titles.add("乘客姓名");
titles.add("用户手机号");
titles.add("状态");
titles.add("类型");
titles.add("司机姓名");
titles.add("司机手机号");
titles.add("车牌号");
titles.add("订单创建时间");
titles.add("订单结束时间");
titles.add("里程");
titles.add("费用");
dataMap.put("titles", titles);
List<PageData> orderList = orderService.datalist(page);
List<PageData> varList = new ArrayList<PageData>();
for(int i=0;i<orderList.size();i++){
PageData vpd = new PageData();
vpd.put("var1", orderList.get(i).getString("ordernumber"));
vpd.put("var2", orderList.get(i).getString("customer_name"));
vpd.put("var3", orderList.get(i).getString("customer_phone"));
if(orderList.get(i).get("order_status")!=null){
if("0".equals(orderList.get(i).get("order_status").toString())){vpd.put("var4", "生成订单");}
if("1".equals(orderList.get(i).get("order_status").toString())){vpd.put("var4", "司机已接单");}
if("2".equals(orderList.get(i).get("order_status").toString())){vpd.put("var4", "确认上车");}
if("3".equals(orderList.get(i).get("order_status").toString())){vpd.put("var4", "行程结束");}
if("4".equals(orderList.get(i).get("order_status").toString())){vpd.put("var4", "完成订单");}
if("5".equals(orderList.get(i).get("order_status").toString())){vpd.put("var4", "到达乘客上车点");}
if("99".equals(orderList.get(i).get("order_status").toString())){vpd.put("var4", "取消订单");}
} if(orderList.get(i).get("order_type")!=null){ if("1".equals(orderList.get(i).get("order_type").toString())){vpd.put("var5", "实时专车");}
if("2".equals(orderList.get(i).get("order_type").toString())){vpd.put("var5", "预约专车");}
if("3".equals(orderList.get(i).get("order_type").toString())){vpd.put("var5", "送机");}
if("4".equals(orderList.get(i).get("order_type").toString())){vpd.put("var5", "接机");}
} vpd.put("var6", orderList.get(i).getString("name"));
vpd.put("var7", orderList.get(i).getString("phone"));
vpd.put("var8", orderList.get(i).getString("license_number")); if(orderList.get(i).get("create_time")!=null){vpd.put("var9", orderList.get(i).get("create_time").toString());} if(orderList.get(i).get("end_time")!=null){vpd.put("var10", orderList.get(i).get("end_time").toString());} /*vpd.put("ordernumber", value);*/
if(orderList.get(i).get("actual_price")!=null){vpd.put("var12", orderList.get(i).get("actual_price").toString());} varList.add(vpd);
}
dataMap.put("varList", varList);
ObjectExcelView erv = new ObjectExcelView();
mv = new ModelAndView(erv,dataMap);
return mv; }
4.加上截图进行说明
java导出数据EXCEL的工具类(以spring-webmvc-4.0.4jar为基础)的更多相关文章
- java导出数据Excel总结
//创建获取到JFileChooser的文件名的JTextField public JTextField getTextField(Container c){ JTextField textField ...
- Excel导入工具类兼容xls和xlsx
package com.bj58.finance.platform.operation.provider.util; import org.apache.log4j.Logger; import or ...
- java实现Excel定制导出(基于POI的工具类)
我的需求: 项目中有一些工程表格需要导出,设计到行列合并,定制样式,原有工具类冗余,内聚性强.所以想写一个可以随意定制excel的工具类,工具类满足需求: 对于常用的工程表格有模板格式,可以任意插拔. ...
- POI导入导出excel(附工具类)
关于POI导出excel的功能我在前面的文章已经写过了,POI导出excel的三种方式 , 导出表格数据到excel并下载(HSSFWorkbook版) ,本篇文章主要是将导入导出功能进一步地封装,在 ...
- 使用Apache poi来编写导出excel的工具类
在JavaWeb开发的需求中,我们会经常看到导出excel的功能需求,然后java并没有提供操作office文档的功能,这个时候我们就需要使用额外的组件来帮助我们完成这项功能了. 很高兴Apache基 ...
- 下载数据到Excel,工具类
使用反射将model数据下载到Excel中 package test.upload.utils; import java.lang.reflect.Method; import java.math.B ...
- java里poi操作excel的工具类(兼容各版本)
转: java里poi操作excel的工具类(兼容各版本) 下面是文件内具体内容,文件下载: import java.io.FileNotFoundException; import java.io. ...
- Java导出数据行写入到Excel表格:基于Apache POI
Java导出数据行写入到Excel表格:基于Apache POI import java.io.File; import java.io.FileOutputStream; import org.ap ...
- 记一次 Java 导出大批量 Excel 优化
常用的excel导出方案,详情见Spring Boot 入门(十二):报表导出,对比poi.jxl和esayExcel的效率,其中jxl.esayEscel 底层都是基于 poi,它们仅仅是对 poi ...
随机推荐
- luogu P2113 看球泡妹子
2333 这么水的蓝题 f[i][j] 表示看了i场比赛,小♀红的什么东西为j时小♂明的什么值 强行压维蛤蛤 剩下的转移很简单(注意i的循环顺序从后往前,01背包) (具体见代码) #include& ...
- python技巧 列表推导
val = [expression for value in collection if condition] 等价于 val = []for value in collection: if c ...
- shiroWeb项目-授权(十一)
使用PermissionsAuthorizationFilter 在applicationContext-shiro.xml中配置url所对应的权限. 测试流程: 1.在applicationCont ...
- SLAM学习资料汇总
转自 http://www.cnblogs.com/wenhust/ 书籍: 1.必读经典 Thrun S, Burgard W, Fox D. <Probabilistic robotic ...
- SpringBoot缓存
(1).使用@EnableCaching注解开启基于注解的缓存 package cn.coreqi; import org.springframework.boot.SpringApplication ...
- k64 datasheet学习笔记4---Memory Map
1.前言 本文主要介绍K64地址空间的映射 2. System Memory Map 3. K64地址映射 4. Armv7m地址映射 4.1 Armv7M.System地址段(0XE0000000~ ...
- MySQL 误操作后数据恢复(update,delete忘加where条件)【转】
在数据库日常维护中,开发人员是最让人头痛的,很多时候都会由于SQL语句 写的有问题导致服务器出问题,导致资源耗尽.最危险的操作就是在做DML操作的时候忘加where条件,导致全表更新,这是作为运维或者 ...
- Python创建、删除桌面、启动组快捷方式的例子分享
一.Python创桌面建快捷方式的2个例子 例子一: 代码如下: import osimport pythoncomfrom win32com.shell import shell from w ...
- 提高delete效率方法
1 open c_1; loop fetch c_1 bulk collect into t2 limit 100000; exit when c_1%notfound ...
- hostapd 和 wap_supplicant
hostapd : user space daemon for access points, including, e.g., IEEE 802.1X/WPA/EAP Authenticator fo ...