感觉poi工具类项目不能很好使用,简单封装了几个样式设置的工具类。里面有个引入涉及项目,****代替
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.style.StyleUtil;
*******************************************
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
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.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/***
* POI扩展工具类,可指定sheet页写入数据
*/
public class ExcelHelper {
/**
* 初始化excel写入器,初始化完成后写入器只包含一个指定名称的sheet
*
* @param sheetName sheet名称
* @return
*/
public static ExcelWriter initExcelWriter(String sheetName) {
ExcelWriter writer = ExcelUtil.getWriter();
writer.getWorkbook().setSheetName(0, sheetName);
return writer;
}
/**
* 初始化excel写入器,初始化完成后写入器只包含一个指定名称的sheet
*
* @param sheetName sheet名称
* @return
*/
public static ExcelWriter initExcel2007Writer(String sheetName) {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
ExcelWriter writer = new ExcelWriter(xssfWorkbook, sheetName);
return writer;
}
/**
* 设置excel写入器当前sheet的列宽
*
* @param columnCount 指定列数,从第一列开始算起
* @param width 宽度
*/
public static void setColumnWidth(ExcelWriter excelWriter, int columnCount, int width) {
for(int i = 0; i < columnCount; i++) {
excelWriter.setColumnWidth(i, width);
}
}
/**
* 指定的行和列添加下拉列表,并保护数据的有效性
*
* @param sheet 写入器对象的sheet页
* @param dropDownText 下拉列表的内容选项
* @param firstRow 开始行,行号下标从0开始,从第1行开始时不包含标题
* @param lastRow 结束行
* @param firstCol 开始列,列号下标从0开始
* @param lastCol 结束列
*/
public static void setDropDownText(Sheet sheet, String[] dropDownText, int firstRow, int lastRow, int firstCol,
int lastCol)
{
set2007DropDownText(sheet, dropDownText, firstRow, lastRow, firstCol, lastCol);
}
/**
* 适用于2003 和 2007 使用隐藏域设置长文本下拉框
*
* 需要在数据写完后再做下拉处理
*/
public static void set0307DropDownLongText(Sheet sheet, String[] dropDownText, int firstRow, int lastRow,
int firstCol, int lastCol)
{
//创建一个隐藏域sheet,将下拉的值写入隐藏的sheet
Workbook workbook = sheet.getWorkbook();
String sheetName = "hidden" + workbook.getNumberOfSheets();
Sheet hiddenSheet = workbook.createSheet(sheetName);
for(int i = 0, length = dropDownText.length; i < length; i++) {
hiddenSheet.createRow(i).createCell(0).setCellValue(dropDownText[i]);
}
int sheetIndex = workbook.getSheetIndex(sheetName);
workbook.setSheetHidden(sheetIndex, Boolean.TRUE);
//设置下拉的区域值
String strFormula = sheetName + "!$A$1:$A$" + dropDownText.length;
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint formulaListConstraint = helper.createFormulaListConstraint(strFormula);
CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
DataValidation validation = helper.createValidation(formulaListConstraint, regions);
//强制选择下拉内容,兼容处理
if(validation instanceof XSSFDataValidation) {
validation.setSuppressDropDownArrow(true);
validation.createErrorBox("输入错误", "请从下拉列表中取值");
validation.setShowErrorBox(true);
}
else {
validation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(validation);
}
/**
* 设置2007版excel下拉框,并设置下拉选择为强制项
*
* @param sheet sheet页
* @param dropDownText 下拉内容
* @param firstRow 区域第一行
* @param lastRow 下拉区域最后一行
* @param firstCol 下拉区域第一列
* @param lastCol 下拉区域最后一列
*/
public static void set2007DropDownText(Sheet sheet, String[] dropDownText, int firstRow, int lastRow, int firstCol,
int lastCol)
{
DataValidationHelper helper = sheet.getDataValidationHelper();
// 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
// 数据有效性对象
XSSFDataValidation dstDataValidation =
(XSSFDataValidation) helper.createValidation(helper.createExplicitListConstraint(dropDownText), regions);
//强制从下拉框中选择内容
dstDataValidation.createErrorBox("输入错误", "请从下拉列表中取值");
dstDataValidation.setShowErrorBox(true);
sheet.addValidationData(dstDataValidation);
}
/**
* 给excel写入器sheet1中,指定单元格添加提示信息(非下拉)
*
* @param sheet 写入器的sheet页
* @param promptTitle 弹出的提示信息标题
* @param promptMsg 弹出的提示信息内容
* @param firstRow 开始行,行号下标从0开始,从第1行开始时不包含标题
* @param lastRow 结束行
* @param firstCol 开始列,列号下标从0开始
* @param lastCol 结束列
*/
public static void setCellsPromptMsg(Sheet sheet, String promptTitle, String promptMsg, int firstRow, int lastRow,
int firstCol, int lastCol)
{
// 构造constraint对象,BB1没有约束就相当于创建了一个没有约束的单元格
DVConstraint constraint = DVConstraint.createCustomFormulaConstraint("BB1");
// 四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
// 数据有效性对象
HSSFDataValidation data_validation_view = new HSSFDataValidation(regions, constraint);
data_validation_view.createPromptBox(promptTitle, promptMsg);
sheet.addValidationData(data_validation_view);
}
/**
* 创建新的sheet页,并将切换到第一行
* 创建或切换完后将写入器的行数切换的第一行
* 设置标题列
*
* @param excelWriter 写入器
* @param columnCount 从第一列开始一共多少列需要设置
* @param width 需要设置的宽度
* @param sheetName 新建的sheetname
*/
public static void createSheet(ExcelWriter excelWriter, int columnCount, int width, String sheetName) {
excelWriter.setOrCreateSheet(sheetName);
excelWriter.resetRow();
ExcelHelper.setColumnWidth(excelWriter, columnCount, width);
}
/**
* 创建或切换sheet页
* 创建时使用默认格式
* 切换时使用切换的目标sheet格式
*
* @param excelWriter
* @param sheetName
*/
public static void createOrSetSheet(ExcelWriter excelWriter, String sheetName) {
excelWriter.setOrCreateSheet(sheetName);
excelWriter.resetRow();
}
/**
* 写保护某列数据
*
* @param sheet sheet页
* @param columnIndex 列下标,从0开始
*/
public static void protectSheetColumn(Sheet sheet, int startRowIndex, int endRowIndex, int columnIndex) {
Workbook workbook = sheet.getWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setLocked(Boolean.TRUE);
setCellFormat(sheet, startRowIndex, endRowIndex, columnIndex, cellStyle);
}
/**
* 设置某列单元格的自定义格式
*
* @param sheet
* @param startRowIndex 开始行
* @param endRowIndex 结束行
* @param columnIndex 列数
* @param format 单元格自定义格式的表达公式
*/
public static void setCellDefinedFormat(Sheet sheet, int startRowIndex, int endRowIndex, int columnIndex,
String format)
{
Workbook workbook = sheet.getWorkbook();
CellStyle cellStyle = StyleUtil.createDefaultCellStyle(workbook);
cellStyle.setDataFormat(workbook.createDataFormat().getFormat(format));
setCellFormat(sheet, startRowIndex, endRowIndex, columnIndex, cellStyle);
}
/**
* 设置单元格格式
*
* @param sheet
* @param startRowIndex
* @param endRowIndex
* @param columnIndex
* @param cellStyle
*/
private static void setCellFormat(Sheet sheet, int startRowIndex, int endRowIndex, int columnIndex,
CellStyle cellStyle)
{
for(int i = startRowIndex; i <= endRowIndex; i++) {
Row row = sheet.getRow(i);
if(Tool.isNullOrEmpty(row)) {
return;
}
Cell cell = row.getCell(columnIndex);
if(Tool.isNullOrEmpty(cell)) {
return;
}
cell.setCellStyle(cellStyle);
}
}