先看效果:
然后直接上代码
/**
* @param path 文件路径
* @param pwd 密码
* @param waterName 水印名称
* @param type 文件类型
*/
public static void excelWaterMarkPOI(String path,String pwd,String waterName,int type){
try {
BufferedImage bufferImg = ImageUtils.createWaterMark(waterName);
// 导出到字节流B
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferImg, "png", os);
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(path));
int pictureIdx = workbook.addPicture(os.toByteArray(), Workbook.PICTURE_TYPE_PNG);
POIXMLDocumentPart poixmlDocumentPart = workbook.getAllPictures().get(pictureIdx);
//获取每个Sheet表
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
XSSFSheet sheet = workbook.getSheetAt(i);
PackagePartName ppn = poixmlDocumentPart.getPackagePart().getPartName();
String relType = XSSFRelation.IMAGES.getRelation();
PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);
sheet.getCTWorksheet().addNewPicture().setId(pr.getId());
sheet.protectSheet(pwd);
}
ByteArrayOutputStream qweqweq = new ByteArrayOutputStream();
try {
workbook.write(qweqweq);
} catch (IOException e) {
log.error(e.getMessage(),e);
}
byte[] content = qweqweq.toByteArray();
// Excel文件生成后存储的位置。
File file1 = new File(path);
try (OutputStream fos = new FileOutputStream(file1)){
fos.write(content);
qweqweq.close();
} catch (Exception e) {
log.error(e.getMessage(),e);
}finally {
if (qweqweq != null) {
try {
qweqweq.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
} catch (IOException e) {
log.error(e.getMessage(),e);
}
}
生成水印图片代码:
/**
* 返回流
* @param content
* @return
*/
public static BufferedImage createWaterMark(String content) {
Integer width = 400;
Integer height = 300;
// 获取bufferedImage对象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
String fontType = "宋体";
Integer fontStyle = Font.PLAIN;
Integer fontSize = 30;
Font font = new Font(fontType, fontStyle, fontSize);
// 获取Graphics2d对象
Graphics2D g2d = image.createGraphics();
image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.dispose();
g2d = image.createGraphics();
//设置字体颜色和透明度
g2d.setColor(Color.lightGray);
// 设置字体
g2d.setStroke(new BasicStroke(1));
// 设置字体类型 加粗 大小
g2d.setFont(font);
g2d.shear(0.1,-0.4);
//设置倾斜度
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(content, context);
double x = (width - bounds.getWidth()) / 2;
double y = (height - bounds.getHeight()) / 2;
double ascent = -bounds.getY();
double baseY = y + ascent;
// 写入水印文字原定高度过小,所以累计写水印,增加高度
g2d.drawString(content, (int) x, (int) baseY);
// 设置透明度
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 释放对象
g2d.dispose();
return image;
}
读取模板往里面填充数据:
/**
* 读取模板,然后往模板中填充数据,文件类型为xlsx
* @param exportObjects
* @param templateFilename
* @return
*/
public static String exportExcelXLSX(List<Map<String, Object>> exportObjects,String templateFilename){
try {
String filename = templateFilename;
if (filename.indexOf(".xlsx") == -1) {
filename += ".xlsx";
}
String path = "";
String tempDir = "";
File file = new File(tempDir);
if (!file.exists()) {
// 创建临时目录
file.mkdir();
}
String templateFile = (templateDir + filename);
String outputFile ="";
//读取模板 然后赛数据进去
FileInputStream in =new FileInputStream(templateFile);
//读取excel模板
XSSFWorkbook wb = new XSSFWorkbook(in);
//读取了模板内所有sheet内容
XSSFSheet sheet = wb.getSheetAt(0);
//最大行数 赛数据这里每个业务可能需要自己实现,其余代码可以复用
int lastRowNum = exportObjects.size();
if (templateFilename.equals("xx")) {
//从第三行开始赛数据
for (int i = 0; i < lastRowNum; i++) {
XSSFRow row = sheet.getRow(i + 3);
Map<String, Object> info = exportObjects.get(i);
if (row == null) {
row = sheet.createRow(i + 3);
}
//序号
row.createCell(0).setCellValue(i + 1);
//
row.createCell(1).setCellValue((String) info.get("xx"));
//
row.createCell(2).setCellValue((String) info.get("xxx"));
//
row.createCell(3).setCellValue((String) info.get("xxxx"));
//
row.createCell(4).setCellValue((String) info.get("xxxxx"));
//
row.createCell(5).setCellValue((String) info.get("xxxxxx"));
//
row.createCell(6).setCellValue((String) info.get("xxxxxxx"));
}
} else {
//todo
}
FileOutputStream out = new FileOutputStream(outputFile);
wb.write(out);
return outputFile;
} catch (IOException e) {
logger.error(e.getMessage(),e);
}
return null;
}
加油,奥利给