当前位置:网站首页>excle加水印
excle加水印
2022-04-23 07:34:00 【beinlife】
excle加水印
效果如下:水印的透明度可以设置,详细见代码

-
相关的jar包:jacob-1.17-x64.rar
- 注意区分32位和64系统
- 复制 jacob-1.17-M4-x64.dll 放在java jdk bin目录下、系统目录/WINDOWS/system32目录下
代码如下:
package com.solex.waterPrint;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.com.ComThread;
public class TestWaterExcel {
//测试功能
public static void main(String[] argv) {
TestWaterExcel d = TestWaterExcel.getInstance();
try {
if (d.initExcelApp()) {
Date date=Calendar.getInstance().getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(date);
String imgPath="C:/Users/solexit06/Desktop/Border.png";
String filePath="C:/Users/solexit06/Desktop/testExcel.xlsx";
d.openWorkbook(filePath,true,false);
d.getCurrentSheet();
d.setFooter(dateStr);//页脚
// d.setHeader(dateStr);//页眉
d.setWater(imgPath);//设置图片水印
// Dispatch curSheet=d.getSheetByIndex(2);//获取第二张表
// d.setValue(curSheet, "A1", "value", "test2");//往第2张表的A1中写入
d.closeDocument();
} else{
System.out.println("打开对象失败!");
}
} catch (Exception e) {
System.out.println(e);
}finally{
d.closeWordObj();
}
}
public TestWaterExcel() {}
private static TestWaterExcel instance;
private ActiveXComponent excelApp;//Word对象
private Dispatch workbooks = null;//存储所有的文档
private Dispatch workbook = null;//用于存储一个文档:比如新增一个文档时返回,新增的文档
private Dispatch sheets = null;// 获得sheets集合对象
private Dispatch currentSheet = null;// 当前sheet
public final static synchronized TestWaterExcel getInstance() {
if (instance == null){
instance = new TestWaterExcel();
}
return instance;
}
private void initComponents(){
workbook = null;
currentSheet = null;
sheets = null;
}
/** * 初始化Excel对象*/
public boolean initExcelApp() {
boolean retFlag = false;
initComponents();
ComThread.InitSTA();//初始化线程、使用结束后要关闭线程
try {
excelApp = new ActiveXComponent("Excel.Application");//初始化表
excelApp.setProperty("Visible", new Variant(true));//配置启动word时是显示执行还是隐式执行
workbooks = excelApp.getProperty("Workbooks").toDispatch();// 获取word所有文档对象
retFlag = true;
} catch (Exception e) {
retFlag = false;
e.printStackTrace();
}
return retFlag;
}
/**打开一个workbook*/
public void openWorkbook(String filename,boolean visible, boolean readonly) {
if (this.workbook != null) {
this.closeDocument();
}
workbook = Dispatch.invoke(workbooks,"Open",Dispatch.Method,
new Object[] { filename, new Variant(visible),new Variant(readonly) },// 是否以只读方式打开
new int[1]).toDispatch();
}
/** * 得到workbook的名字 * @return */
public String getWorkbookName() {
if(workbook==null)
return null;
return Dispatch.get(workbook, "name").toString();
}
/** * 得到sheets的集合对象 * @return */
public Dispatch getSheets() {
if(sheets==null)
sheets = Dispatch.get(workbook, "sheets").toDispatch();
return sheets;
}
/** * 添加新的工作表(sheet),(添加后为默认为当前激活的工作表) */
public Dispatch addSheet() {
return Dispatch.get(sheets,"add").toDispatch();
}
/** * 得到当前sheet * @return */
public Dispatch getCurrentSheet() {
currentSheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
return currentSheet;
}
/** * 得到当前sheet的名字 * @return */
public String getCurrentSheetName() {
return Dispatch.get(getCurrentSheet(), "name").toString();
}
/** * 通过sheetName得到sheet * @param name sheetName * @return */
public Dispatch getSheetByName(String name) {
return Dispatch.invoke(getSheets(),"Item",Dispatch.Get,
new Object[]{name}, new int[1]).toDispatch();
}
/** * 通过工作表索引得到工作表(第一个工作簿index为1) * @param index * @return sheet对象 */
public Dispatch getSheetByIndex(Integer index) {
return Dispatch.invoke(getSheets(), "Item", Dispatch.Get,
new Object[]{index}, new int[1]).toDispatch();
}
/** * 得到sheet的总数 * @return */
public int getSheetCount() {
int count = Integer.parseInt(Dispatch.get(getSheets(), "count").toString());
return count;
}
/** * 单元格写入值 * @param sheet 被操作的sheet * @param position 单元格位置,如:C1 * @param type 值的属性 如:value * @param value */
public void setValue(Dispatch sheet, String position, String type, Object value) {
Dispatch cell = Dispatch.invoke(sheet, "Range",Dispatch.Get,
new Object[] { position }, new int[1]).toDispatch();
Dispatch.put(cell, type, value);
}
/** * 单元格读取值 * @param position 单元格位置,如: C1 * @param sheet * @return */
public Variant getValue(String position, Dispatch sheet) {
Dispatch cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] { position }, new int[1]).toDispatch();
Variant value = Dispatch.get(cell, "Value");
return value;
}
/** * 设置页脚信息 */
public void setFooter(String foot) {
currentSheet=this.getCurrentSheet();
Dispatch PageSetup=Dispatch.get(currentSheet,"PageSetup").toDispatch();
//字体:&10 [注意空格],粗体:&B,颜色:&KFF0000,日期:&D,时间:&T
Dispatch.put(PageSetup,"CenterFooter",
"&13 &B&KF08080&D&");
}
/** * 获取页脚信息 */
public String getFooter() {
currentSheet=this.getCurrentSheet();
Dispatch PageSetup=Dispatch.get(currentSheet,"PageSetup").toDispatch();
return Dispatch.get(PageSetup,"CenterFooter").toString();
}
/** * 设置页眉信息 */
public void setHeader(String header) {
currentSheet=this.getCurrentSheet();
Dispatch PageSetup=Dispatch.get(currentSheet,"PageSetup").toDispatch();
Dispatch.put(PageSetup,"CenterHeader",header);
}
/** * 获取页眉信息 */
public String getHeader() {
currentSheet=this.getCurrentSheet();
Dispatch PageSetup=Dispatch.get(currentSheet,"PageSetup").toDispatch();
return Dispatch.get(PageSetup,"CenterHeader").toString();
}
/** * 通过页眉设置水印 */
public void setWater(String filePath) {
currentSheet=this.getCurrentSheet();
Dispatch PageSetup=Dispatch.get(currentSheet,"PageSetup").toDispatch();
Dispatch.put(PageSetup,"CenterHeader",
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n&G");//显示图片对象,\n\r是换行,&G表示显示图片
Dispatch header=Dispatch.get(PageSetup,"CenterHeaderPicture").toDispatch();//获取图片对象
Dispatch.put(header,"Filename",filePath);
Dispatch.put(header,"Height",105);
Dispatch.put(header,"Width",170);
Dispatch.put(header,"Brightness",0.75);//设置亮度
Dispatch.put(header,"Contrast",0.25);//设置对比
}
/** * 工作簿另存为 * @param fileNewPath 另存为的路径 */
public void SaveAs(String fileNewPath){
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method,
new Object[] { fileNewPath,new Variant(44) }, new int[1]);
}
/** * 保存并关闭当前文档 */
public void closeDocument() {
if (workbook != null) {
Dispatch.call(workbook , "Save");//保存
Dispatch.call(workbook , "Close", new Variant(0));//关闭
workbook = null;
}
}
/** * 释放资源*/
public void closeWordObj() {
if(excelApp!=null){
excelApp.invoke("Quit", new Variant[] {});// 关闭文件
excelApp=null;
}
workbooks = null;
ComThread.Release();// 释放线程
}
}
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://blog.csdn.net/beinlife/article/details/53514236
边栏推荐
- PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
- LeetCode简单题之统计字符串中的元音子字符串
- 万物互联下如何对设备进行加密
- 通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
- LeetCode简单题之计算字符串的数字和
- 396. Rotate Function
- Install MySQL for Ubuntu and query the average score
- Distributed service governance Nacos
- Data deletion and modification (MySQL)
- idea:使用easyYapi插件导出yapi接口
猜你喜欢

扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...

欧圣电气深交所上市:市值52亿 陆为东父女为美国籍

Why are there 1px problems? How?

总线结构概述

Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi

利用Js实现一个千分位

Search the complete navigation program source code

WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code

clang 如何产生汇编文件

监控智能回放是什么,如何使用智能回放查询录像
随机推荐
一键清理项目下pycharm和Jupyter缓存文件
【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
js常用数组方法
【学习】从零开始的音视频开发(9)——NuPlayer
ASAN 极简原理
LeetCoed18. Sum of four numbers
NFT ecological development of Ignis public chain: unicorn Donation and development of Art
Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
使用JWT生成与解析Token
396. Rotate Function
Usage of databinding
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
Generate and parse tokens using JWT
Situational leaders - Chapter 7, solving performance problems
dmp引擎工作总结(2021,光剑)
Comparison of indoor positioning methods of several intelligent robots
CSV column extract column extraction
Online yaml to XML tool
室内定位技术对比