温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用java怎么为表格添加水印

发布时间:2021-04-22 15:15:26 来源:亿速云 阅读:147 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关使用java怎么为表格添加水印,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

Java可以用来干什么

Java主要应用于:1. web开发;2. Android开发;3. 客户端开发;4. 网页开发;5. 企业级应用开发;6. Java大数据开发;7.游戏开发等。

1、添加过程

加载Excel测试文档;

设置文本和字体大小;

调用DrawText() 方法插入图片,将图片设置为页眉;

将图片设置为页眉,定义图片宽度和高度、文本显示样式及位置。

2、实例

import com.spire.xls.*;   import <span><a href="http://www.lanqibing.com/tag/java/" title="View all posts in java" target="_blank">java</a></span>.awt.*; import <span><a href="http://www.lanqibing.com/tag/java/" title="View all posts in java" target="_blank">java</a></span>.awt.image.BufferedImage;   import static <span><a href="http://www.lanqibing.com/tag/java/" title="View all posts in java" target="_blank">java</a></span>.awt.image.BufferedImage.TYPE_INT_ARGB;   public class SingleWatermark {     public static void main(String[] args) {         //加载Excel测试文档         Workbook wb = new Workbook();         wb.loadFromFile("test.xlsx");           //设置文本和字体大小         Font font = new Font("仿宋", Font.PLAIN, 40);           for (int i =0;i<wb.getWorksheets().getCount();i++)         {             Worksheet sheet = wb.getWorksheets().get(i);             //调用DrawText() 方法插入图片             BufferedImage imgWtrmrk = drawText("内部专用", font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());               //将图片设置为页眉             sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk);             sheet.getPageSetup().setCenterHeader("&G");                 //将显示模式设置为Layout             sheet.setViewMode(ViewMode.Layout);         }           //保存文档         wb.saveToFile("SingleWatermark.xlsx", ExcelVersion.Version2013);     }     private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width)     {         //定义图片宽度和高度         BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);           Graphics2D loGraphic = img.createGraphics();           //获取文本size         FontMetrics loFontMetrics = loGraphic.getFontMetrics(font);         int liStrWidth = loFontMetrics.stringWidth(text);         int liStrHeight = loFontMetrics.getHeight();           //文本显示样式及位置         loGraphic.setColor(backColor);         loGraphic.fillRect(0, 0, (int) width, (int) height);         loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);         loGraphic.rotate(Math.toRadians(-45));           loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);         loGraphic.setFont(font);         loGraphic.setColor(textColor);         loGraphic.drawString(text, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);         loGraphic.dispose();         return img;     } }

以上就是使用java怎么为表格添加水印,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI