这个是项目中用到的时候,做的一个类,
package com.topwalk.iwp.pluging;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
public class CreateExcel {
private static XSSFFont font;
private static XSSFWorkbook workbook;
private static XSSFSheet sheet;
/**
* 创建Excel文件,返回File文件对象
* @param excelDataBean
*/
public static File createExcelReturnFile(ExcelDataBean excelDataBean,String IWPTitle){
//创建WorkBook对象
workbook=new XSSFWorkbook();
font=createFont(workbook,(short)0);
FileOutputStream out=null;
/*设置文件名*/
String src="https://my.oschina.net//huiger/blog/224616/ExcelFile/";
File dir=new File(src);
if(!dir.exists()){
dir.mkdir();
}
String filename=RandomStringUtils.randomAlphanumeric(10);
filename=new StringBuffer(src).append(filename).append(".xlsx").toString();
File file=new File(filename);
try {
file.createNewFile();
//创建SHEET
sheet = workbook.createSheet();
workbook.setSheetName(0, "查询结果");
//设置默认高度,默认宽度
sheet.setDefaultColumnWidth((short)25);
//sheet.setDefaultRowHeightInPoints(20);
sheet.setTabColor(123);
//创建头
createHeadRow(workbook, sheet,IWPTitle);
//写入IWP返回的查询信息
putValueToExcel(excelDataBean);
out=new FileOutputStream(file);
} catch (Exception e) {
}finally{
try {
workbook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
/**
*
* @param excelDataBean
* @param IWPTitle
* @return
*/
public static XSSFWorkbook createExcelReturnXSSFWorkbook(ExcelDataBean excelDataBean,String IWPTitle){
//创建WorkBook对象
workbook=new XSSFWorkbook();
font=createFont(workbook,(short)0);
FileOutputStream out=null;
/*设置文件名*/
String src="";
String filename=RandomStringUtils.randomAlphanumeric(10);
filename=new StringBuffer(src).append(filename).append(".xlsx").toString();
File file=new File(filename);
try {
//file.createNewFile();
//创建SHEET
sheet = workbook.createSheet();
workbook.setSheetName(0, "查询结果");
//设置默认高度,默认宽度
sheet.setDefaultColumnWidth((short)25);
//sheet.setDefaultRowHeightInPoints(20);
sheet.setTabColor(123);
//创建头
createHeadRow(workbook, sheet,IWPTitle);
//写入IWP返回的查询信息
putValueToExcel(excelDataBean);
out=new FileOutputStream(file);
} catch (Exception e) {
}finally{
try {
workbook.write(out);
out.close();
file.deleteOnExit();
} catch (Exception e) {
e.printStackTrace();
}
}
return workbook;
}
/**
* 设置CellStyle格式
* @param workbook
* @return
*/
public static XSSFCellStyle createCellStyle(XSSFWorkbook workbook){
XSSFCellStyle cellStyle=workbook.createCellStyle();
// 设置单元格边框样式
// CellStyle.BORDER_DOUBLE 双边线
// CellStyle.BORDER_THIN 细边线
// CellStyle.BORDER_MEDIUM 中等边线
// CellStyle.BORDER_DASHED 虚线边线
// CellStyle.BORDER_HAIR 小圆点虚线边线
// CellStyle.BORDER_THICK 粗边线
//getAllPersonBean
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
//创建字体
Font fontHeader=workbook.createFont();
//字体号码
fontHeader.setFontHeightInPoints((short)10);
//字体名称
fontHeader.setFontName("宋体");
cellStyle.setFont(fontHeader);
return cellStyle;
}
/**
* 竖向合并表格
* @param sheet
* @param rownumber
* @param columnsize
*/
public static void AddMergedRegion(XSSFSheet sheet,int columnsize,int lastRowNum,int rownum){
for (int i = 0; i <=columnsize; i++) {
sheet.addMergedRegion(new CellRangeAddress(lastRowNum,lastRowNum+rownum-1, i,i));
}
}
/**
* 功能:创建CellStyle样式
* @param wb XSSFWorkbook
* @param backgroundColor 背景色
* @param foregroundColor 前置色
* @param font 字体
* @return CellStyle
*/
public static CellStyle createCellStyle(XSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){
CellStyle cs=wb.createCellStyle();
cs.setAlignment(halign);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFillBackgroundColor(backgroundColor);
cs.setFillForegroundColor(foregroundColor);
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
cs.setFont(font);
return cs;
}
/**
* 功能:创建带边框的CellStyle样式
* @param wb XSSFWorkbook
* @param backgroundColor 背景色
* @param foregroundColor 前置色
* @param font 字体
* @return CellStyle
*/
public static CellStyle createBorderCellStyle(XSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){
CellStyle cs=wb.createCellStyle();
cs.setAlignment(halign);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFillBackgroundColor(backgroundColor);
cs.setFillForegroundColor(foregroundColor);
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
cs.setFont(font);
cs.setBorderLeft(CellStyle.BORDER_DASHED);
cs.setBorderRight(CellStyle.BORDER_DASHED);
cs.setBorderTop(CellStyle.BORDER_DASHED);
cs.setBorderBottom(CellStyle.BORDER_DASHED);
return cs;
}
/**
* 功能:创建CELL
* @param row HSSFRow
* @param cellNum int
* @param style HSSFStyle
* @return HSSFCell
*/
public static XSSFCell createCell(XSSFRow row,int cellNum,CellStyle style){
XSSFCell cell=row.createCell(cellNum);
cell.setCellStyle(style);
return cell;
}
/**
* 功能:合并单元格
* @param sheet XSSFSheet
* @param firstRow int
* @param lastRow int
* @param firstColumn int
* @param lastColumn int
* @return int 合并区域号码
*/
public static int mergeCell(XSSFSheet sheet,int firstRow,int lastRow,int firstColumn,int lastColumn){
return sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,firstColumn,lastColumn));
}
/**
* 功能:创建字体
* @param wb XSSFWorkbook
* @param boldweight short
* @param color short
* @return Font
*/
public static Font createFont(XSSFWorkbook wb,short boldweight,short color,short size){
Font font=wb.createFont();
font.setBoldweight(boldweight);
font.setColor(color);
font.setFontHeightInPoints(size);
return font;
}
/**
* 创建Head
* @param wb
* @param sheet
*/
private static void createHeadRow(XSSFWorkbook wb, XSSFSheet sheet,String IWPTitle){
XSSFRow row=sheet.createRow(0);
XSSFFont font=createFont(wb, (short)18);
// 创建单元格样式
XSSFColor color=new XSSFColor(new Color(36, 142, 195));
XSSFCellStyle style =createStyle(wb, color);
style.setFont(font);// 设置字体
XSSFCell cell=row.createCell(0);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
//cell.setCellStyle(style);
//合并单元个(第0行到第一行合并,)
sheet.addMergedRegion(new CellRangeAddress(0,1,0,10));
//设置合并单元格的边框(合并一行用row(0)
//setCellBorder(0,10,row,style);
//合并两行,必须要用roe(1)才能设置,设置边框
setCellBorder(0,10,sheet.createRow(1),style);
setCellBorder(0,10,row,style);
//设置文件头
cell.setCellValue(IWPTitle);
cell.setCellStyle(style);
}
/**
* 合并单元格加边框 水平
* @param sheet
* @param region
* @param cs
*/
public static void setCellBorder(int start, int end, XSSFRow row, XSSFCellStyle style) {
for(int i=start;i<=end;i++){
XSSFCell cell = row.createCell(i);
cell.setCellValue("");
cell.setCellStyle(style);
}
}
/**
* 创建字体
* @param workbook
* @param size 字体大小
* @return
*/
private static XSSFFont createFont(XSSFWorkbook workbook,short size){
XSSFFont font=workbook.createFont();
//字体样式
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
//字体颜色
font.setColor(XSSFFont.COLOR_NORMAL);
//字体大小
if(0==size){
font.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
}else{
font.setFontHeightInPoints(size);
}
font.setFontName("微软雅黑");
return font;
}
/**
* 创建CellStyle
* @param workbook
* @param XSSFColor color 颜色
*/
private static XSSFCellStyle createStyle(XSSFWorkbook workbook,XSSFColor color){
XSSFCellStyle cellStyle=workbook.createCellStyle();
//对齐样式
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//设置颜色
cellStyle.setFillForegroundColor(color);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setBorderTop(BorderStyle.MEDIUM);
cellStyle.setBorderBottom(BorderStyle.MEDIUM);
cellStyle.setBorderLeft(BorderStyle.MEDIUM);
cellStyle.setBorderRight(BorderStyle.MEDIUM);
//设置边框颜色
cellStyle.setBorderColor(XSSFCellBorder.BorderSide.TOP,new XSSFColor(Color.BLACK));
cellStyle.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM,new XSSFColor(Color.BLACK));
cellStyle.setBorderColor(XSSFCellBorder.BorderSide.LEFT,new XSSFColor(Color.BLACK));
cellStyle.setBorderColor(XSSFCellBorder.BorderSide.RIGHT,new XSSFColor(Color.BLACK));
//设置字体
cellStyle.setFont(font);
//设置自动换行
cellStyle.setWrapText(true);
return cellStyle;
}
/**
* 将数据填充到Excel中去
* @param bean
*/
private static void putValueToExcel(ExcelDataBean bean){
//查询时间
String queryDate = bean.getQueryDate();
//查询参数
String queryParam = bean.getQueryParam();
//查询结果
List<QueryResult> queryResultList = bean.getQueryResult();
//写第一行信息,查询参数,查询时间 row 为2
writeParamRow(queryParam, queryDate);
//遍历 写查询获得是真正结果 空一行 row从4开始
for (int i = 0; i < queryResultList.size(); i++) {
QueryResult queryResult = queryResultList.get(i);
//服务名称(空一行,另起一行 服务名称:达梦数据库查询)
//服务名称行样式
XSSFCellStyle serviceNameRowStyle=createStyle(workbook, new XSSFColor(new Color(255,255,255)));
//每个服务方法结果时间空一行,所以加2
XSSFRow serviceNameRow = sheet.createRow(sheet.getLastRowNum()+2);
XSSFCell keyCell = serviceNameRow.createCell(0);
keyCell.setCellValue("服务名称");
keyCell.setCellStyle(serviceNameRowStyle);
XSSFCell valueCell=serviceNameRow.createCell(1);
valueCell.setCellValue(queryResult.getServiceName());
valueCell.setCellStyle(serviceNameRowStyle);
//开始写查询结果
List<List<String>> resultList = queryResult.getList();
for (int j = 0; j < resultList.size(); j++) {
//遍历,这里的每一个rowValueList就对应Excel文件上的一行
List<String> rowValueList = resultList.get(j);
//如果是第一个对象,则是查询字段的字段名,表头,设置样式
if(j==0){
XSSFRow tableHeadRow=sheet.createRow(sheet.getLastRowNum()+1);
//创建Style 有边框,字体,单元格背景等
XSSFCellStyle headRowStyle=createStyle(workbook, new XSSFColor(new Color(155,187,89)));
for (int k = 0; k < rowValueList.size(); k++) {
XSSFCell cell=tableHeadRow.createCell(k);
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
cell.setCellValue(rowValueList.get(k));
cell.setCellStyle(headRowStyle);
}
//如果不是第一个对象,则是服务查询返回的结果信息,需要判断结果中是不是有空信息,设置背景色为白色
}else{
XSSFRow tableHeadRow=sheet.createRow(sheet.getLastRowNum()+1);
XSSFCellStyle tableValueRowStyle=createStyle(workbook, new XSSFColor(new Color(255,255,255)));
for (int k = 0; k < rowValueList.size(); k++) {
XSSFCell cell=tableHeadRow.createCell(k);
//用正则判断,如果值为数据,则将Cell的格式设置为数据格式,如果不能匹配,则设置成字符串格式
if((rowValueList.get(k)).matches("^[1-9]\\d*$")){
cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
}else{
cell.setCellType(XSSFCell.CELL_TYPE_STRING);
}
//如果服务查询结果中,某一个字段信息是空值,则用""填充,避免有空指针错误出现
if(null==rowValueList.get(k)){
cell.setCellValue("");
}else{
cell.setCellValue(rowValueList.get(k));
}
cell.setCellStyle(tableValueRowStyle);
}
}
}
}
}
/**
* 写第一行信息
* @param queryParam 查询参数
* @param queryDate 查询时间
*/
private static void writeParamRow(String queryParam,String queryDate){
//黄色
XSSFCellStyle paramRowStyle=createStyle(workbook, new XSSFColor(new Color(255,235,156)));
//红色
XSSFCellStyle valueRowStyle=createStyle(workbook, new XSSFColor(new Color(255,199,206)));
//设置第三行显示查询参数和查询时间
XSSFRow paramRow = sheet.createRow(2);
XSSFCell paramKeyCell = paramRow.createCell(0);
paramKeyCell.setCellValue("查询参数");
paramKeyCell.setCellStyle(paramRowStyle);
XSSFCell paramValueCell = paramRow.createCell(1);
paramValueCell.setCellValue(queryParam);
paramValueCell.setCellStyle(valueRowStyle);
XSSFCell dateKeyCell = paramRow.createCell(2);
dateKeyCell.setCellValue("查询时间");
dateKeyCell.setCellStyle(paramRowStyle);
XSSFCell dateValueCell = paramRow.createCell(3);
dateValueCell.setCellValue(queryDate);
dateValueCell.setCellStyle(valueRowStyle);
}
}