您的位置:首页 > 其它

POI导出Excel文档通用工具方法

2016-01-30 17:10 507 查看
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
* @description Excel(.xlsx格式)导出通用工具类
* 1.该工具通过传入参数可以自定义行和列
* 2.该工具类时终端系统默认的通用导出格式,即文档标题,标题下方导出参数,表格表头,表格数据等
* 3.使用示例详见终端-我的权益计划-权益列表导出
* @date 2016-1-26
* @author ZhangLP
*/
public class ExcelExportUtil {

/**************************************************方法说明start*******************************************************
*  导出excel主方法
*  @param list 需要导出的数据list,对该list的要求:必须是一个Bean对象类型的list,Bean可以时任意一个
*  @param requestmap 参数要求如下:
*      1.该map泛型为<String,Object><br>
*      2.该map中必须包含th,td,param,paramName以及若干其它值,示例如下:<br>
*
*        ①表头标题,与下面的反射方法名个数及顺序一一对应(序号一项除外)用英文半角下划线隔开<br>
*        map.put("th", "序号_频道_节目_品牌");<br>
*
*        ②表格行所对应的实体反射方法名,必须与导出传入list中的实体get方法一致,用英文半角下划线隔开<br>
*        map.put("td", "getChannelName_getProgramName_getTypeName");<br>
*
*        ③表格表头上方查询参数数据获取字段定义,必须与上面1.导出参数中的名称一致,它将作为导出文档标题下方的条件,用英文半角下划线隔开<br>
*        map.put("param", "channelName_programName_companyName_brandName");<br>
*
*        ④表格表头上方查询参数字段标题,与③中的名称对应,作为③中数据的说明,它将作为导出文档下方的条件的说明,用英文半角下划线隔开<br>
*        map.put("paramName", "频道:_节目:_厂商:_品牌:");<br>
*
*        ⑤若干条件字段值,与③中的值对应<br>
*        map.put("channelName","...");<br>
*        map.put("programName","...");<br>
*        map.put("companyName","...");<br>
*        map.put("brandName","...");<br>
*  @return 返回XSSFWorkbook对象,将用于转化输出流
*****************************************************方法说明end******************************************************/
public XSSFWorkbook exportExcel(List<? extends Object> list,Map<String,Object> requestmap) {
XSSFWorkbook wb = new XSSFWorkbook();            // 创建一个webbook
XSSFSheet sheet = wb.createSheet("sheet");        // 在webbook中添加一个sheet
XSSFRow row = null;                                // 在sheet中声明行
XSSFCell cell;
message(requestmap,wb,row,sheet);            // 条件区处理
//标题行样式
XSSFCellStyle titleStyle=XExcelUtil.borderTitle(wb);
//内容行样式
XSSFCellStyle bodyStyle=XExcelUtil.borderBody(wb);

//获取上一行行号
int lastNumber=sheet.getLastRowNum();
row = XExcelUtil.createHeightRow(sheet,lastNumber+1);

//表头th
String th = (String)requestmap.get("th");
for(int i=0; i < th.split("_").length; i++) {
cell = row.createCell((int) i);                // 列数
cell.setCellValue(th.split("_")[i]);            // 列名称
cell.setCellStyle(titleStyle);                    // 列样式
}

//表格行td
String td = (String)requestmap.get("td");
for(int i = 0; i < list.size(); i++) {
row = XExcelUtil.createHeightRow(sheet,(int) i + lastNumber+2);
row.createCell(0).setCellValue(i+1);//序号自增
try {
//循环填充每行的每个字段,根据传入的实体方法名反射得到list中的实体字段内容
Class<? extends Object> listClass = list.get(i).getClass();
for(int j = 0; j < td.split("_").length; j++){
row.createCell(j+1).setCellValue(listClass.getMethod(td.split("_")[j]).invoke(list.get(i))+"");
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}

//循环填充样式
for(int k=0;k<row.getLastCellNum();k++){
row.getCell(k).setCellStyle(bodyStyle);
}

}
//列宽自适应
for(int i=0; i < th.split("_").length; i++) {
sheet.autoSizeColumn(0, true);
sheet.autoSizeColumn(i, true);
}
return wb;

}

/**
* 头信息处理(title及条件区及部分样式)
* @param list
* @param requestmap
* @param wb
* @param row
* @param sheet
*/
private static void message(Map<String,Object> requestmap, XSSFWorkbook wb,XSSFRow row,XSSFSheet sheet){
sheet.setDisplayGridlines(false);//设置隐藏网格线

//最后一行行号
int lastNumber=0;
sheet.addMergedRegion(new CellRangeAddress(0, (short) 0, 0, (short) 1));//跨一行2列
//条件行样式
XSSFCellStyle conditionStyle=XExcelUtil.conditionCellStyle(wb);

row = XExcelUtil.createHeightRow(sheet,0);
XSSFCell firstXSSFCell = row.createCell(0);
firstXSSFCell.setCellType(XSSFCell.CELL_TYPE_STRING);
firstXSSFCell.setCellValue(new XSSFRichTextString("XXXXX"));
firstXSSFCell.setCellStyle(XExcelUtil.companyCellStyle(wb));//设置单元格的样式

//条件key:通过它可以从map中获取条件名称,例如:湖南卫视
String param = (String)requestmap.get("param");
String[] paramArr = param.split("_");
//条件说明:例如:频道
String paramName = (String)requestmap.get("paramName");
String[] paramNameArr = paramName.split("_");
//循环条件写入条件区
for(int i = 0; i < paramArr.length; i ++){
lastNumber=sheet.getLastRowNum();
row = XExcelUtil.createHeightRow(sheet,lastNumber+1);
if(StringUtils.isNotEmpty((String)requestmap.get(paramArr[i]))){
row.createCell(0).setCellValue(paramNameArr[i]);
row.createCell(1).setCellValue((String)requestmap.get(paramArr[i]));
}else{
row.createCell(0).setCellValue(paramNameArr[i]);
row.createCell(1).setCellValue("无限制");
}
}

//条件区与数据区空一行
lastNumber=sheet.getLastRowNum();
row = XExcelUtil.createHeightRow(sheet,lastNumber+1);
for(int i=1;i<sheet.getLastRowNum()+1;i++){
row=sheet.getRow(i);
for(int j=0;j<row.getLastCellNum();j++){
row.getCell(j).setCellStyle(conditionStyle);
}
}

}

}


XExcelUtil工具类

/** 设置excel的company title样式       */
public static XSSFCellStyle companyCellStyle(XSSFWorkbook wb) {
XSSFFont boldFont = wb.createFont();
boldFont.setFontHeight((short) 260);
boldFont.setFontName("微软雅黑");
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* excel (条件区黑体居左)
*/
public static XSSFCellStyle conditionCellStyle(XSSFWorkbook wb){
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT); // 左对齐
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中
XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setFontHeightInPoints((short) 10);    //设置字体大小
cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* 数据表格标题样式:07版本
*/
public static CellStyle borderTitle(SXSSFWorkbook wb) {
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下边框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中

Font boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); // 字体增粗
boldFont.setFontHeightInPoints((short) 10); // 设置字体大小
// boldFont.setColor(HSSFColor.WHITE.index);
cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* 数据表格数据区样式:07版本
*/
public static CellStyle borderBody(SXSSFWorkbook wb) {
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); // 下边框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// 左边框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);// 上边框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// 右边框
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中

Font boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setFontHeightInPoints((short) 10); // 设置字体大小
cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* 数据表格标题样式
*/
public static XSSFCellStyle borderTitle(XSSFWorkbook wb){
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中

XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);     //字体增粗
boldFont.setFontHeightInPoints((short) 10);            //设置字体大小
//boldFont.setColor(HSSFColor.WHITE.index);

cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* 数据表格标题样式
*/
public static XSSFCellStyle borderTitleWeixin(XSSFWorkbook wb){
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中

cellStyle.setFillBackgroundColor(IndexedColors.SKY_BLUE.getIndex());
cellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);     //字体增粗
boldFont.setFontHeightInPoints((short) 18);            //设置字体大小
boldFont.setColor(IndexedColors.WHITE.index);

cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* 数据来源样式
*/
public static XSSFCellStyle SourceBorder(XSSFWorkbook wb){
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT); // 左右居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中

XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);     //字体增粗
boldFont.setFontHeightInPoints((short) 10);            //设置字体大小

cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* 数据来源样式
*/
public static XSSFCellStyle SourceBorderWeixin(XSSFWorkbook wb){
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT); // 左右居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中

cellStyle.setFillBackgroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
cellStyle.setFillForegroundColor(new XSSFColor( new Color(225, 245, 255)));
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);     //字体增粗
boldFont.setFontHeightInPoints((short) 18);            //设置字体大小

cellStyle.setFont(boldFont);
return cellStyle;
}
/**
* 数据表格数据区样式
*/
public static XSSFCellStyle borderBody(XSSFWorkbook wb){
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中

XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setFontHeightInPoints((short) 10);            //设置字体大小
cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* 数据表格数据区样式
*/
public static XSSFCellStyle borderBodyWeixin(XSSFWorkbook wb){
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 左右居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);// 上下居中

//设置背景色
cellStyle.setFillBackgroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
cellStyle.setFillForegroundColor(new XSSFColor( new Color(225, 245, 255)));
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);

XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setFontHeightInPoints((short) 18);            //设置字体大小
cellStyle.setFont(boldFont);
return cellStyle;
}

/**
* 大标题(居中)
* 软广栏目对比
*/
public static XSSFCellStyle styleBig(XSSFWorkbook wb) {
XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);         //字体增粗
boldFont.setFontHeightInPoints((short) 20);// 设置字体大小

XSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
style.setFillForegroundColor((short)7);

style.setBorderBottom(XSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(XSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(XSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(XSSFCellStyle.BORDER_THIN);//右边框
return style;
}

/**
* 小标题及条件(居左)
* 软广栏目对比
*/
public static XSSFCellStyle styleSmall(XSSFWorkbook wb) {
XSSFFont boldFont = wb.createFont();
boldFont.setFontName("微软雅黑");
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);         //字体增粗
boldFont.setFontHeightInPoints((short) 10);// 设置字体大小

XSSFCellStyle style = wb.createCellStyle();
style.setFont(boldFont);
style.setFillForegroundColor((short)7);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
return style;
}

/**
* 创建行,并附行高
* (short)15.625  一个像素1px的值
*/
public static XSSFRow createHeightRow(XSSFSheet sheet,int rowNumber){
XSSFRow row=sheet.createRow(rowNumber);
row.setHeight((short)(27.25*20.00));
return row;
}

/**
* 创建行,并附行高
* (short)15.625  一个像素1px的值
*/
public static XSSFRow createTitleHeightRow(XSSFSheet sheet,int rowNumber){
XSSFRow row=sheet.createRow(rowNumber);
row.setHeight((short)(48.625*20.00));
return row;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: