/**
*原作者鏈接不記得了。。。。。。。
*導出的excel后綴是.xslx
*/
/**maven依賴
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
*/
package com.poi.excel.water;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
/**
* @Author qixing.chen
* @Date 2020/10/16 8:08 下午
*/
@RestController
@RequestMapping("/aa")
public class Watermark {
@GetMapping("/cc")
public String ex() {
return "ok";
}
@GetMapping("/bb")
public void export() {
try {
XSSFWorkbook workbook = new XSSFWorkbook();
FileOutputStream out = new FileOutputStream("/Users/chen/Downloads/測試xlsx.xlsx");
XSSFSheet sheet = workbook.createSheet("Sheet1");
workbook.getSheet("Sheet1");
//add picture data to this workbook.
// FileInputStream is = new FileInputStream("/Users/Tony/Downloads/data_image.png");
// byte[] bytes = IOUtils.toByteArray(is);
BufferedImage image = FontImage.createWatermarkImage("測試水印","yyyy-MM-dd","#C5CBCF");
// 導出到字節流B
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "png", os);
int pictureIdx = workbook.addPicture(os.toByteArray(), Workbook.PICTURE_TYPE_PNG);
// is.close();
//add relation from sheet to the picture data
String rID = sheet.addRelation(null, XSSFRelation.IMAGES, workbook.getAllPictures().get(pictureIdx)).getRelationship().getId();
// String rid = sheet.addRelation(null,XSSFRelation.IMAGES,workbook.getAllPictures().get(pictureIdx))
//set background picture to sheet
sheet.getCTWorksheet().addNewPicture().setId(rID);
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.poi.excel.water;
import java.awt.*;
import java.awt.image.BufferedImage;
/**
* @Author qixing.chen
* @Date 2020/10/16 7:48 下午
*/
public class FontImage {
public static BufferedImage createWatermarkImage(String text,String dateFormat,String color) {
String[] textArray = text.split("\n");
Font font = new Font("microsoft-yahei", Font.PLAIN, 20);
Integer width = 600;
Integer height = 200;
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(Integer.parseInt(color.substring(1), 16)));// 設定畫筆顏色
g.setFont(font);// 設置畫筆字體
g.shear(0.1, -0.26);// 設定傾斜度
// 設置字體平滑
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int y = 50;
for (int i = 0; i < textArray.length; i++) {
g.drawString(textArray[i], 0, y);// 畫出字符串
y = y + font.getSize();
}
//g.drawString(DateUtils.getNowDateFormatCustom(watermark.getDateFormat()), 0, y);// 畫出字符串
g.drawString("2020-10-16", 0, y);// 畫出字符串
g.dispose();// 釋放畫筆
return image;
}
}
效果如下:
