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. c 越界

    int main(int argc, char* argv[]){ int i = 0; int arr[3] = {0}; for(; i<=3; i++){ arr[i] = 0; prin ...

  2. 分布式事务之最终一致性BASE理论

    一.事务 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元,组成事务的所有操作只有在所有操作均能正常执行的情况下方能提交,只要其中任一操作执行失败,都将导致整个事务的回滚.简单地说 ...

  3. activiti 自定义用户

    https://blog.csdn.net/meng564764406/article/details/53789958 此文目的: 对网络上的关于对activiti 使用做一个总结,因为很难找到一个 ...

  4. docker登录报错Error response from daemon: Get https://192.168.30.10/v1/users/: dial tcp 192.168.30.10:443: connect: connection refused

    背景描述: 登录docker报错: [root@localhost sysconfig]# docker login 192.168.30.10 Username (newcs06): newcs06 ...

  5. 使用draw.io桌面版代替visio制作流程图

    前言 draw.io是一款在github上的开源产品,由于需要构建在线文档,需要插入画图类型, 对比多款开源产品,最终选择了draw.io. draw.io图标资源非常的丰富,方便导入图标资源,基本上 ...

  6. matlab学习笔记9 高级绘图命令_1 图形对象_根对象,轴对象,用户控制对象,用户菜单对象

    一起来学matlab-matlab学习笔记9 高级绘图命令_1 图形对象_根对象,轴对象,用户控制对象,用户菜单对象 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matl ...

  7. 改进初学者的PID-正反作用

    最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...

  8. base64方式显示控件

    data:,文本数据 data:text/plain,文本数据 data:text/html,HTML代码 data:text/html;base64,base64编码的HTML代码 data:tex ...

  9. NB-IOT技术学习问题记录

    1. TAU是什么,跟踪区更新 2. 小区和基站的区别 3. 附着和设备注册的关系? 4. 不携带PDN是什么意思? 5. PLMN 公共陆地移动网络,和小区的关系,区别?

  10. 【Sqoop学习之一】Sqoop简介

    环境 sqoop-1.4.6 Sqoop:将关系数据库(oracle.mysql.postgresql等)数据与hadoop数据进行转换的工具. 两个版本:两个版本完全不兼容,sqoop1使用最多:s ...