ExportExcel(⽤poi导出Excel⽂件:⽤
ListMapString,Object》)
1.导⼊jar包(maven)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10.1</version>
</dependency>
2.ExportExcel⼯具类
package com.zhiyou100.kfs.util;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* ⽤poi导出Excel⽂件的⼯具类
*
* @author KFS
*
*/
public class ExportExcel {
/**
* ⽤poi导出Excel⽂件的静态⽅法
* @param list 数据:只能是List<Map<String, Object>>类型
* @param sheetname Excel的sheet名字
* @param filepath 保存⽂件的地址
* @throws IOException
* @throws IOException
*/
public void exportExcel(List<Map<String, Object>> list,String sheetname,String fileName,HttpServletResponse response) throws IOException {
//新建⼯作簿
HSSFWorkbook workbook=new HSSFWorkbook();
//创建Excel的sheet
HSSFSheet ateSheet(sheetname);
//从list任意⼀个Map对象⾥获取标题(字段名或属性名)放到sheet的第⼀⾏上,若第⼀条记录某字段值没有,则会没有该字段
Map<String, Object> (0);
int num=0;
HSSFRow ateRow(0);//创建sheet的第⼀⾏
for(String key:map.keySet()) {
num++;
}
//从list取第⼀⾏到最后⼀⾏的内容并放到对应的Excel⾥,若记录⾥某字段值没有会有问题
int rownum=1;//⾏数
for(Map<String, Object> data:list) {
HSSFRow ateRow(rownum);//创建sheet的第rownum+1⾏
int n=0;//列数
for(String key:data.keySet()) {
n++;
}
rownum++;
}
//1.通过IO流把数据写到⽂件上:只能写到服务器端
/*FileOutputStream out=new FileOutputStream(fileName);
workbook.write(out);
out.close();*/
//2.响应到客户端:可以下载到客户端(两个选⼀个)
try {
this.setResponseHeader(response, fileName);springframework jar包导入
OutputStream os = OutputStream();
workbook.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// 发送响应流⽅法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new Bytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-
stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
l和对应的接⼝
1. l:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhiyou100.kfs.dao.UserMapper">
<sql id="baseSql">
id,user_name
username,password,name,age,sex,birthday,created,updated
</sql>
<select id="selectListMap" resultType="java.util.LinkedHashMap"> select
<include refid="baseSql"/>
from tb_user
</select>
</mapper>
1. 对应的接⼝:
package com.zhiyou100.kfs.dao;
import java.util.List;
import java.util.Map;
public interface UserMapper{
/
**
* ExportExcel:导出Excel
* @return
*/
List<Map<String, Object>> selectListMap();
}
4.service层
package com.zhiyou100.kfs.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.kfs.dao.UserMapper;
import com.zhiyou100.kfs.dao.UserMapper;
@Service("userService")
public class UserServiceImp {
@Autowired
private UserMapper userMapper;
public List<Map<String, Object>> selectListMap(Integer pageNum,Integer pageSize){
PageHelper.startPage(pageNum, pageSize);
List<Map<String, Object>> list = userMapper.selectListMap();
PageInfo<Map<String, Object>> pageinfo=new PageInfo<>(list);
List();
}
}
5.测试代码:controller层
package com.ller;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhiyou100.kfs.service.UserServiceImp;
import com.zhiyou100.kfs.util.ExportExcel;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserServiceImp userServiceImp;
@RequestMapping("exportExcel")
public String exportExcel(Integer page,Integer rows) throws IOException { List<Map<String, Object>> list =
userServiceImp.selectListMap(page,rows);
String sheetname="⽤户管理";
String filepath="d://⽤户管理:第"+page+"页,每页"+rows+"记录.xls";
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论