|
|
|
@ -1,403 +0,0 @@
|
|
|
|
|
package com.dsideal.Base.Util;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.usermodel.DateUtil;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.*;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class PoiUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:对于指定Cell中文字进行加红色(*)提醒
|
|
|
|
|
*
|
|
|
|
|
* @param wb
|
|
|
|
|
* @param cell
|
|
|
|
|
*/
|
|
|
|
|
public static void addStar(XSSFWorkbook wb, XSSFCell cell) {
|
|
|
|
|
XSSFFont fontRed = wb.createFont();//第一种字体
|
|
|
|
|
fontRed.setBold(true);
|
|
|
|
|
fontRed.setColor(IndexedColors.RED.getIndex());
|
|
|
|
|
fontRed.setFontHeightInPoints((short) 14);
|
|
|
|
|
fontRed.setFontName("黑体");
|
|
|
|
|
|
|
|
|
|
XSSFFont fontBlack = wb.createFont();//第二种字体
|
|
|
|
|
fontBlack.setColor(IndexedColors.BLACK.getIndex());
|
|
|
|
|
fontBlack.setFontName("宋体");
|
|
|
|
|
fontRed.setBold(true);
|
|
|
|
|
fontBlack.setFontHeightInPoints((short) 12);
|
|
|
|
|
String txt = cell.getStringCellValue();
|
|
|
|
|
|
|
|
|
|
int len = txt.length();
|
|
|
|
|
if (len == 0) {
|
|
|
|
|
System.out.println(cell.getSheet().getSheetName());
|
|
|
|
|
System.out.println(cell.getRowIndex() + " " + cell.getColumnIndex());
|
|
|
|
|
System.out.println(cell.getStringCellValue());
|
|
|
|
|
System.out.println("发现问题:" + txt);
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
txt += "(*)";
|
|
|
|
|
XSSFRichTextString richText = new XSSFRichTextString(txt);//全部文字
|
|
|
|
|
|
|
|
|
|
richText.applyFont(0, len, fontBlack); // 从第0个字符到第2个字符应用font1
|
|
|
|
|
richText.applyFont(len, len + 3, fontRed); // 从第2个字符到第4个字符应用font2
|
|
|
|
|
cell.setCellValue(richText);//设置文字
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:只改变单元格的颜色,其它的样式继承原来的样式
|
|
|
|
|
*
|
|
|
|
|
* @param cell
|
|
|
|
|
* @param colorIdx
|
|
|
|
|
*/
|
|
|
|
|
public static void changeColor(Cell cell, short colorIdx) {
|
|
|
|
|
// 创建单元格样式
|
|
|
|
|
CellStyle style = cell.getCellStyle();
|
|
|
|
|
// 设置背景颜色
|
|
|
|
|
style.setFillForegroundColor(colorIdx);
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
// 应用样式到单元格
|
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:移除所有批注信息
|
|
|
|
|
*
|
|
|
|
|
* @param sheet
|
|
|
|
|
*/
|
|
|
|
|
public static void RemoveAllComment(XSSFSheet sheet) {
|
|
|
|
|
// 遍历所有行和单元格,移除批注
|
|
|
|
|
for (Row row : sheet) {
|
|
|
|
|
for (Cell cell : row) {
|
|
|
|
|
if (cell.getCellComment() != null) {
|
|
|
|
|
cell.removeCellComment();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:将指定Sheet表中所有数据区域进行样式还原
|
|
|
|
|
*
|
|
|
|
|
* @param sheet
|
|
|
|
|
*/
|
|
|
|
|
public static void resetStyle(XSSFSheet sheet, int dataStartRow) {
|
|
|
|
|
// 遍历所有行和单元格,恢复颜色
|
|
|
|
|
for (int row = dataStartRow; row <= sheet.getLastRowNum(); row++) {
|
|
|
|
|
for (int col = 0; col < sheet.getRow(row).getLastCellNum(); col++) {
|
|
|
|
|
Cell cell = sheet.getRow(row).getCell(col);
|
|
|
|
|
// 创建单元格样式
|
|
|
|
|
CellStyle style = cell.getCellStyle();
|
|
|
|
|
// 设置背景颜色
|
|
|
|
|
style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
// 应用样式到单元格
|
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:修改指定单元格的样式
|
|
|
|
|
*
|
|
|
|
|
* @param wb
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static void addComment(Workbook wb, Cell cell, String str) {
|
|
|
|
|
Sheet sheet = cell.getSheet();
|
|
|
|
|
// 判断单元格上是否存在批注
|
|
|
|
|
if (cell.getCellComment() != null) {
|
|
|
|
|
// 如果存在批注,先删除
|
|
|
|
|
cell.removeCellComment();
|
|
|
|
|
}
|
|
|
|
|
// 创建批注
|
|
|
|
|
Drawing<?> drawing = sheet.createDrawingPatriarch();
|
|
|
|
|
CreationHelper factory = wb.getCreationHelper();
|
|
|
|
|
ClientAnchor anchor = factory.createClientAnchor();
|
|
|
|
|
anchor.setCol1(cell.getColumnIndex());
|
|
|
|
|
anchor.setCol2(cell.getColumnIndex() + 2);
|
|
|
|
|
anchor.setRow1(cell.getRow().getRowNum());
|
|
|
|
|
anchor.setRow2(cell.getRow().getRowNum() + 3);
|
|
|
|
|
Comment comment = drawing.createCellComment(anchor);
|
|
|
|
|
comment.setString(factory.createRichTextString(str));
|
|
|
|
|
// 将批注添加到单元格
|
|
|
|
|
cell.setCellComment(comment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:在EXCEL的指定范围内添加上下拉框,内容通过String[]提供
|
|
|
|
|
*
|
|
|
|
|
* @param workbook
|
|
|
|
|
* @param sheetIdx
|
|
|
|
|
* @param firstRow
|
|
|
|
|
* @param lastRow
|
|
|
|
|
* @param firstCol
|
|
|
|
|
* @param lastCol
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static void addValidation(Workbook workbook, int sheetIdx, String options, int firstRow, int lastRow, int firstCol, int lastCol) {
|
|
|
|
|
String[] optionArray = options.split(",");
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(sheetIdx);
|
|
|
|
|
|
|
|
|
|
// 删除已有的校验
|
|
|
|
|
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
|
|
|
|
|
DataValidationConstraint dvConstraint = dvHelper.createCustomConstraint("DELETE_ALL");
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0); // 设置一个范围,例如整个表格范围
|
|
|
|
|
DataValidation validation = dvHelper.createValidation(dvConstraint, addressList);
|
|
|
|
|
validation.setSuppressDropDownArrow(true);
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
|
|
|
|
|
// 创建数据验证对象
|
|
|
|
|
DataValidationHelper validationHelper = sheet.getDataValidationHelper();
|
|
|
|
|
DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(optionArray);
|
|
|
|
|
addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); // 指定单元格范围
|
|
|
|
|
validation = validationHelper.createValidation(constraint, addressList);
|
|
|
|
|
// 应用数据验证到单元格
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取指定Cell的内容值
|
|
|
|
|
*
|
|
|
|
|
* @param cell
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
//获取单元格各类型值,返回字符串类型
|
|
|
|
|
public static String getValue(Cell cell) {
|
|
|
|
|
//判断是否为null或空串
|
|
|
|
|
if (cell == null || cell.toString().trim().equals("")) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
String cellValue = "";
|
|
|
|
|
switch (cell.getCellType()) {
|
|
|
|
|
case NUMERIC: // 数字
|
|
|
|
|
short format = cell.getCellStyle().getDataFormat();
|
|
|
|
|
if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
|
|
SimpleDateFormat sdf = null;
|
|
|
|
|
//System.out.println("cell.getCellStyle().getDataFormat()="+cell.getCellStyle().getDataFormat());
|
|
|
|
|
if (format == 20 || format == 32) {
|
|
|
|
|
sdf = new SimpleDateFormat("HH:mm");
|
|
|
|
|
} else if (format == 14 || format == 31 || format == 57 || format == 58) {
|
|
|
|
|
// 处理自定义日期格式:m月d日(通过判断单元格的格式id解决,id的值是58)
|
|
|
|
|
sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
double value = cell.getNumericCellValue();
|
|
|
|
|
Date date = org.apache.poi.ss.usermodel.DateUtil
|
|
|
|
|
.getJavaDate(value);
|
|
|
|
|
cellValue = sdf.format(date);
|
|
|
|
|
} else {// 日期
|
|
|
|
|
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
cellValue = sdf.format(cell.getDateCellValue());// 日期
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try {
|
|
|
|
|
throw new Exception("exception on get date data !".concat(e.toString()));
|
|
|
|
|
} catch (Exception e1) {
|
|
|
|
|
e1.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
sdf = null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
BigDecimal bd = new BigDecimal(cell.getNumericCellValue());
|
|
|
|
|
cellValue = bd.toPlainString();// 数值 这种用BigDecimal包装再获取plainString,可以防止获取到科学计数值
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case STRING: // 字符串
|
|
|
|
|
cellValue = cell.getStringCellValue();
|
|
|
|
|
break;
|
|
|
|
|
case BOOLEAN: // Boolean
|
|
|
|
|
cellValue = cell.getBooleanCellValue() + "";
|
|
|
|
|
break;
|
|
|
|
|
case FORMULA: // 公式
|
|
|
|
|
cellValue = cell.getCellFormula();
|
|
|
|
|
break;
|
|
|
|
|
case BLANK: // 空值
|
|
|
|
|
cellValue = "";
|
|
|
|
|
break;
|
|
|
|
|
case ERROR: // 故障
|
|
|
|
|
cellValue = "ERROR VALUE";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
cellValue = "UNKNOW VALUE";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return cellValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:判断一个单元格是不是被合并了
|
|
|
|
|
*
|
|
|
|
|
* @param sheet
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isMerged(XSSFSheet sheet, XSSFCell cell) {
|
|
|
|
|
// 判断单元格是否被合并
|
|
|
|
|
for (CellRangeAddress range : sheet.getMergedRegions()) {
|
|
|
|
|
if (cell.getRowIndex() >= range.getFirstRow() && cell.getRowIndex() <= range.getLastRow()
|
|
|
|
|
&& cell.getColumnIndex() >= range.getFirstColumn() && cell.getColumnIndex() <= range.getLastColumn()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* )
|
|
|
|
|
* 获取单元格vo
|
|
|
|
|
*
|
|
|
|
|
* @param cell 单元格
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String getColor(XSSFCell cell) {
|
|
|
|
|
if (cell != null) {
|
|
|
|
|
//背景颜色
|
|
|
|
|
CellStyle cellStyle = cell.getCellStyle();
|
|
|
|
|
XSSFColor xssfColor = (XSSFColor) cellStyle.getFillForegroundColorColor();
|
|
|
|
|
byte[] bytes;
|
|
|
|
|
if (xssfColor != null) {
|
|
|
|
|
bytes = xssfColor.getRGB();
|
|
|
|
|
return String.format("#%02X%02X%02X", bytes[0], bytes[1], bytes[2]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "BLANK";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:查找指定Sheet中的表头开始位置与结束位置,目前只支持一个Sheet一个导入模板,并且,只支持单行或双行表头
|
|
|
|
|
*
|
|
|
|
|
* @param sheet
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static List<Integer> getHead(XSSFSheet sheet) {
|
|
|
|
|
List<Integer> list = new ArrayList<>();
|
|
|
|
|
//整行都是同一种非空白颜色,视为表头
|
|
|
|
|
// 遍历行
|
|
|
|
|
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
|
//获得行
|
|
|
|
|
XSSFRow row = sheet.getRow(i);
|
|
|
|
|
//遍历列
|
|
|
|
|
if (row != null) {
|
|
|
|
|
Map<String, Integer> _map = new HashMap<>();
|
|
|
|
|
for (int j = 0; j < row.getLastCellNum(); j++) {
|
|
|
|
|
//获取单元格
|
|
|
|
|
XSSFCell cell = row.getCell(j);
|
|
|
|
|
if (cell == null) continue;
|
|
|
|
|
String color = getColor(cell);
|
|
|
|
|
//记录背景颜色数量
|
|
|
|
|
if (_map.containsKey(color))
|
|
|
|
|
_map.put(color, _map.get(color) + 1);
|
|
|
|
|
else
|
|
|
|
|
_map.put(color, 1);
|
|
|
|
|
}
|
|
|
|
|
if (_map.size() == 1 && _map.entrySet().iterator().next().getKey().startsWith("#")) {
|
|
|
|
|
list.add(i + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:设置整数类型格式
|
|
|
|
|
*
|
|
|
|
|
* @param wb
|
|
|
|
|
* @param sheetIdx
|
|
|
|
|
* @param colIdx
|
|
|
|
|
* @param firstRow
|
|
|
|
|
* @param lastRow
|
|
|
|
|
*/
|
|
|
|
|
public static void setIntegerStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
|
|
|
|
|
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint(
|
|
|
|
|
DataValidationConstraint.ValidationType.INTEGER, XSSFDataValidationConstraint.OperatorType.BETWEEN,
|
|
|
|
|
String.valueOf(Integer.MIN_VALUE), String.valueOf(Integer.MAX_VALUE)
|
|
|
|
|
);
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
|
|
|
|
|
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
|
|
|
|
|
validation.setSuppressDropDownArrow(false);
|
|
|
|
|
validation.setShowErrorBox(true);
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:设置浮点数类型格式
|
|
|
|
|
*
|
|
|
|
|
* @param wb
|
|
|
|
|
* @param sheetIdx
|
|
|
|
|
* @param colIdx
|
|
|
|
|
* @param firstRow
|
|
|
|
|
* @param lastRow
|
|
|
|
|
*/
|
|
|
|
|
public static void setFloatStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
|
|
|
|
|
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint(
|
|
|
|
|
DataValidationConstraint.ValidationType.DECIMAL, XSSFDataValidationConstraint.OperatorType.BETWEEN,
|
|
|
|
|
String.valueOf(Float.MIN_VALUE), String.valueOf(Float.MAX_VALUE)
|
|
|
|
|
);
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
|
|
|
|
|
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
|
|
|
|
|
validation.setSuppressDropDownArrow(false);
|
|
|
|
|
validation.setShowErrorBox(true);
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:设置日期格式
|
|
|
|
|
*
|
|
|
|
|
* @param wb
|
|
|
|
|
* @param sheetIdx
|
|
|
|
|
* @param colIdx
|
|
|
|
|
* @param firstRow
|
|
|
|
|
* @param lastRow
|
|
|
|
|
*/
|
|
|
|
|
public static void setDateStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
|
|
|
|
|
DataValidationConstraint dvConstraint = dvHelper
|
|
|
|
|
.createDateConstraint(DataValidationConstraint.OperatorType.BETWEEN, "date(1900,1,1)", "date(2099,12,31)", "yyyy/MM/dd");
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
|
|
|
|
|
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
|
|
|
|
|
validation.setSuppressDropDownArrow(false);
|
|
|
|
|
validation.setShowErrorBox(true);
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
// InputStream is = new FileInputStream("D:\\dsWork\\dsBase\\src\\main\\resource\\Excel\\sheet.xlsx");
|
|
|
|
|
// XSSFWorkbook wb = new XSSFWorkbook(is);
|
|
|
|
|
//
|
|
|
|
|
// setIntegerStyle(wb, 0, 2, 0, 20000);
|
|
|
|
|
//
|
|
|
|
|
// setFloatStyle(wb, 0, 3, 0, 20000);
|
|
|
|
|
//
|
|
|
|
|
// setDateStyle(wb, 0, 4, 0, 20000);
|
|
|
|
|
// //保存
|
|
|
|
|
// FileOutputStream fileOut = new FileOutputStream("D:\\dsWork\\dsBase\\src\\main\\resource\\Excel\\b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx");
|
|
|
|
|
// wb.write(fileOut);
|
|
|
|
|
// //关闭Excel
|
|
|
|
|
// wb.close();
|
|
|
|
|
|
|
|
|
|
String f2 = "D:\\dsWork\\dsBase\\src\\main\\resource\\Excel\\4.xlsx";
|
|
|
|
|
InputStream is = new FileInputStream(f2);
|
|
|
|
|
XSSFWorkbook wb = new XSSFWorkbook(is);
|
|
|
|
|
int sheet_index = 1;
|
|
|
|
|
int row_index = 3;
|
|
|
|
|
int col_index = 7;
|
|
|
|
|
XSSFCell cell = wb.getSheetAt(sheet_index).getRow(row_index).getCell(col_index);
|
|
|
|
|
Object value = getValue(cell);
|
|
|
|
|
System.out.println(value);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|