jxl 实现excel动态水印
来源:互联网 发布:ipv6跟ipv4无网络权限 编辑:程序博客网 时间:2024/06/05 09:30
static int width=300;
static int height=50;
public static Map createWaterMark1(Map watermarkMessage) throws IOException {
Map map = new HashMap();
File file = new File("watermark.bmp");
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int minx = bi.getMinX();
int miny = bi.getMinY();
for (int i = minx; i< width; i++){
for (int j = miny; j < height; j++){
bi.setRGB(i, j, 0xffffff);
}
}
Graphics2D g2d = bi.createGraphics();
// 设置字体颜色为灰色
g2d.setColor(Color.LIGHT_GRAY);
// 设置图片的属性
g2d.setStroke(new BasicStroke(1));
// 设置字体
g2d.setFont(new Font("Serif", Font.ITALIC, 40));
// 设置字体倾斜度
g2d.rotate(Math.toRadians(-8));
// 写入水印文字原定高度过小,所以累计写水印,增加高度
g2d.drawString("导出时间 : " + watermarkMessage.get("time"), 0, 180 + 40 * (1+ 2));
g2d.drawString("导出工号 : " + watermarkMessage.get("loginName"),0,180 + 40 * (1+3));
g2d.drawString("工号部门 : " + watermarkMessage.get("domain"), 0,180 + 40 * (1+ 4));
// 设置透明度
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 释放对象
g2d.dispose();
// 通过bmp写入文件
// BMPEncoder.write(bi, file);
ImageIO.write(bi, "bmp", file);
map.put("file", file);
map.put("width", width);
map.put("height", height);
return map;
}
public static Map toMap(String jsonString) throws JSONException {
JSONObject jsonObject =JSONObject.fromObject(jsonString);
Map result = new HashMap();
Iterator iterator = jsonObject.keys();
String key = null;
String value = null;
while (iterator.hasNext()) {
key = (String) iterator.next();
value = jsonObject.getString(key);
result.put(key, value);
}
return result;
}
public static void addWatermark(String xls_filePath_in,String xls_filePath_out,JSONObject watermarkText) throws Exception{
InputStream is = new FileInputStream(new File(xls_filePath_in));
Workbook wb = Workbook.getWorkbook(is);
// 获得原始文档
WritableWorkbook wwb = Workbook.createWorkbook(new File(xls_filePath_out),wb);
WritableSheet ws1 = wwb.getSheet(0);
// 得到工作薄中的第一个工作表
//String watermarkMessage = jsonObject.toString();
Map watermarkMessage =toMap(watermarkText.toString());
Map map = createWaterMark1(watermarkMessage); File fileImg = (File) map.get("file");
// File fileImg = new File("kkkk.bmp");
byte imageData[] = new byte[(int) fileImg.length()];
FileInputStream fis = new FileInputStream(fileImg);
fis.read(imageData);
// must be 24 bit true-colour,bmp file // * @paramimageByte // * @paramwidthPixel // * @paramheightPixel
ws1.setWaterMarkImage(imageData, width, height);
wwb.write();
wwb.close();
fis.close();
}
static int height=50;
public static Map createWaterMark1(Map watermarkMessage) throws IOException {
Map map = new HashMap();
File file = new File("watermark.bmp");
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int minx = bi.getMinX();
int miny = bi.getMinY();
for (int i = minx; i< width; i++){
for (int j = miny; j < height; j++){
bi.setRGB(i, j, 0xffffff);
}
}
Graphics2D g2d = bi.createGraphics();
// 设置字体颜色为灰色
g2d.setColor(Color.LIGHT_GRAY);
// 设置图片的属性
g2d.setStroke(new BasicStroke(1));
// 设置字体
g2d.setFont(new Font("Serif", Font.ITALIC, 40));
// 设置字体倾斜度
g2d.rotate(Math.toRadians(-8));
// 写入水印文字原定高度过小,所以累计写水印,增加高度
g2d.drawString("导出时间 : " + watermarkMessage.get("time"), 0, 180 + 40 * (1+ 2));
g2d.drawString("导出工号 : " + watermarkMessage.get("loginName"),0,180 + 40 * (1+3));
g2d.drawString("工号部门 : " + watermarkMessage.get("domain"), 0,180 + 40 * (1+ 4));
// 设置透明度
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
// 释放对象
g2d.dispose();
// 通过bmp写入文件
// BMPEncoder.write(bi, file);
ImageIO.write(bi, "bmp", file);
map.put("file", file);
map.put("width", width);
map.put("height", height);
return map;
}
public static Map toMap(String jsonString) throws JSONException {
JSONObject jsonObject =JSONObject.fromObject(jsonString);
Map result = new HashMap();
Iterator iterator = jsonObject.keys();
String key = null;
String value = null;
while (iterator.hasNext()) {
key = (String) iterator.next();
value = jsonObject.getString(key);
result.put(key, value);
}
return result;
}
public static void addWatermark(String xls_filePath_in,String xls_filePath_out,JSONObject watermarkText) throws Exception{
InputStream is = new FileInputStream(new File(xls_filePath_in));
Workbook wb = Workbook.getWorkbook(is);
// 获得原始文档
WritableWorkbook wwb = Workbook.createWorkbook(new File(xls_filePath_out),wb);
WritableSheet ws1 = wwb.getSheet(0);
// 得到工作薄中的第一个工作表
//String watermarkMessage = jsonObject.toString();
Map watermarkMessage =toMap(watermarkText.toString());
Map map = createWaterMark1(watermarkMessage); File fileImg = (File) map.get("file");
// File fileImg = new File("kkkk.bmp");
byte imageData[] = new byte[(int) fileImg.length()];
FileInputStream fis = new FileInputStream(fileImg);
fis.read(imageData);
// must be 24 bit true-colour,bmp file // * @paramimageByte // * @paramwidthPixel // * @paramheightPixel
ws1.setWaterMarkImage(imageData, width, height);
wwb.write();
wwb.close();
fis.close();
}
阅读全文
0 0
- jxl 实现excel动态水印
- jxl创建excel加水印
- jxl 实现excel导入
- jxl实现导出excel
- Jxl导出Excel实现
- jxl 实现导出excel
- jxl实现excel 导出
- 利用jxl实现excel操作
- 利用jxl实现excel导入
- 利用jxl实现excel导出
- Java使用jxl的api实现excel动态数据验证及导入导出
- Js实现动态添加水印
- jsp + servlet +jxl 实现 export excel
- Strusts2实现jxl导出excel文件
- jxl包实现 java 生成excel
- Java实现导入Excel,jxl方式
- Jxl实现Excel的导入导出
- java jxl实现excel导出功能
- One-hot encoding 数据处理
- BootstrapTable获取选中行的行号
- AF 配置https证书,防止被抓包
- 网络层-7、网络地址转换-NAT
- 关于pip3 install error(invalid command 'egg_info')
- jxl 实现excel动态水印
- include指令和include动作区别
- ios友盟分享
- oracle、DB2、Informix常用约束语句
- Docker安装mysql 配置远程登录
- IDEA 快捷键修改(长期更新)
- 实现Form表单提交页面不进行跳转
- WebSocket In ASP.NET Core
- 17090701_CentOS7(64)下MySQL卸载、安装与配置