1、数据bean

public class ExcelBean {

    private String name;

    private String sheetName;

    private ExcelTitle[] titles;

    private List<String[]> dataList;

    private boolean headBold = true;

    /**
* 列宽 (像素)
*/
private int columnWidth = 200; private int rowHeight; public ExcelBean(String name, String sheetName, ExcelTitle[] titles){
this.name = name;
this.sheetName = sheetName;
this.titles = titles;
this.dataList = new ArrayList<String[]>();
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSheetName() {
return sheetName;
} public void setSheetName(String sheetName) {
this.sheetName = sheetName;
} public ExcelTitle[] getTitles() {
return titles;
} public void setTitles(ExcelTitle[] titles) {
this.titles = titles;
} public List<String[]> getDataList() {
return dataList;
} public void setDataList(List<String[]> dataList) {
this.dataList = dataList;
} public boolean isHeadBold() {
return headBold;
} public void setHeadBold(boolean headBold) {
this.headBold = headBold;
} public int getColumnWidth() {
return columnWidth;
} public void setColumnWidth(int columnWidth) {
this.columnWidth = columnWidth;
} public int getRowHeight() {
return rowHeight;
} public void setRowHeight(int rowHeight) {
this.rowHeight = rowHeight;
} public void add(String[] data){
this.dataList.add(data);
} }
/**
* excel 块状区域
* @author yanglizhe
*
*/
public class ExcelBox {
private int x1; private int y1; private int x2; private int y2; public ExcelBox(int width, int height, int colWidth, int rowHeight, int padding){
double ratio = 1;
int innerWidth = colWidth - 2 * padding;
int innerHeight = rowHeight - 2 * padding;
if((double) width / height > (double) innerWidth/ innerHeight){
if(width > innerWidth){
ratio = (double) innerWidth / width;
}
}
else{
if(height > innerHeight){
ratio = (double) innerHeight / height;
}
} int boxWidth = (int)(width * ratio);
int boxHeight = (int)(height * ratio);
x1 = (colWidth - boxWidth) / 2;
y1 = (rowHeight - boxHeight) / 2;
x2 = x1 + boxWidth;
y2 = y1+ boxHeight;
} public int getX1() {
return x1;
} public void setX1(int x1) {
this.x1 = x1;
} public int getY1() {
return y1;
} public void setY1(int y1) {
this.y1 = y1;
} public int getX2() {
return x2;
} public void setX2(int x2) {
this.x2 = x2;
} public int getY2() {
return y2;
} public void setY2(int y2) {
this.y2 = y2;
}
}
/**
* excel 图片
* @author yanglizhe
*
*/
public class ExcelImage {
private int width; private int height; private byte[] byteArray; public ExcelImage(String imageUrl) throws IOException{
BufferedImage bufferedImage = ImageIO.read(new URL(imageUrl));
ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"png", byteArrayOutputStream);
width = bufferedImage.getWidth();
height = bufferedImage.getHeight();
byteArray = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
} public int getWidth() {
return width;
} public void setWidth(int width) {
this.width = width;
} public int getHeight() {
return height;
} public void setHeight(int height) {
this.height = height;
} public byte[] getByteArray() {
return byteArray;
} public void setByteArray(byte[] byteArray) {
this.byteArray = byteArray;
}
}
/**
* Excel Title
* @author yanglizhe
*
*/
public class ExcelTitle { private String value; /**
* 列宽(像素)
*/
private int width; public static ExcelTitle generate(String value, int width){
ExcelTitle title = new ExcelTitle();
title.setValue(value);
title.setWidth(width);
return title;
} public String getValue() {
return value;
} public void setValue(String value) {
this.value = value;
} public int getWidth() {
return width;
} public void setWidth(int width) {
this.width = width;
}
}

2、工具类

/**
* excel 工具类
* @author yanglizhe
*
*/
public class ExcelUtils { public static void export( ExcelBean excelBean, HttpServletResponse response) throws Exception{
String filename = excelBean.getName();
filename = new String(filename.replaceAll("\\s|;", "").getBytes("gbk"), "ISO8859-1"); response.setContentType("application/octet-stream;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + filename);
OutputStream outputStream = response.getOutputStream();
export(excelBean, outputStream);
} @SuppressWarnings("resource")
public static void export( ExcelBean excelBean, OutputStream outputStream) throws Exception{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(excelBean.getSheetName());
HSSFRow row = sheet.createRow(0); //设置样式
HSSFCellStyle style = wb.createCellStyle();
if(excelBean.isHeadBold()){
HSSFFont headfont = wb.createFont();
headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFont(headfont);
} HSSFCell cell;
ExcelTitle[] titles = excelBean.getTitles();
for(int i=0; i < titles.length; i++){
ExcelTitle title = titles[i];
cell= row.createCell(i);
cell.setCellValue(title.getValue());
cell.setCellStyle(style);
int columnWidth = title.getWidth() > 0 ? title.getWidth() : excelBean.getColumnWidth();
sheet.setColumnWidth(i, getColWidth(columnWidth));
} int rowNumber = 1;
int rowHeihgt = excelBean.getRowHeight();
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
for(String[] data : excelBean.getDataList()){
row = sheet.createRow(rowNumber ++ );
if(rowHeihgt > 0){
row.setHeight((short) getRowHeight(rowHeihgt));
}
else{
rowHeihgt = 18;
}
for(int j=0; j<data.length; j ++){
String value = data[j];
cell = row.createCell(j); if(isUrl(value)){
if(isImage(value)){
int columnWidth = titles[j].getWidth() > 0 ? titles[j].getWidth() : excelBean.getColumnWidth();
ExcelImage excelImage = new ExcelImage(value);
ExcelBox excelBox = new ExcelBox(excelImage.getWidth(), excelImage.getHeight(), rowHeihgt, columnWidth, 10);
HSSFClientAnchor anchor = new HSSFClientAnchor();
int cw = getColWidth(columnWidth);
int rh = getRowHeight(rowHeihgt);
short col = (short)(j);
int rowNum = rowNumber-1;
anchor.setDx1(getAnchorX(excelBox.getX1(), cw));
anchor.setDy1(getAnchorY(excelBox.getY1(), rh));
anchor.setDx2(getAnchorX(excelBox.getX2(), cw));
anchor.setDy2(getAnchorY(excelBox.getY2(), rh));
anchor.setCol1(col);
anchor.setRow1(rowNum);
anchor.setCol2(col);
anchor.setRow2(rowNum); anchor.setAnchorType(0);
patriarch.createPicture(anchor , wb.addPicture(excelImage.getByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
}
else{
cell.setCellValue(value);
cell.setCellFormula("HYPERLINK(\"" + value + "\",\"" + value + "\")");
HSSFCellStyle linkStyle = wb.createCellStyle();
HSSFFont cellFont= wb.createFont();
cellFont.setUnderline((byte) 1);
cellFont.setColor(HSSFColor.BLUE.index);
linkStyle.setFont(cellFont);
linkStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cell.setCellStyle(linkStyle);
}
}
else{
cell.setCellValue(value);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cell.setCellStyle(cellStyle);
}
}
} wb.write(outputStream);
outputStream.flush();
outputStream.close(); } /**
* 获取图片x方向长度坐标转换
* @param px
* @param colWidth
* @return
*/
public static int getAnchorX(int px, int colWidth){
return (int) Math.round(( (double) 701 * 16000.0 / 301)*((double)1/colWidth)*px);
} /**
* 获取图片y方向长度坐标转换
* @param px
* @param rowHeight
* @return
*/
public static int getAnchorY(int px, int rowHeight){
return (int) Math.round(( (double) 144 * 8000 / 301)*((double)1/rowHeight)*px);
} /**
* 行高转换
* @param px
* @return
*/
public static int getRowHeight( int px ){
return (int) Math.round(((double) 4480 / 300 ) * px);
} /**
* 列宽转换
* @param px
* @return
*/
public static int getColWidth( int px ){
return (int) Math.round(((double) 10971 / 300 ) * px);
} /**
* 判断是否为链接地址
*/
public static boolean isUrl(String string){
Pattern pattern = Pattern.compile("^((http|https):\\/\\/([\\w\\-]+\\.)+[\\w\\-]+(\\/[\\w\\u4e00-\\u9fa5\\-\\.\\/?\\@\\%\\!\\&=\\+\\~\\:\\#\\;\\,]*)?)", Pattern.CASE_INSENSITIVE );
return pattern.matcher(string).matches();
} /**
* 判断是否为图片
*/
public static boolean isImage(String string){
Pattern pattern = Pattern.compile("\\S+\\.(jpg|jpeg|png|gif|bmp)(\\?\\S+)?$", Pattern.CASE_INSENSITIVE );
return isUrl(string) && pattern.matcher(string).matches();
} }

3、Demo

List<PictureTopVo> list = statisticsService.topList(from, to, limit, orderBy);
ExcelTitle[] titles = {
ExcelTitle.generate("图片名称", 400),
ExcelTitle.generate("缩略图", 100),
ExcelTitle.generate("访问次数", 100),
ExcelTitle.generate("回复次数", 100)};
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String fileName = format.format(from) + "至"+ format.format(to) + "TOP" + limit +"统计.xls";
ExcelBean excelBean = new ExcelBean(fileName, "统计", titles);
for(PictureTopVo pictureTopVo : list){
excelBean.add(new String[]{pictureTopVo.getName(), pictureTopVo.getPath() + "?size=80", String.valueOf(pictureTopVo.getVisitCount()), String.valueOf(pictureTopVo.getFillCount())});
}
excelBean.setRowHeight(100);
ExcelUtils.export(excelBean, respone);

4、maven

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>

JAVA导出Excel封装的更多相关文章

  1. Workbook导出excel封装的工具类

    在实际中导出excel非常常见,于是自己封装了一个导出数据到excel的工具类,先附上代码,最后会写出实例和解释.支持03和07两个版本的 excel. HSSF导出的是xls的excel,XSSF导 ...

  2. [转载]Java导出Excel

    一.需求介绍 当前B/S模式已成为应用开发的主流,而在开发企业办公系统的过程中,常常有客户这样子要求:把系统数据库中的数据导出到Excel,用户查看报表时直接用Excel打开.或者是:用户已经习惯用E ...

  3. java导出excel报错:getOutputStream() has already been called for this response

    对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...

  4. java导出excel表格

    java导出excel表格: 1.导入jar包 <dependency> <groupId>org.apache.poi</groupId> <artifac ...

  5. java导出excel报表

    1.java导出excel报表: package cn.jcenterhome.util; import java.io.OutputStream;import java.util.List;impo ...

  6. Java导出Excel和CSV(简单Demo)

    Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...

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

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

  8. java导出excel模板数据

    Java导出excel数据模板,这里直接贴代码开发,流程性的走下去就是步骤: String[] colName=new String[]{"期间","科目代码" ...

  9. java导出excel工具类

    java导出excel须要使用HSSFWorkbook这个类,须要导入poi-3.6-20091214.jar 工具类调用例如以下: package com.qlwb.business.util; i ...

随机推荐

  1. 随着visual studio 2013 发布.带来的一些变化

    1.asp.net a.在2013中, asp.net走向了统一.使用不同的asp.net 框架搭(web forms ,api, mvc )建混合应用 b.身份验证 无身份验证 个人用户账户 (窗体 ...

  2. vim自动补全

    Vim 中使用 OmniComplete 为 C/C++ 自动补全 OmniComplete 并不是插件的名字,而是 Vim 众多补全方式中的一种(全能补全).说白了 OmniComplete 其实就 ...

  3. <jsp:forward>、requestDispatcher和sendRedirect()的区别

    1.会话信息保存在服务器内存上,可以断续访问,和cookie相比,其保存在服务器上. 2.男人就像蓝牙:只有在你接近时,他才会找上你.当你离开后,他便又去找其他的"设备"了.女人就 ...

  4. matlab的cell数组

    matlab的cell数组 元胞数组: 元胞数组是MATLAB的一种特殊数据类型,可以将元胞数组看做一种无所不包的通用矩阵,或者叫做广义矩阵.组成元胞数组的元素可以是任何一种数据类型的常数或者常量,每 ...

  5. Count The Pairs

    hdu4750:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给你一个带权无向图,然后让你求这样的点对s,t,使得s--t的所有路径上的最大的边的 ...

  6. 深入了解一下PYTHON中关于SOCKETSERVER的模块-A

    有了这块知识,应该对各类WEB框架有更好的理解吧..FLASK,DJANGO,WEBPY.... #!/usr/bin/env python from BaseHTTPServer import HT ...

  7. activiti入门2流程引擎的API和服务基础

      RepositoryService : 管理和控制发布包和流程定义(包含了一个流程每个环节的结构和行为)的操作 除此之外,服务可以 查询引擎中的发布包和流程定义. 暂停或激活发布包,对应全部和特定 ...

  8. ifstream文件尾最后一行读两次

    看下面一段代码:       ifstream m_fileConfig;     string str;     m_fileConfig.open(FILE_OPERATORS, ios::out ...

  9. HDU-2188 悼念512汶川大地震遇难同胞——选拔志愿者

    http://acm.hdu.edu.cn/showproblem.php?pid=2188 巴什博奕(Bash Game)的转换:换一种说法而已 悼念512汶川大地震遇难同胞——选拔志愿者 Time ...

  10. yarn的初步理解

    查考site: http://hadoop.apache.org/docs/r2.6.0/hadoop-yarn/hadoop-yarn-site/YARN.html yarn结构图如下: 1.yar ...