/**
*
* @param out 输出流
* @param maplist 数据
* @param title 标题
* @param headers 表头
* @param keys 表头对应的字段名
* @return
*/
public static boolean getExcelDao(OutputStream out,
List<Map<String, Object>> maplist, String title, String[] headers,String keys[]) {
try {
// 创建一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建一个带有名称的工作页
HSSFSheet sheet = workbook.createSheet(title); sheet.setDefaultColumnWidth(25); // 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font); // 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2); // 产生坐标行
// 标题行
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellStyle(style); cell.setCellValue(title);
// 使标题居中,合并单元格 四个参数 起始行列 结束行列
if (headers != null && headers.length > 0)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
headers.length - 1));
else
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1)); // 产生表头
row = sheet.createRow(1);
for (int i = 0; i < headers.length; i++) {
cell = row.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(headers[i]);
}
// 产生内容
for (int i = 0; i < maplist.size(); i++) { Map<String, Object> objectmap = maplist.get(i);
row = sheet.createRow(i + 2);
int count = 0;//遍历MAP
for(String key:keys){
cell = row.createCell(count);
cell.setCellStyle(style2);
cell.setCellValue(String.valueOf(objectmap.get(key)).equals("null")?"":objectmap.get(key)+"");
count++;
}
/*for (Entry<String, Object> entry : objectmap.entrySet()) {
// System.out.println("key= " + entry.getKey() +
// " and value= " + entry.getValue());
cell = row.createCell(count);
cell.setCellValue(entry.getValue().toString());
count++;
}*/ }
try {
workbook.write(out);
return true;
} catch (IOException e) { e.printStackTrace();
return false;
}finally {
workbook.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
} 使用
String keys[]={"id","mname","o3title","num","userid","useraddress","Getphone","postname","postage","flagname","buildtime","kjtime"};
String headers[]={"订单号","品类","商品名称","夺宝次数","用户ID","地址","号码","快递","快递单号","订单状态","下单时间","开奖时间"};
//orderServiceImpl.query_queryListAll(query, columns, tablename, sort)
//System.out.println(maplist.toString());
OutputStream outputStream = response.getOutputStream();
ExcelUtil.getExcelDao(outputStream, maplist2, title, headers, keys);
outputStream.close();


java数据导出成 EXCEL的更多相关文章

  1. Pl/sql 如何将oracle的表数据导出成excel文件?

    oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...

  2. Extjs — Grid数据导出成Excel

    最近因为项目问题,需要解决Extjs导出成Excel的问题. 下面简单描述这个问题解决的步骤如下: 1.先在js文件里写了一个button的handler事件,通过点击按钮,来实现调用ExportEx ...

  3. jquery.table2excel,将HTML的table标签数据导出成excel

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...

  4. 使用SSM框架实现Sql数据导出成Excel表

    SSM框架实现SQL数据导出Excel 思路 首先在前端页面中添加一个导出功能的button,然后与后端controller进行交互. 接着在相应的controller中编写导出功能方法. 方法体: ...

  5. 将数据导出成excel表

    /// <summary> /// 生成excel表 /// </summary> /// <param name="dt">数据表</p ...

  6. 有趣的Node爬虫,数据导出成Excel

    最近一直没更新了诶,因为学习Backbone好头痛,别问我为什么不继续AngularJs~因为2.0要出来了啊,妈蛋!好,言归正传,最近帮我的好基友扒数据,他说要一些股票债券的数据.我一听,那不就是要 ...

  7. 将DataTable中的数据导出成Excel

    public bool ExportFile(System.Data.DataTable dt){    SaveFileDialog sfd = new SaveFileDialog();    s ...

  8. mysql中数据导出成excel文件语句

    代码如下 复制代码 mysql>select * from xi_table into outfile ’d:test.xls’; 导出为txt文件:  代码如下 复制代码 select * f ...

  9. 史上最简单的在 Yii2.0 中将数据导出成 Excel

    在 vendor/yiisoft/yii2/helpers/ 创建一个 Excel.php <?php namespace yii\helpers;   class Excel{         ...

随机推荐

  1. UNION 和UNION ALL

    UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...

  2. IOS自适应库---- Masonry的使用

    Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性,而且同时支持 iOS 和 Max OS X.Masonry是一个用代码写iOS或OS ...

  3. C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP (转)

    namespace YongFa365.Validator { using System; using System.Text.RegularExpressions; /**//// <summ ...

  4. java一点东西(3)

    运算符的优先级:()优先级最高 ! ++ -- 单目运算符 * / % + - > < <= >= == != && || 赋值符号 面向对象设计步骤:1.发现 ...

  5. Gengxin讲STL系列目录

    引言:有人催我写关于STL的博客#(滑稽)        STL嘛,昨晚有人一直逼问我STL名字的由来——STL = Standard Template Library,标准模板库,惠普实验室开发的一 ...

  6. ubuntu 新建zend framework 项目

    1. sudo apt-get install zend-framework /usr/share/php/libzend-framework-php 2. 配置include_path /etc/p ...

  7. 在head标签里加一个meta标签让指定ie使用特定内核 解决css在ie中的兼容性问题

    <meta http-equiv="x-ua-compatible" content="IE=edge, chrome=1"/> IE=edge: ...

  8. Excel导出cs文件

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  9. 在 .NET Framework 2.0上使用LINQ

    附件:System.Linq.dll.7z 此为从System.Core.dll中剥离的Linq,含有System.Linq.Enumerable类所有扩展方法,可以在客户只安装了.Net 2.0的环 ...

  10. LeeCode-Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...