package com.fulan.util.excel;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class ExportUtil {
/**
* 设置边框
* @param style 样式
*/
public static void border(XSSFCellStyle style){
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);//填充方式
style.setBorderBottom(BorderStyle.THIN);//设置下边框线条
style.setBorderLeft(BorderStyle.THIN);//设置右边框线条
style.setBorderRight(BorderStyle.THIN);//设置左边框线条
style.setBorderTop(BorderStyle.THIN);//设置上边框线条
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());//下边框
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());//左边框
style.setRightBorderColor(IndexedColors.BLACK.getIndex());//右边框
style.setTopBorderColor(IndexedColors.BLACK.getIndex());//下边框
}
/**
*
* @param r red
* @param g green
* @param b blue
* @param style 样式
*/
public static void color(int r,int g,int b,XSSFCellStyle style){
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);//填充方式
XSSFColor color = new XSSFColor(new Color(r,g,b),null);//获取背景色
style.setFillForegroundColor(color);//填充颜色
}
/**
*
* @param style
*/
public static void alignment(XSSFCellStyle style) {
style.setAlignment(HorizontalAlignment.CENTER);//创建居中格式
}
/**
* 设置字体
* @param wb 为了获取字体
* @param index 字体颜色下标 零不设置颜色 列子:HSSFColor.HSSFColorPredefined.WHITE.getIndex();
* @Param Bold 是否加粗
* @Param name 字体样式
* @param style 样式
*/
public static void font(XSSFWorkbook wb,short index,Boolean bold,String name,XSSFCellStyle style){
HSSFColor.HSSFColorPredefined.WHITE.getIndex();
XSSFFont font = wb.createFont();//获取字体
if (index != 0) {
font.setColor(index);//设置字体颜色
}
if (bold) {
font.setBold(bold);//字体加粗
}
if (!StringUtils.isEmpty(name)){
font.setFontName(name);//字体样式
}
style.setFont(font);//放入样式
}
/**
* 合并单元格
* @param firstRow 行起始格
* @param lastRow 行起终止格
* @param firstCol 列起始格
* @param lastCol 列起终止格
* @param sheet
*/
public static void CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol, XSSFSheet sheet){
CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
sheet.addMergedRegion(cellRangeAddress);
}
/**
*
* @param list 集合
* @param row 行
* @param style 样式
*/
public static void forList(List<String> list, XSSFRow row,XSSFCellStyle style){
for (String s:list){
XSSFCell cell1 = row.createCell(list.indexOf(s));
cell1.setCellValue(s);
cell1.setCellStyle(style);
}
}
/**
*
* @param list 集合
* @param row 行
* @param style 样式
* @param k 第几格开始
* @param y y-k等于循环次数
*/
public static void forList(List<String> list, XSSFRow row,XSSFCellStyle style,int k,int y){
for (int i = k; i < y; i++) {
XSSFCell cell1 = row.createCell(i);
cell1.setCellValue(list.get(i-k));
cell1.setCellStyle(style);
}
}
/**
*关闭资源
* @param file
* @param wb
* @param os
*/
public static void close(File file, XSSFWorkbook wb, FileOutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (wb != null) {
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (file != null) {
file.delete();
}
}
/**
* 返回空字符串
* @param s
* @return
*/
public static String isEmpty(String s){
if (StringUtils.isEmpty(s)) {
return “”;
}
return s;
}
}