java将数据库中查询到的数据导入到Excel表格
1.Maven需要的依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
2.导入示例
/**
* 黑名单查询
* @param blackListBean
* @return
*/
@RequestMapping(value = "/blackList", method = RequestMethod.POST)
@ResponseBody
public BaseReturnModel blackList(
@RequestBody BlackListBean blackListBean
) {
BaseReturnModel baseReturnModel = new BaseReturnModel();
List<BlackListBean> resultList = null;
int total = 0;
try {
if (blackListBean.getDownLoad() == null) {
int start = (blackListBean.getPagination() - 1) * blackListBean.getRownum(); // 分页处理 //rownum页数
blackListBean.setStart(start);
}
if (blackListBean.getUserIds() != null && !"".equals(blackListBean.getUserIds())) {
blackListBean.setIdUsers(blackListBean.getUserIds().split(","));
}
resultList = blackListSerivce.selectBlackList(blackListBean);
total = blackListSerivce.selectResult(blackListBean); } catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
baseReturnModel.setCode(200);// 状态
baseReturnModel.setMess("系统错误");
}
if (blackListBean.getDownLoad() != null) {
if (resultList.size() > 0) {
//创建一个表格
SXSSFWorkbook workbook = new SXSSFWorkbook();
//创建一个工作表
SXSSFSheet sheet = workbook.createSheet("sheet1");
//设置表头行
SXSSFRow xssfRow = sheet.createRow(0);
//设置单元格格式居中
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
//添加表头内容
SXSSFCell headCell = xssfRow.createCell(0);
headCell.setCellValue("站点ID");
headCell.setCellStyle(cellStyle);
// 单元格列宽 0代表列位置
sheet.setColumnWidth(0, 5500);
sheet.setColumnWidth(1, 5500);
sheet.setColumnWidth(2, 5500);
sheet.setColumnWidth(3, 5500);
sheet.setColumnWidth(4, 5500); //设置每列的参数
headCell = xssfRow.createCell(1);
headCell.setCellValue("站点名称");
headCell.setCellStyle(cellStyle); headCell = xssfRow.createCell(2);
headCell.setCellValue("黑名单手机号/单号");
headCell.setCellStyle(cellStyle); headCell = xssfRow.createCell(3);
headCell.setCellValue("理由");
headCell.setCellStyle(cellStyle); headCell = xssfRow.createCell(4);
headCell.setCellValue("时间");
headCell.setCellStyle(cellStyle); int b = 1;
String s = JSON.toJSONString(resultList);
SimpleDateFormat sdf = new SimpleDateFormat
("yyyy-MM-dd HH:mm:ss");
List<BlackListBean> list = JSON.parseArray(s, BlackListBean.class);
for (BlackListBean obj : list) {
xssfRow = sheet.createRow(b++); //时间戳转换
Long aLong = new Long(obj.getCreateTime());
Date date = new Date(aLong); //创建单元格,并添加值
SXSSFCell cell = xssfRow.createCell(0);
cell.setCellValue(obj.getIdUser());
cell.setCellStyle(cellStyle); cell = xssfRow.createCell(1);
cell.setCellValue(obj.getName());
cell.setCellStyle(cellStyle); cell = xssfRow.createCell(2);
cell.setCellValue(obj.getMobile());
cell.setCellStyle(cellStyle); cell = xssfRow.createCell(3);
cell.setCellValue(obj.getNote());
cell.setCellStyle(cellStyle); cell = xssfRow.createCell(4);
cell.setCellValue(sdf.format(date));
cell.setCellStyle(cellStyle);
}
//保存文件
String imgpath = "/usr/local/tomcat/apache-tomcat-8.5.32/webapps/excel/";
File file = new File(imgpath);
if (!file.exists()) {
file.mkdirs();
}
try {
String imgFilePath = imgpath + "blackList" + ".xlsx";
OutputStream outputStream = new FileOutputStream(imgFilePath);
baseReturnModel.setObj("/excel/" + "blackList" + ".xlsx");
baseReturnModel.setCode(200);
baseReturnModel.setMess("正在下载"); try {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
baseReturnModel.setCode(500);
baseReturnModel.setMess("写入失败");
baseReturnModel.setObj("");
} try {
outputStream.close();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} } catch (FileNotFoundException e) {
e.printStackTrace();
baseReturnModel.setCode(500);
baseReturnModel.setMess("下载失败");
}
return baseReturnModel;
} else {
baseReturnModel.setCode(500);
baseReturnModel.setMess("没有可提供下载得内容");
return baseReturnModel;
}
}
baseReturnModel.setCode(200);// 状态
baseReturnModel.setMess("成功");
baseReturnModel.setObj(resultList);
baseReturnModel.setTotal(total);
return baseReturnModel;
}
java将数据库中查询到的数据导入到Excel表格的更多相关文章
- SpringMvc处理模型数据(也就是从数据库中查询出来的数据放到请求域中)
这讲的是从数据库中查询到的数据,存放到请求域中.然后页面上直接可以从请求域中获取值. 有4种方式: 1):ModelAndView 是作为一个对象. /** * 目标方法的返回值可以是 Model ...
- vue中使用分页组件、将从数据库中查询出来的数据分页展示(前后端分离SpringBoot+Vue)
文章目录 1.看实现的效果 2.前端vue页面核心代码 2.1. 表格代码(表格样式可以去elementui组件库直接调用相应的) 2.2.分页组件代码 2.3 .script中的代码 3.后端核心代 ...
- Java向数据库中一次性插入大量数据
String sql = “insert into username.tablename(id) values(?)”; PreparedStatement stmt = conn.prepareSt ...
- C#将数据导入到Excel表格中
public static DataTable GetExcelToDataTableBySheet(string FileFullPath, string SheetName){ ...
- JDBC方式从数据库中查询数据并显示
1.创建数据库表myuser DROP TABLE IF EXISTS `myuser`; CREATE TABLE `myuser` ( `) NOT NULL COMMENT '姓名', `id` ...
- MongoDB数据库中查询数据(下)
MongoDB数据库中查询数据(下) 在find中,options参数值为一个对象,用来设置查询数据时使用的选项,下面我们来对该参数值对象中可以使用的属性进行介绍: 1. fields; 该属性值为一 ...
- 在MongoDB数据库中查询数据(上)
在MongoDB数据库中查询数据(上) 在MongoDB数据库中,可以使用Collection对象的find方法从一个集合中查询多个数据文档,find方法使用方法如下所示: collection.fi ...
- flask再学习-思考之怎么从数据库中查询数据在页面展示!
看别人视频觉得很简单,要自己做蒙蔽了!这样子.NO! 1. 流程: 首先要有和数据库连接的驱动!一般有PYMySQL mysqlclient 等 使用扩展Flask-SQLAlchemy 获得orm对 ...
- 使用JDBC从数据库中查询数据的方法
* ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...
随机推荐
- 从tom大叔那想着拿书的,呵呵。
//var tgtttime = new Date("2014/05/26 09:59:30"); var tgtttime = new Date("2014/05/26 ...
- oracle 表所占空间统计
1.通过查询dba_segments Select owner,segment_name,sum(bytes)/1024/1024 as MB from dba_segments group by o ...
- BUGKU login3
先看的wp,呢么来复现一遍,emmmmmm,尝试一波,用户名输入admin后,密码随便输,发现提示password error,呢么填其他用户名的话,发现提示username does not exi ...
- day10 函数的定义及函数语法详解
""" 今日内容: (1)函数的定义及特点 (2)函数的语法及函数的四部分 (3)函数的分类 (4)函数的调用 (5)函数的return详解 一.函数的定义 1.什么是函 ...
- find your present (2) hdoj 2095
/* author:谦智 find your present (2) hdoj 2095 法一:用暴力 法二:用map 法三: 符号是^. 异或是个位运算符号,具体是怎么操作的请百度,这里有个特性使得 ...
- Python爬虫实战三之爬取嗅事百科段子
一.前言 俗话说,上班时间是公司的,下班了时间才是自己的.搞点事情,写个爬虫程序,每天定期爬取点段子,看着自己爬的段子,也是一种乐趣. 二.Python爬取嗅事百科段子 1.确定爬取的目标网页 首先我 ...
- C# 7.0特性
一.out的形参变量无需再提前声明 befor: "; int numericResult; if (int.TryParse(input, out numericResult)) Cons ...
- 使用SecureCRT做端口转发
我的笔记本只能访问跳板机,跳板机是Linux系统,访问内网机器需要在跳板机内通过ssh命令访问,特别不方便,而且我们还需要访问Windows或web网站. 这是我们就可以做一个端口转发,通过自己的笔记 ...
- SVG初尝试(二)
基本图形 rect(矩形).circle.ellipse(椭圆).line(直线).polyline(折线).polygon(多边形).path(可以绘制任意图形) rect x,y定义矩形坐标,矩形 ...
- 【原创】beyond compare 解决文件一样,对比有差异的问题
在beyond compare的5~6年的使用过程中突然有一天发现,beyond compare对比的文件都一样,但是却都显示红色!很是烦人. 这是因为beyond compare一开始对比的时时间, ...