public class ExcelExportUtiler {
private static Log log = LogFactory.getLog(ExcelExportUtiler.class);
private HSSFWorkbook workbook;
private int defaultColumnWidth = 20;
private String sheetName;
private HSSFFont defaultTitleFont;
private HSSFFont defaultContentFont;
private HSSFCellStyle defaultTitleStyle;
private HSSFCellStyle defaultContentStyle;
private int lastSheetIndex = 0;
public HSSFWorkbook getWorkbook() {
return workbook;
}
public int getDefaultColumnWidth() {
return defaultColumnWidth;
}
public String getSheetName() {
return sheetName;
}
public HSSFFont getDefaultTitleFont() {
return defaultTitleFont;
}
public HSSFFont getDefaultContentFont() {
return defaultContentFont;
}
public HSSFCellStyle getDefaultTitleStyle() {
return defaultTitleStyle;
}
public HSSFCellStyle getDefaultContentStyle() {
return defaultContentStyle;
}
public ExcelExportUtiler(){
this(null,0,null,null,null,null,null);
}
public ExcelExportUtiler(int defaultColumnWidth){
this(defaultColumnWidth,null);
}
public ExcelExportUtiler(String sheetName){
this(0,sheetName);
}
public ExcelExportUtiler(int defaultColumnWidth, String sheetName){
this(null,defaultColumnWidth,sheetName);
}
public ExcelExportUtiler(HSSFWorkbook workbook, int defaultColumnWidth, String sheetName){
this(workbook,defaultColumnWidth,sheetName,null,null,null,null);
}
public ExcelExportUtiler(HSSFWorkbook workbook, int defaultColumnWidth, String sheetName, HSSFFont defaultTitleFont, HSSFFont defaultContentFont, HSSFCellStyle defaultTitleStyle, HSSFCellStyle defaultContentStyle){
if(workbook == null){
this.workbook = new HSSFWorkbook();
}else{
this.workbook = workbook;
}
if(defaultColumnWidth == 0){
this.defaultColumnWidth = 20;
}else{
this.defaultColumnWidth = defaultColumnWidth;
}
if(sheetName == null || "".equals(sheetName.trim())){
this.sheetName = "sheet";
}else{
this.sheetName = sheetName;
}
if(defaultTitleFont == null){
this.defaultTitleFont = getDefaultTitleFont(this.workbook);
}else{
this.defaultTitleFont = defaultTitleFont;
}
if(defaultContentFont == null){
this.defaultContentFont = getDefaultContentFont(this.workbook);
}else{
this.defaultContentFont = defaultContentFont;
}
if(defaultTitleStyle == null){
this.defaultTitleStyle = getDefaultTitleStyle(this.workbook);
}else{
this.defaultTitleStyle = defaultTitleStyle;
}
if(defaultContentStyle == null){
this.defaultContentStyle = getDefaultContentStyle(this.workbook);
}else{
this.defaultContentStyle = defaultContentStyle;
}
}
public static HSSFFont getDefaultTitleFont(HSSFWorkbook workbook){
HSSFFont titleFont = workbook.createFont();
titleFont.setColor(HSSFColor.BLACK.index);
titleFont.setFontHeightInPoints((short) 9);
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
titleFont.setFontName("微软雅黑");
return titleFont;
}
public static HSSFFont getDefaultContentFont(HSSFWorkbook workbook){
HSSFFont contentFont = workbook.createFont();
contentFont.setColor(HSSFColor.BLACK.index);
contentFont.setFontHeightInPoints((short) 9);
contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
contentFont.setFontName("微软雅黑");
return contentFont;
}
public static HSSFCellStyle getDefaultTitleStyle(HSSFWorkbook workbook){
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
titleStyle.setFont(getDefaultTitleFont(workbook));
titleStyle.setWrapText(true);
return titleStyle;
}
public static HSSFCellStyle getDefaultContentStyle(HSSFWorkbook workbook){
HSSFCellStyle contentStyle = workbook.createCellStyle();
contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
contentStyle.setFont(getDefaultContentFont(workbook));
contentStyle.setWrapText(true);
return contentStyle;
}
/**
* 提示用户下载excel
* @param response 响应对象
* @param fileName excel文件名
* create by ronghui.xiao @2015-8-5
*/
public static void downloadExcel(HSSFWorkbook workbook , HttpServletResponse response , String fileName){
try{
if(StringUtils.isNotBlank(fileName)){
fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
}else{
fileName = "excel";
}
response.setContentType("text/html;charset=utf-8");
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.close();
}catch (IOException e){
log.error(e);
}
}
/**
* 提示用户下载excel
* @param response 响应对象
* @param fileName excel文件名
* create by ronghui.xiao @2015-8-5
*/
public void downloadExcel(HttpServletResponse response , String fileName){
try{
if(StringUtils.isNotBlank(fileName)){
fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
}else{
fileName = "excel";
}
response.setContentType("text/html;charset=utf-8");
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.close();
}catch (IOException e){
log.error(e);
}
}
/**
* 提示用户下载excel
* @function 对ie浏览器和firefox进行了兼容,不会出现乱码问题浏
* @author junqiang.qiu
* @date 2016年12月8日
*/
public void downloadExcel(HttpServletRequest request,HttpServletResponse response,String fileName ){
String agent = request.getHeader("USER-AGENT").toLowerCase();
String codedFileName;
try {
codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
if (agent.contains("firefox")) {
response.setCharacterEncoding("utf-8");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xls");
} else {
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
}
response.setContentType("text/html;charset=utf-8");
response.setContentType("application/x-msdownload");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.close();
} catch (Exception e) {
log.error(e);
}
}
/**
* @function 创建Excel,在服务器端或者是本地
* @author junqiang.qiu
* @date 2017年1月12日
*/
public void createExcel(String path,String fileName){
try {
String param=null;
param=path+File.separator+fileName+".xls";
FileOutputStream fos=new FileOutputStream(param);
workbook.write(fos);
fos.close();
} catch (Exception e) {
log.info("创建excel失败"+e);
}
}
/**
* 添加标题行
* @param titles 标题集合
* @param rowHeight 行高
*/
public void addTitlesRow(Collection titles , int rowHeight){
try{
if(titles != null && rowHeight > 0){
int rowIndex = 0;
HSSFSheet sheet = workbook.getSheet(sheetName);
if(sheet == null){
sheet = workbook.createSheet(sheetName);
}else{
sheet = workbook.getSheetAt(lastSheetIndex);
rowIndex = sheet.getLastRowNum() + 1;
}
if(rowIndex > 65535){
lastSheetIndex ++;
sheet = workbook.createSheet(sheetName + lastSheetIndex);
rowIndex = 0;
}
sheet.setDefaultColumnWidth(defaultColumnWidth);
HSSFRow row = sheet.createRow(rowIndex);
row.setHeightInPoints(rowHeight);
Iterator iterator = titles.iterator();
int index = 0;
while(iterator.hasNext()){
String title = iterator.next();
HSSFCell cell = row.createCell(index);
cell.setCellValue(title);
cell.setCellStyle(defaultTitleStyle);
index ++ ;
}
}
}catch (Exception e){
log.error("添加标题行发生错误==>" , e);
}
}
/**
* 添加内容行
* @param titleKeyMap 标题和mapList中key的对应
* @param mapList 内容集合
* @param rowHeight 内容行行高
*/
public void addContentRow(Map titleKeyMap , List