|
|
@ -2,24 +2,47 @@ package com.dsideal.QingLong.Util;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFFont;
|
|
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
|
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
|
|
|
|
public class PoiUtil {
|
|
|
|
public class PoiUtil {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 功能:修改指定单元格的样式
|
|
|
|
* 功能:对于指定Cell中文字进行加红色(*)提醒
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param wb
|
|
|
|
* @param wb
|
|
|
|
* @param sheetIndex
|
|
|
|
* @param cell
|
|
|
|
* @param r
|
|
|
|
|
|
|
|
* @param c
|
|
|
|
|
|
|
|
* @param fontSize
|
|
|
|
|
|
|
|
* @param colorIdx
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void addComment(Workbook wb, int sheetIndex, int r, int c,
|
|
|
|
public static void addStar(XSSFWorkbook wb, XSSFCell cell) {
|
|
|
|
int fontSize, short colorIdx, String str) {
|
|
|
|
XSSFFont fontRed = wb.createFont();//第一种字体
|
|
|
|
Sheet sheet = wb.getSheetAt(sheetIndex);
|
|
|
|
fontRed.setBold(true);
|
|
|
|
Row row = sheet.getRow(r);
|
|
|
|
fontRed.setColor(IndexedColors.RED.getIndex());
|
|
|
|
Cell cell = row.getCell(c);
|
|
|
|
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();
|
|
|
|
|
|
|
|
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();
|
|
|
|
CellStyle style = wb.createCellStyle();
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
@ -32,14 +55,25 @@ public class PoiUtil {
|
|
|
|
Font font = wb.createFont();
|
|
|
|
Font font = wb.createFont();
|
|
|
|
font.setFontName("宋体");//字体
|
|
|
|
font.setFontName("宋体");//字体
|
|
|
|
font.setBold(true);//加粗
|
|
|
|
font.setBold(true);//加粗
|
|
|
|
font.setFontHeightInPoints((short) fontSize);//字号
|
|
|
|
font.setFontHeightInPoints((short) 12);//字号
|
|
|
|
style.setFont(font);
|
|
|
|
style.setFont(font);
|
|
|
|
// 设置背景颜色
|
|
|
|
// 设置背景颜色
|
|
|
|
|
|
|
|
//short colorIdx = IndexedColors.YELLOW.getIndex();
|
|
|
|
style.setFillForegroundColor(colorIdx);
|
|
|
|
style.setFillForegroundColor(colorIdx);
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
// 应用样式到单元格
|
|
|
|
// 应用样式到单元格
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 功能:修改指定单元格的样式
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param wb
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void addComment(Workbook wb, Cell cell,
|
|
|
|
|
|
|
|
String str) {
|
|
|
|
|
|
|
|
Sheet sheet = cell.getSheet();
|
|
|
|
// 判断单元格上是否存在批注
|
|
|
|
// 判断单元格上是否存在批注
|
|
|
|
if (cell.getCellComment() != null) {
|
|
|
|
if (cell.getCellComment() != null) {
|
|
|
|
// 如果存在批注,先删除
|
|
|
|
// 如果存在批注,先删除
|
|
|
@ -50,9 +84,9 @@ public class PoiUtil {
|
|
|
|
CreationHelper factory = wb.getCreationHelper();
|
|
|
|
CreationHelper factory = wb.getCreationHelper();
|
|
|
|
ClientAnchor anchor = factory.createClientAnchor();
|
|
|
|
ClientAnchor anchor = factory.createClientAnchor();
|
|
|
|
anchor.setCol1(cell.getColumnIndex());
|
|
|
|
anchor.setCol1(cell.getColumnIndex());
|
|
|
|
anchor.setCol2(cell.getColumnIndex() + 1);
|
|
|
|
anchor.setCol2(cell.getColumnIndex() + 2);
|
|
|
|
anchor.setRow1(row.getRowNum());
|
|
|
|
anchor.setRow1(cell.getRow().getRowNum());
|
|
|
|
anchor.setRow2(row.getRowNum() + 3);
|
|
|
|
anchor.setRow2(cell.getRow().getRowNum() + 3);
|
|
|
|
Comment comment = drawing.createCellComment(anchor);
|
|
|
|
Comment comment = drawing.createCellComment(anchor);
|
|
|
|
comment.setString(factory.createRichTextString(str));
|
|
|
|
comment.setString(factory.createRichTextString(str));
|
|
|
|
// 将批注添加到单元格
|
|
|
|
// 将批注添加到单元格
|
|
|
|