POI导出图片到excel的bug或者说填充颜色
刚刚接触poi的导出
导出之前的默认图片,图片的背景色是透明的
出来后会变成这样子,怎么解决。。
出现问题:如果被导出的图片为背景色透明的话导出来之后图片颜色会失真,求解决办法。
导出方法如下
package com.idcsol.bml.web.controller; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.idcsol.bml.domain.Product; public class ViewController { /** log 对象 */ protected final Log log = LogFactory.getLog(this.getClass()); /** * 记录写成excel表 * @param cunForm * @param wb * @param dPicPath * @param mPicpath * @param nPicpath * @return */ @SuppressWarnings("finally") public HSSFWorkbook createExcel(Product product,HSSFWorkbook wb, String picPath, String dPicPath,HttpServletRequest request, String mPicpath, String nPicpath) throws Exception{ HSSFCellStyle style1 = wb.createCellStyle(); style1.setBorderTop((short)1); style1.setBorderLeft((short)1); style1.setBorderRight((short)1); style1.setBorderBottom((short)1); HSSFSheet sheet = wb.getSheet("产品信息"); BufferedImage bufferImg = null; BufferedImage bufferImg1 = null; try { //插入文字信息 HSSFRow row=sheet.getRow(12); row.getCell(2).setCellValue(product.getSupplier().getSupplierNm()); HSSFRow row1=sheet.getRow(13); row1.getCell(2).setCellValue(product.getPerson().getPersonPM()); HSSFRow row2=sheet.getRow(14); row2.getCell(2).setCellValue(product.getPerson().getTelNo()); row2.getCell(4).setCellValue(product.getPerson().getMobileNo()); HSSFRow row3=sheet.getRow(15); row3.getCell(2).setCellValue(product.getPerson().getFaxNo()); HSSFRow row4=sheet.getRow(18); row4.getCell(2).setCellValue(product.getTitle()); HSSFRow row5=sheet.getRow(19); row5.getCell(2).setCellValue(product.getBrandnm()); HSSFRow row6=sheet.getRow(20); row6.getCell(2).setCellValue(product.getModelno()); HSSFRow row7=sheet.getRow(21); row7.getCell(2).setCellValue(product.getDimensions()); HSSFRow row8=sheet.getRow(22); row8.getCell(2).setCellValue(product.getNote()); HSSFRow row9=sheet.getRow(33); //数量 if(product.getQuantity()!= null ){ row9.getCell(2).setCellValue(String.valueOf(product.getQuantity())); } //单价 if(product.getRate()!= null ){ row9.getCell(6).setCellValue(String.valueOf(product.getRate())); } HSSFRow row10=sheet.getRow(34); row10.getCell(2).setCellValue(product.getItem()); row10.getCell(6).setCellValue(product.getItemno()); //图片写入excel HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream(); ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream(); if (!"".equals(dPicPath) && dPicPath != null) { bufferImg = ImageIO.read(new File(dPicPath)); }else { String path = request.getSession().getServletContext().getRealPath("/files/comm/logo1.jpg");//文件路径 bufferImg = ImageIO.read(new File(path)); } if (!"".equals(mPicpath) && mPicpath != null) { bufferImg1 = ImageIO.read(new File(picPath)); ImageIO.write(bufferImg1,"jpg",byteArrayOut1); HSSFClientAnchor anchor1 = new HSSFClientAnchor(0,0,200,200,(short) 0,24,(short)1,30); //控制产品图片输出位置 anchor1.setAnchorType(2); patriarch.createPicture(anchor1 , wb.addPicture(byteArrayOut1.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG)); } ImageIO.write(bufferImg,"jpg",byteArrayOut); HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,200,200,(short) 5,1,(short)6,3);//设计师的logo图片 anchor.setAnchorType(2); patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG)); //输出实际尺寸参数:HSSFWorkbook.PICTURE_TYPE_JPEG)).resize(1);按比例输出50% .resize(0.5); }catch(Exception e){ System.out.println(e.toString()); } finally { return wb; } } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有什么好的方法请赐教,菜鸟刚上道,之后还要做成批量多sheet导出的,求个简洁方法呀