有时需要给poi导出的excel添加水印及设置打印是默认的一些参数。在此记录下。
在此使用的poi的接口是 org.apache.poi.xssf.usermodel.XSSFWorkbook; HSSFWorkbook不行,试过了。
package com.southgroup.psimp.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* 添加水印
* @author Jfei
*
*/
public class WaterMarkUtil {
/**
* 坪山项目excel导出添加水印
* @param workbook
* @throws IOException
*/
public static void insertWaterMarkTextToXlsx(XSSFWorkbook workbook) throws IOException{
String s = "XXXX项目管理系统";
String userName = UserUtil.getUser().getUserName();
String date = DateUtils.dateToString(new Date(), "yyyy-MM-dd");
String waterMarkText = s + "\n"+userName+" "+date;
insertWaterMarkTextToXlsx(workbook,waterMarkText);
}
/**
* 给excel添加水印
* @throws IOException
*/
public static void insertWaterMarkTextToXlsx(XSSFWorkbook workbook,String waterMarkText) throws IOException{
BufferedImage image = createWatermarkImage(waterMarkText);
ByteArrayOutputStream imageOs = new ByteArrayOutputStream();
ImageIO.write(image, "png", imageOs);
int pictureIdx = workbook.addPicture(imageOs.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG);
POIXMLDocumentPart poixmlDocumentPart = workbook.getAllPictures().get(pictureIdx);
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {//获取每个Sheet表
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());
}
}
public static BufferedImage createWatermarkImage(String waterMark){
String[] textArray = waterMark.split("\n");
Font font = new Font("microsoft-yahei", Font.PLAIN, 32);
Integer width = 500;
Integer height = 400;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 背景透明 开始
Graphics2D g = image.createGraphics();
image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g.dispose();
// 背景透明 结束
g = image.createGraphics();
g.setColor(new Color(Color.lightGray.getRGB()));// 设定画笔颜色
g.setFont(font);// 设置画笔字体
// g.shear(0.1, -0.26);// 设定倾斜度
// 设置字体平滑
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//文字从中心开始输入,算出文字宽度,左移动一半的宽度,即居中
FontMetrics fontMetrics = g.getFontMetrics(font);
// 水印位置
int x = width / 2;
int y = height / 2;
// 设置水印旋转
g.rotate(Math.toRadians(-40), x,y);
for (int i = 0; i < textArray.length; i++) {
// 文字宽度
int textWidth = fontMetrics.stringWidth(textArray[i]);
g.drawString(textArray[i], x-(textWidth/2), y);// 画出字符串
y = y + font.getSize();
}
g.dispose();// 释放画笔
return image;
}
}
设置打印时默认参数
/**
* 设置打印的参数
* @param wb
*/
private static void setPrintParams(XSSFWorkbook wb){
XSSFSheet sheet = wb.getSheetAt(0);
XSSFPrintSetup printSetup = sheet.getPrintSetup();
// 打印方向,true:横向,false:纵向(默认
printSetup.setLandscape(true);
//设置A4纸
printSetup.setPaperSize(XSSFPrintSetup.A4_PAPERSIZE);
// 将整个工作表打印在一页(缩放),如果行数很多的话,可能会出问题
// sheet.setAutobreaks(true);
//将所有的列调整为一页,行数多的话,自动分页
printSetup.setScale((short)70);//缩放的百分比,自行调整
sheet.setAutobreaks(false);
}
导出来的水印效果,水印不会遮住内容,点击水印上的单元格,仍然可以编辑