java数据库数据导入excel
data导出入excel中
controller:
package com.longfor.hrssc.api.controller; import com.longfor.hrssc.api.model.BasicInformation;
import com.longfor.hrssc.api.service.IBasicInformationService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; /**
* Created by fmgao on 2019/5/5.
*/
@RestController
@RequestMapping("/export")
public class ExportExcel {
@Autowired
private IBasicInformationService basicInformationService; @RequestMapping(value = "/excel", method = RequestMethod.GET)
public Object excel2(HttpServletResponse response) throws Exception {
// list = getUsers();
String columnName = "t_basic_information";
BasicInformation basicInformation = new BasicInformation();
basicInformation.setTableName(columnName);
List<String> titles = new ArrayList();
titles = basicInformationService.getColumnNames(basicInformation);
System.out.println(titles);
List<BasicInformation> list = new ArrayList();
list = basicInformationService.getAllDatas(basicInformation); stuList2Excel(list,titles);
return null;
} /**
* @param stuList 从数据库中查询需要导入excel文件的信息列表
* @return 返回生成的excel文件的路径
* @throws Exception
*/
public static String stuList2Excel(List<BasicInformation> stuList,List<String> titles) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd hhmmss");
Workbook wb = new XSSFWorkbook();
//标题行抽出字段
// String[] title = titles;
//设置sheet名称,并创建新的sheet对象
String sheetName = "学生信息一览";
Sheet stuSheet = wb.createSheet(sheetName);
//获取表头行
Row titleRow = stuSheet.createRow(0);
//创建单元格,设置style居中,字体,单元格大小等
CellStyle style = wb.createCellStyle();
Cell cell = null;
//把已经写好的标题行写入excel文件中
for (int i = 0; i < titles.size(); i++) {
cell = titleRow.createCell(i);
cell.setCellValue(titles.get(i));
cell.setCellStyle(style);
}
//把从数据库中取得的数据一一写入excel文件中
Row row = null;
for (int i = 0; i < stuList.size(); i++) {
//创建list.size()行数据
row = stuSheet.createRow(i + 1);
//把值一一写进单元格里
//设置第一列为自动递增的序号
// row.createCell(0).setCellValue(i + 1);
row.createCell(0).setCellValue(stuList.get(i).getId());
row.createCell(1).setCellValue(stuList.get(i).getBasicCode());
row.createCell(2).setCellValue(stuList.get(i).getBasicName());
row.createCell(3).setCellValue(stuList.get(i).getBasicType());
row.createCell(4).setCellValue(stuList.get(i).getBasicPid());
row.createCell(5).setCellValue(stuList.get(i).getIsDelete());
row.createCell(6).setCellValue(stuList.get(i).getCreateUserId());
//把时间转换为指定格式的字符串再写入excel文件中
if (stuList.get(i).getCreateTime() != null) {
row.createCell(7).setCellValue(sdf.format(stuList.get(i).getCreateTime()));
}
if (stuList.get(i).getUpdateTime() != null) {
row.createCell(8).setCellValue(sdf.format(stuList.get(i).getUpdateTime()));
} }
//设置单元格宽度自适应,在此基础上把宽度调至1.5倍
for (int i = 0; i < titles.size(); i++) {
stuSheet.autoSizeColumn(i, true);
stuSheet.setColumnWidth(i, stuSheet.getColumnWidth(i) * 15 / 10);
}
//获取配置文件中保存对应excel文件的路径,本地也可以直接写成F:excel/stuInfoExcel路径
// String folderPath = ResourceBundle.getBundle("systemconfig").getString("downloadFolder") + File.separator + "stuInfoExcel";
String folderPath = "F:\\file_soft\\me\\excel\\";
//创建上传文件目录
File folder = new File(folderPath);
//如果文件夹不存在创建对应的文件夹
if (!folder.exists()) {
folder.mkdirs();
}
//设置文件名
String fileName = sdf1.format(new Date()) + sheetName + ".xlsx";
String savePath = folderPath + File.separator + fileName;
// System.out.println(savePath); OutputStream fileOut = new FileOutputStream(savePath);
wb.write(fileOut);
fileOut.close();
//返回文件保存全路径
System.out.println(savePath);
return savePath;
}
}
sevice:
/**
* 获取所有的列
* @param basicInformation
* @return
*/
public List<String> getColumnNames(BasicInformation basicInformation){
List<String> names = basicInformationMapper.getColumnNames(basicInformation);
return names;
} /**
* 获取所有的数据
* @param basicInformation
* @return
*/
public List<BasicInformation> getAllDatas(BasicInformation basicInformation){
List<BasicInformation> list = basicInformationMapper.getAllDatas(basicInformation);
return list;
}
dao:
List<String> getColumnNames(BasicInformation basicInformation);
List<BasicInformation> getAllDatas(BasicInformation basicInformation);
xml:
<!--getColumnNames-->
<select id="getColumnNames" resultType="java.lang.String"
parameterType="com.longfor.hrssc.api.model.BasicInformation">
select DISTINCT COLUMN_NAME from information_schema.COLUMNS where table_name=#{tableName}
</select> <!--get all-->
<select id="getAllDatas" resultMap="BaseResultMap"
parameterType="com.longfor.hrssc.api.model.BasicInformation">
select * from t_basic_information;
</select>
pom:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency> <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
java数据库数据导入excel的更多相关文章
- Java将数据库数据导入EXCEL
一般的数据库数据怎么导入excel中呢??这让人非常疑惑,今天我找到了一个方法能够实现 须要导入一个第三方包下载地址 详细内容例如以下: 里面含有指导文档,index.html里面含有怎样读取数据库文 ...
- ASP.NET中数据库数据导入Excel并打印(2)
大家可能觉得上面的代码比较复杂,因为上面对于对打印要求比较高的应用,是十分有效的.如果只是单单对数据进行导出,还可以使用简单的格式,比如使用以下的代码: Private Sub Page_L ...
- PHP把数据库数据导入Excel
<?php function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; ...
- [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...
- 转:[Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载]) 本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较 ...
- python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图
python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...
- C#变成数据导入Excel和导出Excel
excel 基础 •整个excel 表格叫工作表:workbook:工作表包含的叫页:sheet:行:row:单元格:cell. •excel 中的电话号码问题,看起来像数字的字符串以半角单引号开头就 ...
- 数据库数据用Excel导出的3种方法
将数据库数据用Excel导出主要有3种方法:用Excel.Application接口.用OleDB.用HTML的Tabel标签 方法1——Excel.Application接口: 首先,需要要Exce ...
随机推荐
- 【NumPy】 之常见运算(np.around、np.floor、np.ceil、np.where)
aroundnp.around 返回四舍五入后的值,可指定精度. around(a, decimals=0, out=None) a 输入数组 decimals 要舍入的小数位数. 默认值为0. 如果 ...
- [转] cmake源码编译安装jsoncpp
1.下载jsoncpp源码 wget https://github.com/open-source-parsers/jsoncpp/archive/master.zip 2.解压缩源码文件 unzip ...
- RabbitMQ整合Spring Booot【消费者应答模式】
生产者代码不变,消费者: package com.toov5.Consumer; import java.io.IOException; import java.util.concurrent.Tim ...
- 泡泡一分钟:Using Geometric Features to Represent Near-Contact Behavior in Robotic Grasping
张宁 Using Geometric Features to Represent Near-Contact Behavior in Robotic Grasping链接:https://pan.ba ...
- C++使用fill初始化二维数组
类似如下用法: fill(dis[0], dis[0]+maxn*maxn, INF); 因为 dis[0]才是dis的首元素 dis[0][0] 的地址.
- [LeetCode]577. Employee Bonus 员工奖金
Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...
- Web服务器、应用程序服务器、web应用服务器、反向代理服务器
参考链接:https://www.cnblogs.com/zhaoyl/archive/2012/10/10/2718575.html 首先我们来了解什么是服务器(server) 一般来说,serve ...
- 密码工具:KeePassXC
KeePassXC 开源免费 只支持数据库存放在本地, 跨平台 自动填充 使用浏览器插件时,软件必须启动着 KeePassXC is a community fork of KeePassX, a n ...
- mysql 5.7 sql_mode设置 坑
原文地址:https://blog.csdn.net/u012259256/article/details/56482218 1.查看sql_mode select @@sql_mode查询出来的值为 ...
- Linux系统下Dubbo安装的详细教程
Linux系统下Dubbo安装的详细教程 1.Dubbo的简介 Dubbo是 [1] 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 [ ...