Export导出工具-java

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值