从数据库导出数据到excel之List<map>导出
说明:很多时候取出来的数据是封装为List<Map<String,Object>>,可以直接导出excel表格
项目说明就在 “上一篇”
直接上代码(数据层和业务层不用说了,查出的代码格式为List<Map<String,Object>>即可):
控制层
- List<Map<String,String>> list=expot.GetStudentTest(gender.getGender());
- System.out.println("listDate:"+list);
- ExportExcelHSSF<List<Map<String,String>>> ee= new ExportExcelHSSF<>();
- ExportExcelOutputStream ee=new ExportExcelOutputStream();
- //String[] headers = { "姓名", "性别", "体重","班级","网络协议","javaEE","计算机基础","Linux操作系统","网络安全","sql数据库","数据结构" };
- String[] headers = { "姓名","体重","网络协议","javaEE","计算机基础","Linux操作系统","网络安全","sql数据库","数据结构" };
- String fileName = "信息表";
- ee.exportExcel(list, headers,fileName, response);
实体类:
- public class ExportDateTest implements Serializable{
- private String name;
- //private String gender;//性别
- private String weight;
- //private String grades;//班级
- private Double Networkprotocol;
- private Double javaEE;
- private Double Computerbasis;
- private Double Linuxoperatingsystem;
- private Double networksecurity;
- private Double SQLdatabase;
- private Double datastructure;
- public ExportDateTest() {
- // TODO Auto-generated constructor stub
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- /*
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- */
- public String getWeight() {
- return weight;
- }
- public void setWeight(String weight) {
- this.weight = weight;
- }
- /*
- public String getGrades() {
- return grades;
- }
- public void setGrades(String grades) {
- this.grades = grades;
- }
- */
- public Double getNetworkprotocol() {
- return Networkprotocol;
- }
- public void setNetworkprotocol(Double networkprotocol) {
- Networkprotocol = networkprotocol;
- }
- public Double getJavaEE() {
- return javaEE;
- }
- public void setJavaEE(Double javaEE) {
- this.javaEE = javaEE;
- }
- public Double getComputerbasis() {
- return Computerbasis;
- }
- public void setComputerbasis(Double computerbasis) {
- Computerbasis = computerbasis;
- }
- public Double getLinuxoperatingsystem() {
- return Linuxoperatingsystem;
- }
- public void setLinuxoperatingsystem(Double linuxoperatingsystem) {
- Linuxoperatingsystem = linuxoperatingsystem;
- }
- public Double getNetworksecurity() {
- return networksecurity;
- }
- public void setNetworksecurity(Double networksecurity) {
- this.networksecurity = networksecurity;
- }
- public Double getSQLdatabase() {
- return SQLdatabase;
- }
- public void setSQLdatabase(Double sQLdatabase) {
- SQLdatabase = sQLdatabase;
- }
- public Double getDatastructure() {
- return datastructure;
- }
- public void setDatastructure(Double datastructure) {
- this.datastructure = datastructure;
- }
- public ExportDateTest(String name, String gender, String weight, String grades, Double networkprotocol, Double javaEE,
- Double computerbasis, Double linuxoperatingsystem, Double networksecurity, Double sQLdatabase,
- Double datastructure) {
- super();
- this.name = name;
- //this.gender = gender;
- this.weight = weight;
- //this.grades = grades;
- Networkprotocol = networkprotocol;
- this.javaEE = javaEE;
- Computerbasis = computerbasis;
- Linuxoperatingsystem = linuxoperatingsystem;
- this.networksecurity = networksecurity;
- SQLdatabase = sQLdatabase;
- this.datastructure = datastructure;
- }
- @Override
- public String toString() {
- return "ExportDate [name=" + name + ""
- //+ ", gender=" + gender + ""
- + ", weight=" + weight + ""
- // + ", grades=" + grades
- + ", Networkprotocol=" + Networkprotocol + ", javaEE=" + javaEE + ", Computerbasis=" + Computerbasis
- + ", Linuxoperatingsystem=" + Linuxoperatingsystem + ", networksecurity=" + networksecurity
- + ", SQLdatabase=" + SQLdatabase + ", datastructure=" + datastructure + "]";
- }
- }
导出代码:
- /**
- * 将List<Map<>>数据格式导出到excel中
- * @author 开发者
- *
- */
- public class ExportExcelOutputStreamTest {
- public String exportExcel(List<Map<String, String>> orderlist,String[] headerlist,
- String name,HttpServletResponse response ) {
- String result = "系统提示:Excel文件导出成功!";
- // 以下开始输出到EXCEL
- try {
- //定义输出流,以便打开保存对话框______________________begin
- OutputStream os = response.getOutputStream();// 取得输出流
- response.reset();// 清空输出流
- String fileName = name + ".xls";
- response.setContentType("application/x-msdownload");// 设定输出文件类型
- response.setHeader("Content-Disposition",
- "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" )); //设定文件输出类型
- //定义输出流,以便打开保存对话框_______________________end
- /** **********创建工作簿************ */
- WritableWorkbook workbook = Workbook.createWorkbook(os);
- /** **********创建工作表************ */
- WritableSheet sheet = workbook.createSheet("Sheet1", 0);
- /** **********设置纵横打印(默认为纵打)、打印纸***************** */
- SheetSettings sheetset = sheet.getSettings();
- sheetset.setProtected(false);
- sheetset.setDefaultColumnWidth(20);
- /** ************设置单元格字体************** */
- WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
- WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,
- WritableFont.BOLD);
- WritableFont TitleFont = new WritableFont(WritableFont.ARIAL, 20,
- WritableFont.BOLD);
- /** ************以下设置三种单元格样式,灵活备用************ */
- // 用于表名,要高端大气!
- WritableCellFormat title_center = new WritableCellFormat(TitleFont);
- title_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
- title_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
- title_center.setAlignment(Alignment.CENTRE); // 文字水平对齐
- title_center.setWrap(false); // 文字是否换行
- // 用于标题居中
- WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
- wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
- wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
- wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐
- wcf_center.setWrap(false); // 文字是否换行
- // 用于正文居左
- WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
- wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
- wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
- wcf_left.setAlignment(Alignment.LEFT); // 文字水平对齐
- wcf_left.setWrap(true); // 文字是否换行
- /** ***************以下是EXCEL第一行列标题********************* */
- for (int i = 0; i < headerlist.length; i++) {
- sheet.addCell(new Label(i, 3, headerlist[i], wcf_center));
- }
- /** ***************以下是EXCEL正文数据********************* */
- //这里要分成两种情况,第一种传的是List<Map>,用Map的方式处理
- //第二种传的是List<Object>,用普通类取属性的方式来处理;
- Iterator iterator = orderlist.iterator();
- Object o = iterator.next();
- Map m = (Map) o;
- System.out.println("m:"+m);
- Iterator iter = m.entrySet().iterator();
- int first_j=0;
- while (iter.hasNext()) {
- Map.Entry entry = (Map.Entry) iter.next();
- String[] key_value = entry.toString().split("=");
- System.out.println(key_value[1]);
- sheet.addCell(new Label(first_j, 4, key_value[1], wcf_left));
- first_j++;
- }
- int i=5;
- while(iterator.hasNext()){
- Map row_map = (Map) iterator.next();
- Iterator row_iterator = row_map.entrySet().iterator();
- int second_j=0;
- while(row_iterator.hasNext()){
- Map.Entry entry_column = (Map.Entry) row_iterator.next();
- String[] key_value = entry_column.toString().split("=");
- sheet.addCell(new Label(second_j, i, key_value[1], wcf_left));
- second_j++;
- }
- i++;
- }
- /** **********将以上缓存中的内容写到EXCEL文件中******** */
- workbook.write();
- /** *********关闭文件************* */
- workbook.close();
- System.out.println(result);
- } catch (Exception e) {
- result = "系统提示:Excel文件导出失败,原因:" + e.toString();
- System.out.println(result);
- e.printStackTrace();
- }
- return result;
- }
从数据库导出数据到excel之List<map>导出的更多相关文章
- 从数据库导出数据到excel之List<List<Object>>导出
说明:有时候数据处理为List<List<Object>>更方便 姊妹篇:从数据库导出数据到excel之List<Map<>>导出 兄弟篇:从数据库导出 ...
- 从数据库导出数据到excel之POI操作
项目说明: 1:数据库中有两张表,主键关联 2:根据条件查询数据 3:处理为需要的数据封装类型,然后传到导出excel的方法中 <--框架部署就不详谈了,用的spring框架--> 补充: ...
- 使用python脚本从数据库导出数据到excel
python从数据库导出数据到excel 最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令 select * from xxx where xxx into outfile 'xx ...
- 数据库导出数据到excel格式
场景: 由于业务人员经常会找DBA导出一些数据,写了一个自动导出脚本. import pymysql from openpyxl import Workbook from openpyxl.write ...
- 【转】c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
准备工作就是可以分页的DataGridView,和两个按钮,一个用来导出当前页数据到Excel,一个用来导出全部数据到Excel 没有使用SaveFileDialog,但却可以弹出保存对话框来 先做导 ...
- ASP.NET导出数据到Excel 实例介绍
ASP.NET导出数据到Excel 该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成. 下面是代码 新建 ...
- 微软BI 之SSIS 系列 - 导出数据到 Excel 2013 的实现
开篇介绍 碰到有几个朋友问到这个问题,比较共性,就特意写了这篇小文章说明一下如何实现在 SSIS 中导出数据到 Office Excel 2013 中.通常情况下 2013 以前的版本大多没有问题,但 ...
- 导出数据到Excel方法总结
一,问题的提出 近来在网上经常有人问怎样把数据导出到Excel中?针对这个问题网上也有很多资料.大都比较的琐碎.本人当前从事的项目中,刚好涉及到这些内容.就顺便做了一些归纳整理.共享给大家.避免大家再 ...
- phpexcel如何读取excel的数据和如何导出数据到excel
phpexcel如何读取excel的数据和如何导出数据到excel 一.总结 一句话总结:去官网看参考手册和api,或者找中文的博客或者参考手册 1.phpexcel插件如何下载? 其实这些插件不仅可 ...
- 从DataTable高效率导出数据到Excel
首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: using Microsoft.Office.I ...
随机推荐
- String类的subString(a,b)方法(基于jdk 1.9)
基于上文:http://www.jianshu.com/p/a20ee3bb9c1b public String substring(int beginIndex, int endIndex) { i ...
- wget下载指定目录下的文件
wget -r -np -k -P ~/tmp/ http://xxx.com/download -P 表示下载到哪个目录-r 表示递归下载-np 表示不下载旁站连接.-k 表示将下载的网页里 ...
- bzoj1001平面图最小割转对偶图最短路
https://www.lydsy.com/JudgeOnline/problem.php?id=1001 很明显的求对偶图的最短路即可(由于特判写错了一直wa = = ) //#pragma com ...
- ZOJ 2599 Graduated Lexicographical Ordering ★(数位DP)
题意 定义两个数的比较方法,各位数字之和大的数大,如果数字和相等则按字典序比较两个数的大小.输入n,k,求:1.数字k的排名:2.排名为k的数. 思路 算是一类经典的统计问题的拓展吧~ 先来看第一问. ...
- 基于centos的docker安装
1. 安装需求 内核版本3.10以上 Centos 7以上 64位版本 2. 使用root登录或者具有sudo权限 3. 确保系统是最新的 yum update 4. 添加yum源 tee /etc/ ...
- C++高级编程2. 静态动态链接库
C++高级编程2. 静态动态链接库20131018 1.动态链接库和静态链接库的区别: 静态链接库就是把lib文件中用到的函数代码直接连接进目标程序,程序运行的时候不在需要其他的库文件:动态链接库是把 ...
- C++(笔记)浅析vector容器的实例
http://www.runoob.com/w3cnote/cpp-vector-container-analysis.html 转载请注明出处 浅然的专栏 https://blog.csdn.net ...
- CocoaPods Mac App的安装和使用
CocoaPods桌面应用版下载地址:https://cocoapods.org/app打开应用会提示你是否安装命令行工具,选择install就也可以在命令行使用Pod了.省去了上面的步骤们,方便快捷 ...
- ES获取磁盘使用率情况
private void diskUage() { ClusterStateResponse stateResponse = client.admin().cluster().prepareState ...
- APUE学习笔记——5.5~5.7数据流的打开与读写
1.open #include <stdio.h> FILE *fopen(const char *restrict pathname,const char *restrict type) ...