您好我在使用POI 3.6
我想将图像插入Excel中,但是当我创建图像时,我收到的错误就像这样
java.io.EOFException
at javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:330) at javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:330)
at java.io.DataInputStream.readUTF(DataInputStream.java:565) at java.io.DataInputStream.readUTF(DataInputStream.java:565)
at javax.imageio.stream.ImageInputStreamImpl.readUTF(ImageInputStreamImpl.java:309) at javax.imageio.stream.ImageInputStreamImpl.readUTF(ImageInputStreamImpl.java:309)
at com.sun.imageio.plugins.png.PNGImageReader.parse_iTXt_chunk(PNGImageReader.java:443) at com.sun.imageio.plugins.png.PNGImageReader.parse_iTXt_chunk(PNGImageReader.java:443)
at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:647) at com.sun.imageio.plugins.png.PNGImageReader.readMetadata(PNGImageReader.java:647)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1309) at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1309)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1579) at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1579)
at javax.imageio.ImageReader.read(ImageReader.java:923) at javax.imageio.ImageReader.read(ImageReader.java:923)
at org.apache.poi.ss.util.ImageUtils.getImageDimension(ImageUtils.java:66) at org.apache.poi.ss.util.ImageUtils.getImageDimension(ImageUtils.java:66)
at org.apache.poi.xssf.usermodel.XSSFPicture.getImageDimension(XSSFPicture.java:284) at org.apache.poi.xssf.usermodel.XSSFPicture.getImageDimension(XSSFPicture.java:284)
at org.apache.poi.xssf.usermodel.XSSFPicture.getPreferredSize(XSSFPicture.java:200) at org.apache.poi.xssf.usermodel.XSSFPicture.getPreferredSize(XSSFPicture.java:200)
at org.apache.poi.xssf.usermodel.XSSFPicture.resize(XSSFPicture.java:167) at org.apache.poi.xssf.usermodel.XSSFPicture.resize(XSSFPicture.java:167)
at org.apache.poi.xssf.usermodel.XSSFPicture.resize(XSSFPicture.java:154) at org.apache.poi.xssf.usermodel.XSSFPicture.resize(XSSFPicture.java:154)
at com.yagnaiq.core.ui.renderer.grid.ExportExcelHelper.insertChartToExcel(ExportExcelHelper.java:137)
我的代码是这样的。
InputStream inputStream = new FileInputStream("C:/dev/workspaces/4.2.3/YagnaWebJSP/WebRoot/images/Forum_Sprite.png");
byte[] picData = null;
File pic = new File( "C:\\dev\\workspaces\\4.2.3\\YagnaWebJSP\\WebRoot\\images\\Forum_Sprite.png" );
long length = pic.length();
picData = new byte[ ( int ) length ];
FileInputStream picIn = new FileInputStream( pic );
picIn.read( picData );
int pictureIdx = workbook.addPicture( picData, HSSFWorkbook.PICTURE_TYPE_JPEG );
/*if (imageByteArray == null)
imageByteArray = IOUtils.toByteArray(inputStream);
int pictureIdx = workbook.addPicture(imageByteArray, Workbook.PICTURE_TYPE_PNG);
*/
inputStream.close();
// Returns an object that handles instantiating concrete classes
CreationHelper helper = workbook.getCreationHelper();
// Creates the top-level drawing patriarch.
Drawing drawing = chartSheet.createDrawingPatriarch();
// Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
// set top-left corner for the image
anchor.setCol1(3);
anchor.setRow1(2);
// Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
// Reset the image to the original size
pict.resize();
请在此处提出建议。
我也得到了#34; pictureIdx"总是零(0),天气是正确还是没有?