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的更多相关文章

  1. Java将数据库数据导入EXCEL

    一般的数据库数据怎么导入excel中呢??这让人非常疑惑,今天我找到了一个方法能够实现 须要导入一个第三方包下载地址 详细内容例如以下: 里面含有指导文档,index.html里面含有怎样读取数据库文 ...

  2. ASP.NET中数据库数据导入Excel并打印(2)

    大家可能觉得上面的代码比较复杂,因为上面对于对打印要求比较高的应用,是十分有效的.如果只是单单对数据进行导出,还可以使用简单的格式,比如使用以下的代码:      Private Sub Page_L ...

  3. PHP把数据库数据导入Excel

    <?php function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; ...

  4. [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!

    引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...

  5. 转:[Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!

    引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...

  6. 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转

    效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载])    本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较   ...

  7. python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图

    python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...

  8. C#变成数据导入Excel和导出Excel

    excel 基础 •整个excel 表格叫工作表:workbook:工作表包含的叫页:sheet:行:row:单元格:cell. •excel 中的电话号码问题,看起来像数字的字符串以半角单引号开头就 ...

  9. 数据库数据用Excel导出的3种方法

    将数据库数据用Excel导出主要有3种方法:用Excel.Application接口.用OleDB.用HTML的Tabel标签 方法1——Excel.Application接口: 首先,需要要Exce ...

随机推荐

  1. http各类型请求方法工具总结

    本文为博主原创,未经允许不得转载: 在项目中会用到各种类型的http请求,包含put,get,post,delete,formData等各种请求方式,在这里总结一下 用过比较好的请求工具,使用serv ...

  2. python 代码性能分析 库

    问题描述 1.Python开发的程序在使用过程中很慢,想确定下是哪段代码比较慢: 2.Python开发的程序在使用过程中占用内存很大,想确定下是哪段代码引起的: 解决方案 使用profile分析分析c ...

  3. Android 动态更换桌面图标

    每当双 11.12 来临之际,Android 手机 Launcher 中的淘宝.天猫图标就会变成双 11.12 主题的图标.实现了动态切换图标.名称 MainActivity package com. ...

  4. Linux_CentOS 中systemctl 管理服务、防火墙 firewalld 以及 SELinux 配置

    使用 systemctl 管理服务 systemctl 就是 service 和 chkconfig 这两个命令的整合,在 CentOS 7 就开始被使用了,systemctl是系统服务管理器命令,它 ...

  5. 使用Spring容器最简单的代码姿势

    如果仅仅是为了测试简单使用一下Spring的IOC容器,或者研究一下Spring的容器的源码实现,那么在搭建Spring工程的时候,不需要复杂的xml配置.Spring3.0之后提供了Java注解配置 ...

  6. URLDoBase64

    import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; ...

  7. PCL读取PCD文件的数据

    1.pcd文件——rabbit.pcd 链接:https://pan.baidu.com/s/1v6mjPjwd7fIqUSjlIGTIGQ提取码:zspx 新建项目pcl rabbit.pcd 和p ...

  8. 【Tools】Myeclise-2018.12.0 最新破解文件

    Myeclise-2018.12.0 最新破解文件. 最近在写android app登录块,需要用到这个工具,顺手就拿到了,发现资源太少.这里分享给大家. 有币高富帅打赏下载地址: https://d ...

  9. [转帖]首颗国产DRAM芯片的技术与专利,合肥长鑫存储的全面深度剖析

    首颗国产DRAM芯片的技术与专利,合肥长鑫存储的全面深度剖析 https://mp.weixin.qq.com/s/g_gnr804q8ix4b9d81CZ1Q 2019.11 存储芯片已经成为全球珍 ...

  10. 开发板与pc之间文件传输:kermit and lrzsz

    imx6开发板与pc机之间通过串口传输文件步骤: 1. 安装好kermit并可以使用 2. 交叉编译lrzsz开源软件并把可执行程序lrz lsz拷贝到开发板 2.1 下载并解压lrzsz-0.12. ...