|
|
|
@ -7,9 +7,8 @@ import com.jfinal.kit.StrKit;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFColor;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.*;
|
|
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
@ -417,4 +416,131 @@ public class GenericTemplateUtil {
|
|
|
|
|
//关闭excel
|
|
|
|
|
wb.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:对于指定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 - 1, fontBlack); // 从第0个字符到第2个字符应用font1
|
|
|
|
|
richText.applyFont(len, len + 3, fontRed); // 从第2个字符到第4个字符应用font2
|
|
|
|
|
cell.setCellValue(richText);//设置文字
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:给指定单元格加边框
|
|
|
|
|
*
|
|
|
|
|
* @param wb
|
|
|
|
|
* @param cell
|
|
|
|
|
*/
|
|
|
|
|
public static void addBorder(Workbook wb, Cell cell, short colorIdx) {
|
|
|
|
|
// 创建单元格样式
|
|
|
|
|
CellStyle style = wb.createCellStyle();
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
// 创建字体样式
|
|
|
|
|
Font font = wb.createFont();
|
|
|
|
|
font.setFontName("宋体");//字体
|
|
|
|
|
font.setBold(true);//加粗
|
|
|
|
|
font.setFontHeightInPoints((short) 12);//字号
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
// 设置背景颜色
|
|
|
|
|
//short colorIdx = IndexedColors.YELLOW.getIndex();
|
|
|
|
|
style.setFillForegroundColor(colorIdx);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|