说明:很多时候取出来的数据是封装为List<Map<String,Object>>,可以直接导出excel表格

项目说明就在 “上一篇

直接上代码(数据层和业务层不用说了,查出的代码格式为List<Map<String,Object>>即可):

控制层

  1. List<Map<String,String>> list=expot.GetStudentTest(gender.getGender());
  2. System.out.println("listDate:"+list);
  3. ExportExcelHSSF<List<Map<String,String>>> ee= new ExportExcelHSSF<>();
  4. ExportExcelOutputStream ee=new ExportExcelOutputStream();
  5. //String[] headers = { "姓名", "性别", "体重","班级","网络协议","javaEE","计算机基础","Linux操作系统","网络安全","sql数据库","数据结构" };
  6. String[] headers = { "姓名","体重","网络协议","javaEE","计算机基础","Linux操作系统","网络安全","sql数据库","数据结构" };
  7. String fileName = "信息表";
  8. ee.exportExcel(list, headers,fileName, response);

实体类:

  1. public class ExportDateTest implements Serializable{
  2. private String name;
  3. //private String gender;//性别
  4. private String weight;
  5. //private String grades;//班级
  6. private Double Networkprotocol;
  7. private Double javaEE;
  8. private Double Computerbasis;
  9. private Double Linuxoperatingsystem;
  10. private Double networksecurity;
  11. private Double SQLdatabase;
  12. private Double datastructure;
  13. public ExportDateTest() {
  14.  
  15. // TODO Auto-generated constructor stub
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. /*
  24. public String getGender() {
  25. return gender;
  26. }
  27. public void setGender(String gender) {
  28. this.gender = gender;
  29. }
  30. */
  31. public String getWeight() {
  32. return weight;
  33. }
  34. public void setWeight(String weight) {
  35. this.weight = weight;
  36. }
  37. /*
  38. public String getGrades() {
  39. return grades;
  40. }
  41. public void setGrades(String grades) {
  42. this.grades = grades;
  43. }
  44. */
  45. public Double getNetworkprotocol() {
  46. return Networkprotocol;
  47. }
  48. public void setNetworkprotocol(Double networkprotocol) {
  49. Networkprotocol = networkprotocol;
  50. }
  51. public Double getJavaEE() {
  52. return javaEE;
  53. }
  54. public void setJavaEE(Double javaEE) {
  55. this.javaEE = javaEE;
  56. }
  57. public Double getComputerbasis() {
  58. return Computerbasis;
  59. }
  60. public void setComputerbasis(Double computerbasis) {
  61. Computerbasis = computerbasis;
  62. }
  63. public Double getLinuxoperatingsystem() {
  64. return Linuxoperatingsystem;
  65. }
  66. public void setLinuxoperatingsystem(Double linuxoperatingsystem) {
  67. Linuxoperatingsystem = linuxoperatingsystem;
  68. }
  69. public Double getNetworksecurity() {
  70. return networksecurity;
  71. }
  72. public void setNetworksecurity(Double networksecurity) {
  73. this.networksecurity = networksecurity;
  74. }
  75. public Double getSQLdatabase() {
  76. return SQLdatabase;
  77. }
  78. public void setSQLdatabase(Double sQLdatabase) {
  79. SQLdatabase = sQLdatabase;
  80. }
  81. public Double getDatastructure() {
  82. return datastructure;
  83. }
  84. public void setDatastructure(Double datastructure) {
  85. this.datastructure = datastructure;
  86. }
  87. public ExportDateTest(String name, String gender, String weight, String grades, Double networkprotocol, Double javaEE,
  88. Double computerbasis, Double linuxoperatingsystem, Double networksecurity, Double sQLdatabase,
  89. Double datastructure) {
  90. super();
  91. this.name = name;
  92. //this.gender = gender;
  93. this.weight = weight;
  94. //this.grades = grades;
  95. Networkprotocol = networkprotocol;
  96. this.javaEE = javaEE;
  97. Computerbasis = computerbasis;
  98. Linuxoperatingsystem = linuxoperatingsystem;
  99. this.networksecurity = networksecurity;
  100. SQLdatabase = sQLdatabase;
  101. this.datastructure = datastructure;
  102. }
  103. @Override
  104. public String toString() {
  105. return "ExportDate [name=" + name + ""
  106. //+ ", gender=" + gender + ""
  107. + ", weight=" + weight + ""
  108. // + ", grades=" + grades
  109. + ", Networkprotocol=" + Networkprotocol + ", javaEE=" + javaEE + ", Computerbasis=" + Computerbasis
  110. + ", Linuxoperatingsystem=" + Linuxoperatingsystem + ", networksecurity=" + networksecurity
  111. + ", SQLdatabase=" + SQLdatabase + ", datastructure=" + datastructure + "]";
  112. }
  113.  
  114. }

导出代码:

  1. /**
  2. * 将List<Map<>>数据格式导出到excel中
  3. * @author 开发者
  4. *
  5. */
  6. public class ExportExcelOutputStreamTest {
  7. public String exportExcel(List<Map<String, String>> orderlist,String[] headerlist,
  8. String name,HttpServletResponse response ) {
  9. String result = "系统提示:Excel文件导出成功!";
  10. // 以下开始输出到EXCEL
  11. try {
  12. //定义输出流,以便打开保存对话框______________________begin
  13. OutputStream os = response.getOutputStream();// 取得输出流
  14. response.reset();// 清空输出流
  15.  
  16. String fileName = name + ".xls";
  17. response.setContentType("application/x-msdownload");// 设定输出文件类型
  18. response.setHeader("Content-Disposition",
  19. "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" )); //设定文件输出类型
  20. //定义输出流,以便打开保存对话框_______________________end
  21. /** **********创建工作簿************ */
  22. WritableWorkbook workbook = Workbook.createWorkbook(os);
  23. /** **********创建工作表************ */
  24. WritableSheet sheet = workbook.createSheet("Sheet1", 0);
  25. /** **********设置纵横打印(默认为纵打)、打印纸***************** */
  26. SheetSettings sheetset = sheet.getSettings();
  27. sheetset.setProtected(false);
  28. sheetset.setDefaultColumnWidth(20);
  29.  
  30. /** ************设置单元格字体************** */
  31. WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
  32. WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,
  33. WritableFont.BOLD);
  34. WritableFont TitleFont = new WritableFont(WritableFont.ARIAL, 20,
  35. WritableFont.BOLD);
  36.  
  37. /** ************以下设置三种单元格样式,灵活备用************ */
  38. // 用于表名,要高端大气!
  39. WritableCellFormat title_center = new WritableCellFormat(TitleFont);
  40. title_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
  41. title_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
  42. title_center.setAlignment(Alignment.CENTRE); // 文字水平对齐
  43. title_center.setWrap(false); // 文字是否换行
  44.  
  45. // 用于标题居中
  46. WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
  47. wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
  48. wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
  49. wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐
  50. wcf_center.setWrap(false); // 文字是否换行
  51.  
  52. // 用于正文居左
  53. WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
  54. wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条
  55. wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
  56. wcf_left.setAlignment(Alignment.LEFT); // 文字水平对齐
  57. wcf_left.setWrap(true); // 文字是否换行
  58.  
  59. /** ***************以下是EXCEL第一行列标题********************* */
  60. for (int i = 0; i < headerlist.length; i++) {
  61. sheet.addCell(new Label(i, 3, headerlist[i], wcf_center));
  62. }
  63. /** ***************以下是EXCEL正文数据********************* */
  64. //这里要分成两种情况,第一种传的是List<Map>,用Map的方式处理
  65. //第二种传的是List<Object>,用普通类取属性的方式来处理;
  66.  
  67. Iterator iterator = orderlist.iterator();
  68. Object o = iterator.next();
  69. Map m = (Map) o;
  70. System.out.println("m:"+m);
  71. Iterator iter = m.entrySet().iterator();
  72. int first_j=0;
  73. while (iter.hasNext()) {
  74. Map.Entry entry = (Map.Entry) iter.next();
  75. String[] key_value = entry.toString().split("=");
  76.  
  77. System.out.println(key_value[1]);
  78.  
  79. sheet.addCell(new Label(first_j, 4, key_value[1], wcf_left));
  80. first_j++;
  81. }
  82. int i=5;
  83. while(iterator.hasNext()){
  84. Map row_map = (Map) iterator.next();
  85. Iterator row_iterator = row_map.entrySet().iterator();
  86. int second_j=0;
  87. while(row_iterator.hasNext()){
  88. Map.Entry entry_column = (Map.Entry) row_iterator.next();
  89. String[] key_value = entry_column.toString().split("=");
  90. sheet.addCell(new Label(second_j, i, key_value[1], wcf_left));
  91. second_j++;
  92. }
  93. i++;
  94. }
  95. /** **********将以上缓存中的内容写到EXCEL文件中******** */
  96. workbook.write();
  97. /** *********关闭文件************* */
  98. workbook.close();
  99.  
  100. System.out.println(result);
  101.  
  102. } catch (Exception e) {
  103. result = "系统提示:Excel文件导出失败,原因:" + e.toString();
  104. System.out.println(result);
  105. e.printStackTrace();
  106. }
  107. return result;
  108. }

从数据库导出数据到excel之List<map>导出的更多相关文章

  1. 从数据库导出数据到excel之List<List<Object>>导出

    说明:有时候数据处理为List<List<Object>>更方便 姊妹篇:从数据库导出数据到excel之List<Map<>>导出 兄弟篇:从数据库导出 ...

  2. 从数据库导出数据到excel之POI操作

    项目说明: 1:数据库中有两张表,主键关联 2:根据条件查询数据 3:处理为需要的数据封装类型,然后传到导出excel的方法中 <--框架部署就不详谈了,用的spring框架--> 补充: ...

  3. 使用python脚本从数据库导出数据到excel

    python从数据库导出数据到excel 最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令 select * from xxx where xxx into outfile 'xx ...

  4. 数据库导出数据到excel格式

    场景: 由于业务人员经常会找DBA导出一些数据,写了一个自动导出脚本. import pymysql from openpyxl import Workbook from openpyxl.write ...

  5. 【转】c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据

    准备工作就是可以分页的DataGridView,和两个按钮,一个用来导出当前页数据到Excel,一个用来导出全部数据到Excel 没有使用SaveFileDialog,但却可以弹出保存对话框来 先做导 ...

  6. ASP.NET导出数据到Excel 实例介绍

    ASP.NET导出数据到Excel  该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成. 下面是代码 新建 ...

  7. 微软BI 之SSIS 系列 - 导出数据到 Excel 2013 的实现

    开篇介绍 碰到有几个朋友问到这个问题,比较共性,就特意写了这篇小文章说明一下如何实现在 SSIS 中导出数据到 Office Excel 2013 中.通常情况下 2013 以前的版本大多没有问题,但 ...

  8. 导出数据到Excel方法总结

    一,问题的提出 近来在网上经常有人问怎样把数据导出到Excel中?针对这个问题网上也有很多资料.大都比较的琐碎.本人当前从事的项目中,刚好涉及到这些内容.就顺便做了一些归纳整理.共享给大家.避免大家再 ...

  9. phpexcel如何读取excel的数据和如何导出数据到excel

    phpexcel如何读取excel的数据和如何导出数据到excel 一.总结 一句话总结:去官网看参考手册和api,或者找中文的博客或者参考手册 1.phpexcel插件如何下载? 其实这些插件不仅可 ...

  10. 从DataTable高效率导出数据到Excel

    首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: using Microsoft.Office.I ...

随机推荐

  1. String类的subString(a,b)方法(基于jdk 1.9)

    基于上文:http://www.jianshu.com/p/a20ee3bb9c1b public String substring(int beginIndex, int endIndex) { i ...

  2. wget下载指定目录下的文件

    wget -r -np -k -P  ~/tmp/    http://xxx.com/download -P 表示下载到哪个目录-r 表示递归下载-np 表示不下载旁站连接.-k 表示将下载的网页里 ...

  3. bzoj1001平面图最小割转对偶图最短路

    https://www.lydsy.com/JudgeOnline/problem.php?id=1001 很明显的求对偶图的最短路即可(由于特判写错了一直wa = = ) //#pragma com ...

  4. ZOJ 2599 Graduated Lexicographical Ordering ★(数位DP)

    题意 定义两个数的比较方法,各位数字之和大的数大,如果数字和相等则按字典序比较两个数的大小.输入n,k,求:1.数字k的排名:2.排名为k的数. 思路 算是一类经典的统计问题的拓展吧~ 先来看第一问. ...

  5. 基于centos的docker安装

    1. 安装需求 内核版本3.10以上 Centos 7以上 64位版本 2. 使用root登录或者具有sudo权限 3. 确保系统是最新的 yum update 4. 添加yum源 tee /etc/ ...

  6. C++高级编程2. 静态动态链接库

    C++高级编程2. 静态动态链接库20131018 1.动态链接库和静态链接库的区别: 静态链接库就是把lib文件中用到的函数代码直接连接进目标程序,程序运行的时候不在需要其他的库文件:动态链接库是把 ...

  7. C++(笔记)浅析vector容器的实例

    http://www.runoob.com/w3cnote/cpp-vector-container-analysis.html 转载请注明出处 浅然的专栏 https://blog.csdn.net ...

  8. CocoaPods Mac App的安装和使用

    CocoaPods桌面应用版下载地址:https://cocoapods.org/app打开应用会提示你是否安装命令行工具,选择install就也可以在命令行使用Pod了.省去了上面的步骤们,方便快捷 ...

  9. ES获取磁盘使用率情况

    private void diskUage() { ClusterStateResponse stateResponse = client.admin().cluster().prepareState ...

  10. APUE学习笔记——5.5~5.7数据流的打开与读写

    1.open #include <stdio.h> FILE *fopen(const char *restrict pathname,const char *restrict type) ...