200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > java导出excel加水印且加密(已实现)

java导出excel加水印且加密(已实现)

时间:2023-03-07 19:59:11

相关推荐

java导出excel加水印且加密(已实现)

水印是以图片形式加在excel上的,介意的不需要往下看了.

加密是真正的加密.

水印效果图:

密码效果图:

废话不多说,直接上代码:

package com.cpic.excelWater;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.*;public class ExcelWaterRemarkUtils {/** 为Excel打上水印工具函数 请自行确保参数值,以保证水印图片之间不会覆盖。 在计算水印的位置的时候,并没有考虑到单元格合并的情况,请注意** @param wb* Excel Workbook* @param sheet* 需要打水印的Excel* @param waterRemarkPath* 水印地址,classPath,目前只支持png格式的图片,* 因为非png格式的图片打到Excel上后可能会有图片变红的问题,且不容易做出透明效果。* 同时请注意传入的地址格式,应该为类似:"\\excelTemplate\\test.png"* @param startXCol* 水印起始列* @param startYRow* 水印起始行* @param betweenXCol* 水印横向之间间隔多少列* @param betweenYRow* 水印纵向之间间隔多少行* @param XCount* 横向共有水印多少个* @param YCount* 纵向共有水印多少个* @param waterRemarkWidth* 水印图片宽度为多少列* @param waterRemarkHeight* 水印图片高度为多少行* @throws IOException*/public static void putWaterRemarkToExcel(Workbook wb, Sheet sheet, String waterRemarkPath, int startXCol,int startYRow, int betweenXCol, int betweenYRow, int XCount, int YCount, int waterRemarkWidth,int waterRemarkHeight) throws IOException {// 加载图片ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();BufferedImage bufferImg = ImageUtils.createWaterMark("xxx");ImageIO.write(bufferImg, "png", byteArrayOut);// 开始打水印Drawing drawing = sheet.createDrawingPatriarch();Drawing<?> drawingPatriarch = sheet.getDrawingPatriarch();// 按照共需打印多少行水印进行循环for (int yCount = 0; yCount < YCount; yCount++) {// 按照每行需要打印多少个水印进行循环for (int xCount = 0; xCount < XCount; xCount++) {// 创建水印图片位置int xIndexInteger = startXCol + (xCount * waterRemarkWidth) + (xCount * betweenXCol);int yIndexInteger = startYRow + (yCount * waterRemarkHeight) + (yCount * betweenYRow);/** 参数定义: 第一个参数是(x轴的开始节点); 第二个参数是(是y轴的开始节点); 第三个参数是(是x轴的结束节点);* 第四个参数是(是y轴的结束节点); 第五个参数是(是从Excel的第几列开始插入图片,从0开始计数);* 第六个参数是(是从excel的第几行开始插入图片,从0开始计数); 第七个参数是(图片宽度,共多少列);* 第8个参数是(图片高度,共多少行);*/ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, xIndexInteger,yIndexInteger, xIndexInteger + waterRemarkWidth, yIndexInteger + waterRemarkHeight);Picture pic = drawing.createPicture(anchor,wb.addPicture(byteArrayOut.toByteArray(), Workbook.PICTURE_TYPE_PNG));pic.resize();}}}public static final String inFilePath = "D:\\xxx.xlsx";public static void main(String[] args) {try {//读取excel文件Workbook wb =null;if (inFilePath.endsWith("xls")) {wb = new HSSFWorkbook(new FileInputStream(inFilePath));}else if (inFilePath.endsWith("xlsx")){wb = new XSSFWorkbook(new FileInputStream(inFilePath));}//获取excel sheet个数int sheets = wb.getNumberOfSheets();//循环sheet给每个sheet添加水印for (int i = 0; i < sheets; i++) {Sheet sheet = wb.getSheetAt(i);//获取excel实际所占行int row = sheet.getFirstRowNum() + sheet.getLastRowNum();//获取excel实际所占列Row row1 = sheet.getRow(sheet.getFirstRowNum());if (row1==null){continue;}int cell = sheet.getRow(sheet.getFirstRowNum()).getLastCellNum() + 1;//根据行与列计算实际所需多少水印ExcelWaterRemarkUtils.putWaterRemarkToExcel(wb, sheet, null, 0, 0, 15, 15, cell / 15 + 1, row / 15 + 1, 0, 0);sheet.protectSheet("xxx");}ByteArrayOutputStream os = new ByteArrayOutputStream();try {wb.write(os);} catch (IOException e) {e.printStackTrace();}wb.close();byte[] content = os.toByteArray();// Excel文件生成后存储的位置。File file1 = new File(inFilePath);OutputStream fos = null;try {fos = new FileOutputStream(file1);fos.write(content);os.close();fos.close();} catch (Exception e) {e.printStackTrace();}} catch (Exception e) {System.out.println(e.getMessage());}}}

下面是个utils:

package com.cpic.excelWater;import javax.imageio.ImageIO;import java.awt.*;import java.awt.font.FontRenderContext;import java.awt.geom.Rectangle2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.InputStream;public class ImageUtils {public static BufferedImage createWaterMark(String content) {Integer width = 300;Integer height = 200;BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取bufferedImage对象String fontType = "宋体";Integer fontStyle = Font.PLAIN;Integer fontSize = 50;Font font = new Font(fontType, fontStyle, fontSize);Graphics2D g2d = image.createGraphics(); // 获取Graphics2d对象image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);g2d.dispose();g2d = image.createGraphics();g2d.setColor(new Color(0, 0, 0, 80)); //设置字体颜色和透明度g2d.setStroke(new BasicStroke(1)); // 设置字体g2d.setFont(font); // 设置字体类型 加粗 大小g2d.rotate(Math.toRadians(-10), (double) image.getWidth() / 2, (double) image.getHeight() / 2);//设置倾斜度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;}public static void createWaterMark(String content, String path) throws IOException {Integer width = 300;Integer height = 200;BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取bufferedImage对象String fontType = "宋体";Integer fontStyle = Font.PLAIN;Integer fontSize = 50;Font font = new Font(fontType, fontStyle, fontSize);Graphics2D g2d = image.createGraphics(); // 获取Graphics2d对象image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);g2d.dispose();g2d = image.createGraphics();g2d.setColor(new Color(0, 0, 0, 80)); //设置字体颜色和透明度g2d.setStroke(new BasicStroke(1)); // 设置字体g2d.setFont(font); // 设置字体类型 加粗 大小g2d.rotate(Math.toRadians(-10), (double) image.getWidth() / 2, (double) image.getHeight() / 2);//设置倾斜度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();ImageIO.write(image, "png", new File(path));}}

加油吧 少年 奥利给

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。