从数据库导出数据到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 ...
随机推荐
- Java 练习题摘录
2018-01-21 23:23:08 1.finally与return同时出现的情况 finally中有return语句则会使try...catch中的return语句失效 public stati ...
- mysql数据库优化课程---15、mysql优化步骤
mysql数据库优化课程---15.mysql优化步骤 一.总结 一句话总结:索引优化最立竿见影 1.mysql中最常用最立竿见影的优化是什么? 索引优化 索引优化,不然有多少行要扫描多少次,1亿行大 ...
- DataTable 操作
public void CreateTable() { //创建表 DataTable dt = new DataTable(); //1.添加列 dt.Columns.Add("Name& ...
- 第三章 如何使用Burp Suite代理
Burp Proxy 是Burp Suite以用户驱动测试流程功能的核心,通过代理模式,可以让我们拦截.查看.修改所有在客户端和服务端之间传输的数据. 本章主要讲述以下内容: Burp Proxy基本 ...
- 启用/禁用以太网的批处理,用于一个网卡切换本地网络和wifi使用(Win10)
注意下面时英文版上默认网络使用,同时接入了网线和wifi时,本地网络优先wifi. 所以禁用本地网络就会自动连接到wifi,启用本地网络,就会禁用wifi. 批处理支持 -y 参数,跳过用户输入y,代 ...
- DOM: EVENT FLOW
捕获阶段(capture phase) 捕获阶段的定义如下(w3c):The event object propagate through the target's ancestors from th ...
- OGNL表达式语言中的"#"和"$"的区别
Struts2使用标准的Context来进行OGNL表达式语言求值,OGNL的顶级对象是一个Context,这个Context对象就是一个Map类型实例,其根对象就是ValueStack,如果需要访问 ...
- python----tkinterm模块
python tkinter学习——布局 目录 一.pack() 二.grid() 三.place() 四.Frame() 正文 布局 一.pack() pack()有以下几个常用属性: side ...
- Docker - 在Ubuntu 14.04 Server上的安装Docker
在 Ubuntu 14.04 Server 上安装过程是最简单的, 其满足了安装 Docker的所有要求,只需要执行如下安装脚本即可. 如果你有可能,请使用14.04版本的Ubuntu, 避免给自己挖 ...
- qt QTableWidget&&QTableView 导出数据到excel
通常情况下,我们在开发过程中比较常用的方法是将表格的数据到处到excel文件.我也在这个点上头疼了很长时间,不过功夫不负苦心人,最终还是勉强达到效果,为了后面再次用到时不手忙脚乱现在将方法寄存在此,如 ...