|
|
|
@ -0,0 +1,114 @@
|
|
|
|
|
package UnitTest.ImportExcel;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
public class TestTitle {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:修改指定单元格的样式
|
|
|
|
|
*
|
|
|
|
|
* @param wb
|
|
|
|
|
* @param sheetIndex
|
|
|
|
|
* @param r
|
|
|
|
|
* @param c
|
|
|
|
|
* @param fontSize
|
|
|
|
|
* @param colorIdx
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Workbook addComment(Workbook wb, int sheetIndex, int r, int c,
|
|
|
|
|
int fontSize, short colorIdx, String str) {
|
|
|
|
|
Sheet sheet = wb.getSheetAt(sheetIndex);
|
|
|
|
|
Row row = sheet.getRow(r);
|
|
|
|
|
Cell cell = row.getCell(c);
|
|
|
|
|
// 创建单元格样式
|
|
|
|
|
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) fontSize);//字号
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
// 设置背景颜色
|
|
|
|
|
style.setFillForegroundColor(colorIdx);
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
// 应用样式到单元格
|
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
|
|
|
|
|
|
// 判断单元格上是否存在批注
|
|
|
|
|
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() + 1);
|
|
|
|
|
anchor.setRow1(row.getRowNum());
|
|
|
|
|
anchor.setRow2(row.getRowNum() + 3);
|
|
|
|
|
Comment comment = drawing.createCellComment(anchor);
|
|
|
|
|
comment.setString(factory.createRichTextString(str));
|
|
|
|
|
// 将批注添加到单元格
|
|
|
|
|
cell.setCellComment(comment);
|
|
|
|
|
return wb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:在EXCEL的指定范围内添加上下拉框,内容通过String[]提供
|
|
|
|
|
*
|
|
|
|
|
* @param workbook
|
|
|
|
|
* @param sheetIdx
|
|
|
|
|
* @param firstRow
|
|
|
|
|
* @param lastRow
|
|
|
|
|
* @param firstCol
|
|
|
|
|
* @param lastCol
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static Workbook addValidation(Workbook workbook, int sheetIdx, String[] options, int firstRow, int lastRow, int firstCol, int lastCol) {
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(sheetIdx);
|
|
|
|
|
// 创建数据验证对象
|
|
|
|
|
DataValidationHelper validationHelper = sheet.getDataValidationHelper();
|
|
|
|
|
DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(options);
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol); // 指定单元格范围
|
|
|
|
|
DataValidation validation = validationHelper.createValidation(constraint, addressList);
|
|
|
|
|
// 应用数据验证到单元格
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
return workbook;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
String path = "D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\";
|
|
|
|
|
String filePath = path + "1.xlsx";
|
|
|
|
|
int r = 5, c = 6;//第6行第7列
|
|
|
|
|
int fontSize = 12;
|
|
|
|
|
short colorIdx = IndexedColors.YELLOW.getIndex();
|
|
|
|
|
// 创建下拉框的选项
|
|
|
|
|
String[] options = new String[]{"男", "女", "未知"};
|
|
|
|
|
|
|
|
|
|
try (FileInputStream fileIn = new FileInputStream(filePath);
|
|
|
|
|
Workbook workbook = new XSSFWorkbook(fileIn)) {
|
|
|
|
|
//在指定单元格上添加背景黄色+标注提示信息
|
|
|
|
|
addComment(workbook, 0, r, c, fontSize, colorIdx, "此单元格应该是非空的!");
|
|
|
|
|
//添加下拉框校验
|
|
|
|
|
addValidation(workbook, 0, options, 4, 21, c - 1, c - 1);
|
|
|
|
|
// 保存到文件
|
|
|
|
|
FileOutputStream fileOut = new FileOutputStream(filePath);
|
|
|
|
|
workbook.write(fileOut);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|