这两天在研究导出excel的功能,简单的写了一个写excel样式的工具类,在此分享一下:
import java.util.Calendar;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
public class ExcelStyle {
/**
* 设置sheet页列宽
* @param sheet
* @param widthArr int类型的数组
*/
public static void setColumnWidth(Sheet sheet, int[] widthArr){
try {
for(int i = 0; i < widthArr.length; i++){
sheet.setColumnWidth(i, widthArr[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置字体的样式 字体大小 字体加粗 字体颜色
* @param wb
* @param boldWeight 字体加粗 没有样式为null
* @param fontHeightInPoints 设置字体大小 默认为null
* @param color 字体颜色 没有改样式为null
* @return Font
*/
public static Font setFontStyle(Workbook wb,Short boldWeight, Short fontHeightInPoints, Short color){
Font font = wb.createFont();
try {
if(boldWeight != null){
font.setBoldweight(boldWeight); //设置字体加粗
}
if(fontHeightInPoints != null){
font.setFontHeightInPoints(fontHeightInPoints); //字体大小
}
if(color != null){
font.setColor(color); //设置字体颜色
}
} catch (Exception e) {
e.printStackTrace();
}
return font;
}
/**
* 设置单元格样式 垂直样式 水平样式 字体样式
* @param wb
* @param verticalAlignment 垂直样式
* @param alignment 水平样式
* @param font 字体样式
* @return CellStyle
*/
public static CellStyle setCellStyle(Workbook wb,Short verticalAlignment,Short alignment, Font font){
CellStyle cellStyle = wb.createCellStyle();
try {
if(verticalAlignment != null){
cellStyle.setVerticalAlignment(verticalAlignment); //设置单元格垂直样式
}
if(alignment != null){
cellStyle.setAlignment(alignment); //设置单元格水平样式
}
if(font != null){
cellStyle.setFont(font); //设置单元格字体样式
}
} catch (Exception e) {
e.printStackTrace();
}
return cellStyle;
}
/**
* 合并单元格并且设置单元格格式
* @param sheet
* @param rownum 第几行从0开始
* @param fistCol 合并的开始列数 从0开始
* @param lastCol 合并的结束列数 从0开始
* @param cellStyle 单元格格式 没有格式为null
* @param cellHeight 行高 不设置行高为null
* @param value 该合并单元格的数据为
*/
public static void margeCellAndSetCellStyle(Sheet sheet,int rownum,int fistCol,int lastCol,CellStyle cellStyle, Float cellHeight, String value){
try {
Row row = sheet.createRow(rownum); //创建行
Cell cell = row.createCell(0);
CellRangeAddress region = new CellRangeAddress(rownum, rownum, fistCol, lastCol); //合并单元格 rownum为第几行 从第fistCol列到lastCol
sheet.addMergedRegion(region);
if(cellStyle != null){
cell.setCellStyle(cellStyle); //设置单元格的样式
}
if(cellHeight != null){
row.setHeightInPoints(cellHeight); //设置行高
}
if(value != null){
cell.setCellValue(value); //添加内容
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取日期格式的字符串 形式为:“日期:yyyy年MM月dd日”
* @return String
*/
public static String getDateString(){
String dateString = "";
try {
Calendar c = Calendar.getInstance();
int day = c.get(5);
int year = c.get(1);
int month = c.get(2) + 1;
dateString = (new StringBuilder("日期:")).append(year).append("年").append(month).append("月").append(day).append("日").toString();
} catch (Exception e) {
e.printStackTrace();
}
return dateString;
}
/**
* 设置excel的标题 标题的行高 标题的单元格样式
* @param sheet
* @param rownum 行号 从0开始
* @param titleArr 标题的String数组
* @param rowHeight 行高
* @param cellStyle 单元格的样式
*/
public static void setExcelTitle(Sheet sheet,int rownum, String[] titleArr, Float rowHeight, CellStyle cellStyle){
try {
Row row = sheet.createRow(rownum); //创建行
if(rowHeight != null){
row.setHeightInPoints(rowHeight); //设置行高
}
for(int i = 0; i < titleArr.length; i++){
Cell cell = row.createCell(i); //创建单元格
cell.setCellValue(titleArr[i]); //设置单元格的内容
if(cellStyle != null){
cell.setCellStyle(cellStyle); //设置单元格的样式
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
service导出excel的方法为:
public String[] exportExcel(QueryParamList param, PageInfo pageInfo, String path){
String back[] = new String[3];
String fileName = "localCompany.xls";
List<Organization> orgList = retrieve(pageInfo,param);
Workbook wb = new HSSFWorkbook();
String groupId = null;
try{
Sheet sheet = wb.createSheet("localCompany");
//设置列宽 A-D列为 0-3
int[] widthArr = {6120,5120,5120,5120};
ExcelStyle.setColumnWidth(sheet, widthArr);
//设置正文的单元格的样式(垂直对齐,和水平对齐)
CellStyle cellStyle = ExcelStyle.setCellStyle(wb, CellStyle.VERTICAL_CENTER, CellStyle.ALIGN_CENTER, null);
//设置标题列单元格的样式(垂直对齐,和水平对齐)
Font titleFontStyle = ExcelStyle.setFontStyle(wb, HSSFFont.BOLDWEIGHT_NORMAL, (short)12, null);
CellStyle titleCellStyle = ExcelStyle.setCellStyle(wb, CellStyle.VERTICAL_CENTER, CellStyle.ALIGN_CENTER, titleFontStyle);
//设置大标题单元格的样式(垂直对齐,和水平对齐,字体大小)
Font fontStyle1 = ExcelStyle.setFontStyle(wb, HSSFFont.BOLDWEIGHT_BOLD, (short)20, null);
CellStyle fistCellStyle = ExcelStyle.setCellStyle(wb, CellStyle.VERTICAL_CENTER, CellStyle.ALIGN_CENTER, fontStyle1);
//合并第一列设置第一列的格式
ExcelStyle.margeCellAndSetCellStyle(sheet, 0, 0, 3, fistCellStyle, 40F, "本地商家信息表");
//合并第二列设置第二列的格式
String dateString = ExcelStyle.getDateString();
ExcelStyle.margeCellAndSetCellStyle(sheet, 1, 0, 3, cellStyle, 40F, dateString);
//设置第三列的格式
ExcelStyle.margeCellAndSetCellStyle(sheet, 2, 0, 3, null, null, null);
//设置标题栏
String[] titleArr = {"商家名称","有效期开始时间","有效期结束时间","备注"};
ExcelStyle.setExcelTitle(sheet, 3, titleArr, 30F, titleCellStyle);
int start = 3;
for(int i = 0; i < orgList.size(); i++){
Organization org = (Organization)orgList.get(i);
start++;
Row row = sheet.createRow(start);
row.setHeightInPoints(30F);
Cell ce1 = row.createCell(0);
ce1.setCellValue(org.getGroupName());
ce1.setCellStyle(cellStyle);
Cell ce2 = row.createCell(1);
if(org.getFromDate() != null){
ce2.setCellValue(org.getFromDate().toString());
ce2.setCellStyle(cellStyle);
}
Cell ce3 = row.createCell(2);
if(org.getThruDate() != null){
ce3.setCellValue(org.getThruDate().toString());
ce3.setCellStyle(cellStyle);
}
Cell ce4 = row.createCell(3);
ce4.setCellValue(org.getRemark());
ce4.setCellStyle(cellStyle);
}
String outpath = (new StringBuilder(String.valueOf(path))).append(fileName).toString();
OutputStream out = new FileOutputStream(new File(outpath));
wb.write(out);
back[0] = outpath;
back[1] = "0";
back[2] = fileName;
out.close();
}
catch(Exception e)
{
back[1] = "-1";
e.printStackTrace();
}
return back;
}
欢迎大家的指正和借鉴。