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();
        
}
原创粉丝点击