要求,不是简单的只导出数据到EXCEL而是数据里面有一列是图片,要同时将图片一起导出到EXCEL中,比如产品名称,图片,型号,单价,金额图片列在数据库里面要用什么类型?如何导出到EXCEL呢?求助,急
解决方案 »
- 请问朋友们,如何实现邮箱中“今天,昨天,上周,更早”这样的列表?
- 医药行业网站架构图
- 关于论坛用户权限的问题
- 问个Div问题:div上面放个div??
- 在线等,IIS提示要输入拥护名密码
- 发现个好的支持 .net 的免费空间
- 中间代码都一样,用webform 调用 会出错,而winform就不会有问题
- menu 怎么实现类似 input 的mousover 事件 来动态更换菜单项图片?
- MVC中关于过滤器OnException的问题
- 怎样能使datagrid的表格在已经建好的外层表格中垂直居上?
- 求救怎么写好C#语言啊
- 请将注册表值 [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD)设置为 1
HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch();
HSSFClientAnchor anchor;
anchor = new HSSFClientAnchor(0, 0, 0, 255, 2, 2, 4, 7);
anchor.AnchorType = 2;
//C#导出Excel
HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage(“1.jpg”, hssfworkbook));
picture.Resize();
picture.LineStyle = HSSFPicture.LINESTYLE_DASHDOTGEL;
C#导出Excel–加载图片代码public static int LoadImage(string path, HSSFWorkbook wb)
{
System.IO.FileStream file = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[file.Length];
file.Read(buffer, 0, (int)file.Length);
return wb.AddPicture(buffer, PictureType.JPEG);
}
首先要设置HSSFPatriarch对象,然后确定图片显示的区域new HSSFClientAnchor(0, 0, 0, 255, 2, 2, 4, 7),最后加载图片然后显示。通过NPOI来实现C#导出Excel文件
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
List result = (List)model.get("userExcels");
InputStream is=this.getInputStream();
hw=new HSSFWorkbook(is);
is.close();
HSSFSheet sheet=hw.getSheetAt(0);
//真正有意义的是下面这句代码,不管你插入多少图片,都只要生成一个HSSFPatriarch 的对象
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();//一定要放在循环外,只能声明一次。
int rowNum=1;
sheet.setColumnWidth((short)3, (short)2856); //setColumnWidth(short columnIndex, short width) width=2856/35.7=80px;
for(int i=0;i<result.size();i++)
{
HSSFRow row = sheet.createRow(rowNum + i);
row.setHeightInPoints(80);//行高80px;
User user=(User)result.get(i);
row.createCell((short)0).setCellValue(user.getId());
row.createCell((short)1).setCellValue(user.getMobile());
row.createCell((short)2).setCellValue(user.getEmail());
String path = this.getServletContext().getRealPath("/");
String picName=path+user.getPersonPhoto();
if(user.getPersonPhoto()!=null)
{
File file=new File(picName);
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(file);
String s=user.getPersonPhoto();
String ss=s.substring(s.lastIndexOf(".")+1);
if("jpg".equalsIgnoreCase(ss))
{
ImageIO.write(bufferImg, "jpg", byteArrayOut);
HSSFClientAnchor anchor =new HSSFClientAnchor(0,0,1020,250,(short)3,i+1,(short)3,i+1);//dx2最大值 1023,dy2最大值255
//HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)
patriarch.createPicture(anchor,hw.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
//POI只支持DIB,EMF,JPEG,PICT,PNG,WMF格式
} else if("png".equalsIgnoreCase(ss)) {
ImageIO.write(bufferImg, "png", byteArrayOut);
HSSFClientAnchor anchor =new HSSFClientAnchor(0,0,1020,250,(short)3,i+1,(short)3,i+1);
patriarch.createPicture(anchor,hw.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_PNG));
} else{
row.createCell((short)3).setCellValue("无照片");
}
}
}
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("GBK");
hw.write(response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}