java类 图片导入到excel 模糊 就是图片被盖上了一层红色
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java类 图片导入到excel 模糊 就是图片被盖上了一层红色相关的知识,希望对你有一定的参考价值。
FileOutputStream fileOut = null;
BufferedImage bufferImg =null;
BufferedImage bufferImg1 = null;
try
File file = new File("C:/CDMA_AIMS_HOME/htdocs/upload/day_call_monitor.xls");
if(file.exists())
file.delete();
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
bufferImg = ImageIO.read(new File ("C:/CDMA_AIMS_HOME/htdocs/upload/call_monitor.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
bufferImg.flush();
byteArrayOut.flush();
//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG);
//设置单元格的背景色
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 0,10,(short)3,27);
//插入图片
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
public static BufferedImage getImages(byte[] data) throws IOException
ByteArrayInputStream input = new ByteArrayInputStream(data);
return ImageIO.read(input);
经过查阅得知ImageIO.read()方法读取图片时可能存在不正确处理图片ICC信息的问题,ICC为JPEG图片格式中的一种头部信息,导致渲染图片前景色时蒙上一层红色。解决方案:
不再使用ImageIO.read()方法加载图片,而使用JDK中提供的Image src=Toolkit.getDefaultToolkit().getImage
Image src=Toolkit.getDefaultToolkit().getImage(file.getPath());
BufferedImage image=BufferedImageBuilder.toBufferedImage(src);//Image to BufferedImage
或者Toolkit.getDefaultToolkit().createImage
Image imageTookit = Toolkit.getDefaultToolkit().createImage(bytes);
BufferedImage cutImage = BufferedImageBuilder.toBufferedImage(imageTookit);
BufferedImageBuilder源码:
public static BufferedImage toBufferedImage(Image image)
if (image instanceof BufferedImage)
return (BufferedImage) image;
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null),
image.getHeight(null), transparency);
catch (HeadlessException e)
// The system does not have a screen
if (bimage == null)
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null),
image.getHeight(null), type);
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
参考技术A 你不是设置了背景色吗?追问
没看到设背景色啊! 关于图片的代码全在这里了!
追答你又nc 了....
style.setFillForegroundColor(HSSFColor.GREEN.index);
我吧这条注释了 他就不生成execl了 改成白色,他就不一点反应 还是生成上面那个样子的图
追答setFillBackgroundColor 用这个方法试一下.
C# 导入图片到Excel单元格
导出Excel功能时需要将数据中含有图片也导出到excel单元格中
public void InsertPicture(string RangeName, string PicturePath, Microsoft.Office.Interop.Excel.Worksheet worksheet) { Microsoft.Office.Interop.Excel.Range m_objRange = worksheet.get_Range(RangeName, Type.Missing); m_objRange.Select(); float PicLeft, PicTop, PicWidth, PicHeight; //距离左边距离,顶部距离,图片宽度、高度 PicTop = Convert.ToSingle(m_objRange.Top); PicWidth = Convert.ToSingle(m_objRange.MergeArea.Width); PicHeight = Convert.ToSingle(m_objRange.Height)-10; PicWidth = Convert.ToSingle(m_objRange.Width)-10; PicLeft = Convert.ToSingle(m_objRange.Left);//+ (Convert.ToSingle(rng.MergeArea.Width) - PicWidth) / 2; try { //worksheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, PictuteWidth, PictureHeight); worksheet.Shapes.AddPicture(PicturePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, 90, 70); } catch { } }
以上是关于java类 图片导入到excel 模糊 就是图片被盖上了一层红色的主要内容,如果未能解决你的问题,请参考以下文章