Apache中的POI 3.9使用autosizeColumn在同一列上的图象present得到streched [英] In Apache POI 3.9 using autosizeColumn the image present on the same column getting streched

查看:836
本文介绍了Apache中的POI 3.9使用autosizeColumn在同一列上的图象present得到streched的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像,而在Excel工作表的图像下方一些文字。当我申请autoSizeColumn()的列中的文本present的形象也越来越streched。
我是anchortype也设置为2,但没有保护的图像缩放。
我在这里张贴一些样本code。任何帮助AP preciated

 公共静态无效的主要(字串[] args){
    尝试{
    XSSFWorkbook书=新XSSFWorkbook();
    XSSFSheet表= book.createSheet(测试表);
     InputStream为=新的FileInputStream(D:\\\\ \\\\ RPM_Eclipse_Workspaces \\\\ B6.9 \\\\ 00POI Chrysanthemum.jpg);
        字节[]字节= IOUtils.toByteArray(是);
        INT pictureIdx = book.addPicture(字节,Workbook.PICTURE_TYPE_PNG);
        图画= sheet.createDrawingPatriarch();
        ClientAnchor锚=新XSSFClientAnchor(0,0,1023,255,2,2,10,10);        //图像不应该得到调整大小,而这样做自动调整
        anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);
        图片PICT = drawing.createPicture(锚,pictureIdx);        XSSFRow行= sheet.createRow(12);
        的for(int i = 2; I< 11;我++){
            XSSFCell细胞= row.createCell(ⅰ);
            cell.setCellValue(椭圆形的(尽管锚的类型设置为MOVE_DONT_RESIZE)......但一​​个办法。);        }
       sheet.autoSizeColumn(2);
    book.write(新的FileOutputStream(新文件(D:\\\\ auto.xlsx)));
    的System.out.println(=== DONE ===);
    }赶上(例外五){    }
}


解决方案

POI用途的 TwoCellAnchors 添加图片...所以有一点讨厌的反射,你可以用的 OneCellAnchor

在该示例中 drawing.createAnchor(10,10,110,110,2,2,0,0)用于将图像10×10从顶部位置离开细胞(2,2)的拐角处,缩放图片100×100像素,也就是1×10 -10。

(与自由报办公室4.0,版Excel Viewer 2010测试)

 进口java.io. *;
导入的java.lang.reflect *。进口org.apache.poi.openxml4j.opc.PackageRelationship;
进口org.apache.poi.ss.usermodel.Workbook;
进口org.apache.poi.util.IOUtils;
导入org.apache.poi.xssf.usermodel *。
进口org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
进口org.openxmlformats.schemas.drawingml.x2006.s preadsheetDrawing *。公共类自动化{    公共静态无效的主要(字串[] args)抛出异常{
        XSSFWorkbook书=新XSSFWorkbook();
        XSSFSheet表= book.createSheet(测试表);
        InputStream为=新的FileInputStream(SRC /测试/资源/ smiley.jpg);
        字节[]字节= IOUtils.toByteArray(是);
        INT pictureIdx = book.addPicture(字节,Workbook.PICTURE_TYPE_PNG);
        XSSFDrawing绘图= sheet.createDrawingPatriarch();        XSSFClientAnchor锚= drawing.createAnchor(10,10,110,110,2 2,0,0);        createPicture(锚,pictureIdx,图纸);        XSSFRow行= sheet.createRow(12);
        的for(int i = 2; I< 11;我++){
            XSSFCell细胞= row.createCell(ⅰ);
            cell.setCellValue(椭圆形的(尽管锚的类型设置为MOVE_DONT_RESIZE)......但一​​个办法。);        }
        sheet.autoSizeColumn(2);
        book.write(新的FileOutputStream(新文件(auto.xlsx)));
    }    公共静态XSSFPicture createPicture(XSSFClientAnchor锚,诠释pictureIndex,XSSFDrawing图)
    抛出异常
    {
        方法M = XSSFDrawing.class.getDeclaredMethod(addPictureReference,int.class);
        m.setAccessible(真);
        PackageRelationship相对=(PackageRelationship)m.invoke(拉丝,(整数)pictureIndex);        长shapeId = 1000 + drawing.getCTDrawing()sizeOfOneCellAnchorArray()。
        CTOneCellAnchor ctAnchor = createOneCellAnchor(拉丝,锚);
        CTPicture ctShape = ctAnchor.addNewPic();        M = XSSFPicture.class.getDeclaredMethod(原型);
        m.setAccessible(真);
        CTPicture CTP =(CTPicture)m.invoke(NULL);
        ctShape.set(CTP);
        。ctShape.getNvPicPr()getCNvPr()SETID(shapeId)。        构造函数和LT; XSSFPicture> picCon = XSSFPicture.class
            .getDeclaredConstructor(XSSFDrawing.class,CTPicture.class);
        picCon.setAccessible(真);        XSSFPicture形状= picCon.newInstance(拉丝,ctShape);
        域F = XSSFShape.class.getDeclaredField(锚);
        f.setAccessible(真);
        f.set(形状,锚);        M = XSSFPicture.class.getDeclaredMethod(setPictureReference,PackageRelationship.class);
        m.setAccessible(真);
        m.invoke(形状,REL);
        返回形状;
    }    公共静态CTOneCellAnchor createOneCellAnchor(XSSFDrawing图纸,XSSFClientAnchor锚){
        最终诠释pixel2emu = 12700;
        CTOneCellAnchor ctAnchor = drawing.getCTDrawing()addNewOneCellAnchor()。        长CX =(anchor.getTo()getRowOff() - anchor.getFrom()getRowOff()。)* pixel2emu;
        长CY =(anchor.getTo()getColOff() - anchor.getFrom()getColOff()。)* pixel2emu;
        CTPositiveSize2D大小= CTPositiveSize2D.Factory.newInstance();
        size.setCx(CX);
        size.setCy(CY);
        ctAnchor.setExt(大小);        ctAnchor.setFrom(anchor.getFrom());
        CTMarker M = ctAnchor.getFrom();
        m.setColOff(m.getColOff()* pixel2emu);
        m.setRowOff(m.getRowOff()* pixel2emu);
        ctAnchor.addNewClientData();
        尝试{
            方法MT = XSSFClientAnchor.class.getDeclaredMethod(setFrom,CTMarker.class);
            mt.setAccessible(真);
            mt.invoke(锚,ctAnchor.getFrom());
        }赶上(例外五){
            抛出新的RuntimeException(处理我,E);
        }        返回ctAnchor;
    }
}

I have an image and some text below the image in an excel sheet. when i am applying autoSizeColumn() to the column where text present the image is also getting streched . i am also setting the anchortype to 2 but this is not protecting the image to resize. I am posting some sample code here. any help is appreciated

public static void main(String[] args) {
    try{
    XSSFWorkbook book = new XSSFWorkbook();
    XSSFSheet sheet = book.createSheet("Test Sheet");
     InputStream is = new          FileInputStream("D:\\RPM_Eclipse_Workspaces\\B6.9\\00POI\\Chrysanthemum.jpg");
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = book.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
        Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = new XSSFClientAnchor(0, 0, 1023, 255, 2,2,10,10);

        //Image should not get Resized while doing Autosize
        anchor.setAnchorType(ClientAnchor.DONT_MOVE_AND_RESIZE);
        Picture pict = drawing.createPicture(anchor, pictureIdx);

        XSSFRow row = sheet.createRow(12);
        for(int i = 2 ; i < 11 ; i++){
            XSSFCell cell = row.createCell(i);
            cell.setCellValue("oval (although anchor's type is set to MOVE_DONT_RESIZE ). ... But the one way to ");

        }
       sheet.autoSizeColumn(2);
    book.write(new FileOutputStream(new File("D:\\auto.xlsx")));
    System.out.println("=== DONE ===");
    }catch (Exception e){

    }
}

解决方案

POI uses TwoCellAnchors for adding pictures ... so with a bit of nasty reflections, you can add a Picture with a OneCellAnchor

In the example the drawing.createAnchor(10, 10, 110, 110, 2, 2, 0, 0) is used to position the image 10x10 from the top left corner of the cell(2,2) and scale the picture to 100x100 pixels, i.e. 110-10.

(tested with Libre Office 4.0, Excel Viewer 2010)

import java.io.*;
import java.lang.reflect.*;

import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.*;

public class Automation {

    public static void main(String[] args) throws Exception {
        XSSFWorkbook book = new XSSFWorkbook();
        XSSFSheet sheet = book.createSheet("Test Sheet");
        InputStream is = new FileInputStream("src/test/resources/smiley.jpg");
        byte[] bytes = IOUtils.toByteArray(is);
        int pictureIdx = book.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
        XSSFDrawing drawing = sheet.createDrawingPatriarch();

        XSSFClientAnchor anchor = drawing.createAnchor(10, 10, 110, 110, 2, 2, 0, 0);

        createPicture(anchor, pictureIdx, drawing);

        XSSFRow row = sheet.createRow(12);
        for (int i = 2; i < 11; i++) {
            XSSFCell cell = row.createCell(i);
            cell.setCellValue("oval (although anchor's type is set to MOVE_DONT_RESIZE ). ... But the one way to ");

        }
        sheet.autoSizeColumn(2);
        book.write(new FileOutputStream(new File("auto.xlsx")));
    }

    public static XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex, XSSFDrawing drawing)
    throws Exception
    {
        Method m = XSSFDrawing.class.getDeclaredMethod("addPictureReference", int.class);
        m.setAccessible(true);
        PackageRelationship rel = (PackageRelationship)m.invoke(drawing, (Integer)pictureIndex);

        long shapeId = 1000+drawing.getCTDrawing().sizeOfOneCellAnchorArray();
        CTOneCellAnchor ctAnchor = createOneCellAnchor(drawing, anchor);
        CTPicture ctShape = ctAnchor.addNewPic();

        m = XSSFPicture.class.getDeclaredMethod("prototype");
        m.setAccessible(true);
        CTPicture ctp = (CTPicture)m.invoke(null);
        ctShape.set(ctp);
        ctShape.getNvPicPr().getCNvPr().setId(shapeId);

        Constructor<XSSFPicture> picCon = XSSFPicture.class
            .getDeclaredConstructor(XSSFDrawing.class, CTPicture.class);
        picCon.setAccessible(true);

        XSSFPicture shape = picCon.newInstance(drawing, ctShape);
        Field f = XSSFShape.class.getDeclaredField("anchor");
        f.setAccessible(true);
        f.set(shape, anchor);

        m = XSSFPicture.class.getDeclaredMethod("setPictureReference", PackageRelationship.class);
        m.setAccessible(true);
        m.invoke(shape, rel);
        return shape;
    }

    public static CTOneCellAnchor createOneCellAnchor(XSSFDrawing drawing, XSSFClientAnchor anchor) {
        final int pixel2emu = 12700;
        CTOneCellAnchor ctAnchor = drawing.getCTDrawing().addNewOneCellAnchor();

        long cx = (anchor.getTo().getRowOff()-anchor.getFrom().getRowOff())*pixel2emu;
        long cy = (anchor.getTo().getColOff()-anchor.getFrom().getColOff())*pixel2emu;
        CTPositiveSize2D size = CTPositiveSize2D.Factory.newInstance();
        size.setCx(cx);
        size.setCy(cy);
        ctAnchor.setExt(size);

        ctAnchor.setFrom(anchor.getFrom());
        CTMarker m = ctAnchor.getFrom();
        m.setColOff(m.getColOff()*pixel2emu);
        m.setRowOff(m.getRowOff()*pixel2emu);
        ctAnchor.addNewClientData();
        try {
            Method mt = XSSFClientAnchor.class.getDeclaredMethod("setFrom", CTMarker.class);
            mt.setAccessible(true);
            mt.invoke(anchor, ctAnchor.getFrom());
        } catch (Exception e) {
            throw new RuntimeException("handle me", e);
        }

        return ctAnchor;
    }
}

这篇关于Apache中的POI 3.9使用autosizeColumn在同一列上的图象present得到streched的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆