main
黄海 2 years ago
parent 7b1ca8e6f9
commit 014dad882d

@ -2,6 +2,7 @@ package UnitTest.ImportExcel;
import com.dsideal.QingLong.Util.PoiUtil; import com.dsideal.QingLong.Util.PoiUtil;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -11,21 +12,20 @@ import java.io.IOException;
public class AddComment { public class AddComment {
//工作目录 //工作目录
public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\"; public static String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
//学校上传的Excel //学校上传的Excel
String source = path + "1.xlsx"; String source = path + "1.xlsx";
//要进行标注的行和列 //要进行标注的行和列
int r = 5, c = 6;//第6行第7列 int r = 5, c = 6;//第6行第7列
//字号
int fontSize = 12;
//标识的颜色,比如黄色、白色
short colorIdx = IndexedColors.YELLOW.getIndex();
//对文件进行校验,标注 //对文件进行校验,标注
try (FileInputStream fileIn = new FileInputStream(source); try (FileInputStream fileIn = new FileInputStream(source);
Workbook workbook = new XSSFWorkbook(fileIn)) { Workbook workbook = new XSSFWorkbook(fileIn)) {
//在指定单元格上添加背景黄色+标注提示信息 //在指定单元格上添加背景黄色+标注提示信息
PoiUtil.addComment(workbook, 0, r, c, fontSize, colorIdx, "此单元格应该是非空的!"); Cell cell = workbook.getSheetAt(0).getRow(r).getCell(c);
PoiUtil.addComment(workbook, cell, "此单元格应该是非空的!");
// 保存 // 保存
FileOutputStream fileOut = new FileOutputStream(source); FileOutputStream fileOut = new FileOutputStream(source);
workbook.write(fileOut); workbook.write(fileOut);

@ -0,0 +1,32 @@
package UnitTest.ImportExcel;
import com.dsideal.QingLong.Util.PoiUtil;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestDiffrentColor {
public static void main(String[] args) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("RichTextStringExample");
XSSFRow row = sheet.createRow(0);//行号
XSSFCell cell = row.createCell(0);//单元格
cell.setCellValue("我是原来的内容");
//添加重点提示符*和Comment
PoiUtil.addStar(wb, cell);
//添加Comment
PoiUtil.addComment(wb, cell, "此单元格内容必须输入!");
//输出
FileOutputStream fileOut = new FileOutputStream("c:\\workbook.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();
}
}

@ -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));
// 将批注添加到单元格 // 将批注添加到单元格

Loading…
Cancel
Save