java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar

工具类调用例如以下:

package com.qlwb.business.util;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress; /**
* 导出报表的公共类,提供公共的导出报表的方法
* @Copyright: Copyright (c) 2015
* @Company:
* @author 鹿伟伟
*
*/
public class ExportExcel { static boolean tempDirflag1 = true;
static boolean tempDirflag2 = true; /**
* 依据传入的集合数据生成报表的方法
*
* @param list
* 报表标题表头及数据封装的list,格式:list的第一条数据是标题。String类型;第二条数据是表头列表,List类型;从第三条開始是数据列表
* ,List类型
* @param filename
* 报表excel的文件名称,不须要扩展名
* @param response
* HttpServletResponse
* @return 1表示成功 0表示失败
* @throws IOException
* @Create luweiwei 2015-08-18
* @change
*/
@SuppressWarnings("unchecked")
public static int createReport(List<Object> list, String filename,
HttpServletResponse response) throws IOException { // 数据list至少包含标题和表头
if (list.size() < 2) {
return 0;
} // 组装excel
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(); // 设置Excel标题字体和样式
HSSFFont headFont = workbook.createFont();
headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headFont.setFontHeightInPoints((short) 20);
HSSFCellStyle headCellStyle = workbook.createCellStyle();
headCellStyle.setFont(headFont);
// headCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// headCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
// headCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
// headCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headCellStyle.setWrapText(true);
headCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// headCellStyle.setFillForegroundColor(GREEN_COLOR); // 设置Excel表头的字体和样式
HSSFFont thfont = workbook.createFont();
thfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle thCellStyle = workbook.createCellStyle();
thCellStyle.setFont(thfont);
thCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
thCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
thCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
thCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
thCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
thCellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
thCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 设置Excel内容的字体和样式
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
HSSFCellStyle titleCellStyle = workbook.createCellStyle();
titleCellStyle.setFont(font);
titleCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
titleCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
titleCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
titleCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
// titleCellStyle.setFillForegroundColor(GREEN_COLOR); // 单元格居左样式
HSSFCellStyle cellLeft = null; // 单元格居中样式
HSSFCellStyle cellCenter = null; // 单元格居右样式
HSSFCellStyle cellRight = null; // 单元格居左样式
cellLeft = workbook.createCellStyle();
cellLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 单元格居中样式
cellCenter = workbook.createCellStyle();
cellCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 单元格居右样式
cellRight = workbook.createCellStyle();
cellRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellRight.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellRight.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT); // 第一行
// 合并标题单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,
((List<String>) list.get(1)).size() - 1));
HSSFRow row = sheet.createRow(0);
row.setHeightInPoints(40);
HSSFCell cell = null;
cell = row.createCell(0);
cell.setCellStyle(headCellStyle);
cell.setCellValue((String) (list.get(0))); // 设置表头单元格
List<String> tmpList = null;
tmpList = (List<String>) list.get(1);
int rownum = sheet.getLastRowNum();
row = sheet.createRow(rownum + 1);
for (int i = 0; i < tmpList.size(); i++) {
sheet.setColumnWidth(i, 6000);
cell = row.createCell(i);
cell.setCellStyle(thCellStyle);
cell.setCellValue(tmpList.get(i));
} // 循环设置数据的单元格
for (int i = 2; i < list.size(); i++) {
tmpList = (List<String>) list.get(i);
rownum = sheet.getLastRowNum();
row = sheet.createRow(rownum + 1);
for (int j = 0; j < tmpList.size(); j++) {
cell = row.createCell(j);
cell.setCellStyle(titleCellStyle);
cell.setCellValue(tmpList.get(j));
}
}
response.setHeader("Content-disposition", "attachment; filename="
+ URLEncoder.encode(filename, "utf-8") + ".xls");
response.setContentType("application/msexcel;charset=utf-8");
OutputStream out = null;
try {
out = response.getOutputStream();
out.flush();
workbook.write(out);
} finally {
try {
if(out!=null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return 1;
}
}

java导出excel工具类的更多相关文章

  1. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  2. 导入导出Excel工具类ExcelUtil

    前言 前段时间做的分布式集成平台项目中,许多模块都用到了导入导出Excel的功能,于是决定封装一个ExcelUtil类,专门用来处理Excel的导入和导出 本项目的持久化层用的是JPA(底层用hibe ...

  3. javaEE开发之导出excel工具类

    web开发中,一个系统的普通需求也包含导出excel,一般採用POI做统计报表导出excel. 导出excel工具类: import java.io.FileOutputStream; import ...

  4. java 解析excel工具类

      java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...

  5. java操作excel 工具类

    java操作excel 可参考https://blog.csdn.net/xunwei0303/article/details/53213130 直接上代码: 一.java生成excel文件: pac ...

  6. Java解析Excel工具类(兼容xls和xlsx)

    依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...

  7. Java XSSF 导出excel 工具类

    参数解释: title:导出excel标题.headers 导出到excel显示的列头. columns 对应数据库字段 .list 导出数据1.pox中添加依赖 <dependency> ...

  8. 导出Excel工具类

    import java.io.OutputStream; import java.lang.reflect.Method; import java.text.SimpleDateFormat; imp ...

  9. 使用POI导出EXCEL工具类并解决导出数据量大的问题

    POI导出工具类 工作中常常会遇到一些图表需要导出的功能,在这里自己写了一个工具类方便以后使用(使用POI实现). 项目依赖 <dependency> <groupId>org ...

随机推荐

  1. iis 7上发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容

    iis 7上发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 提示里面的解决方法是: 如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. 使用 II ...

  2. Emgu CV

    摄像头 Capture _capture = new Capture(0); _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PR ...

  3. Orchard模块开发全接触7:订单与支付之Event Bus

    在这部分,我们要完成的工作有: 1:将购物车内的商品变成真正的订单: 2:理解 父子及一对多关系: 3:写一个针对 Event Bus 的扩展点: 4:实现一个针对该扩展点的模拟的 支付服务: 一:创 ...

  4. tf.argmax

    tf.argmax(input, axis=None, name=None, dimension=None) Returns the index with the largest value acro ...

  5. Web用户的身份验证及WebApi权限验证流程的设计和实现 asp.net mvc AllowAnonymous 不起作用, asp.net mvc 匿名访问

    原文地址: https://blog.csdn.net/zjlovety/article/details/17095627 前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个 ...

  6. GIST特征描述符使用

    来源:http://www.cnblogs.com/justany/archive/2012/12/06/2804211.html 一种场景特征描述 场景特征描述? 通常的特征描述符都是对图片的局部特 ...

  7. html5各种页面切换效果和模态对话框

    页面动画:data-transition 属性可以定义页面切换是的动画效果.例如:<a href="index.html" data-transition="pop ...

  8. 如何基于TensorFlow使用LSTM和CNN实现时序分类任务

    https://www.jiqizhixin.com/articles/2017-09-12-5 By 蒋思源2017年9月12日 09:54 时序数据经常出现在很多领域中,如金融.信号处理.语音识别 ...

  9. ICTCLAS中的HMM人名识别

    http://www.hankcs.com/nlp/segment/ictclas-the-hmm-name-recognition.html 本文主要从代码的角度分析标注过程中的细节,理论谁都能说, ...

  10. javascript——选择行之后才可以进行控制操作