2011-04-18 110 views
4

我使用npoi来生成excel文档。我有要求将图像添加到单元格。使用下面的代码我可以插入图像到我的文档。然而,图像跨越很多细胞。我如何确保图像恰好适合一次细胞。如何使用npoi在一个单元格中放置图像

public ActionResult NPOICreate() 
{ 
    try 
    { 
     FileStream fs = new FileStream(Server.MapPath(@"\Content\NPOITemplate.xls"), FileMode.Open, FileAccess.ReadWrite); 
     HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true); 
     var sheet = templateWorkbook.GetSheet("Sheet1"); 
     var patriarch = sheet.CreateDrawingPatriarch(); 
     HSSFClientAnchor anchor; 
     anchor = new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short)6, 5); 
     anchor.AnchorType = 2; 
     var picture = patriarch.CreatePicture(anchor, LoadImage(@"D:\dev\Website/HumpbackWhale.jpg", templateWorkbook)); 
     picture.Resize(); 
     picture.LineStyle = HSSFPicture.LINESTYLE_DASHDOTGEL; 
     sheet.ForceFormulaRecalculation = true; 
     MemoryStream ms = new MemoryStream(); 
     templateWorkbook.Write(ms); 
     TempData["Message"] = "Excel report created successfully!"; 
     return File(ms.ToArray(), "application/vnd.ms-excel", "NPOINewFile.xls"); 
    } 
    catch (Exception ex) 
    { 
     TempData["Message"] = "Oops! Something went wrong."; 

     return RedirectToAction("NPOI"); 
    } 

} 

回答

6

据我所知,这是不可能的图像对象分配给在Excel中特定的细胞
这不是POI/NPOI的限制,而是方式的Excel工作:图片插入到电子表格中只是浮动(在电子表格网格本身)...
充其量一个可以相信它是通过确保细胞和图片的大小和位置完美匹配来确定的。有一个图片的属性(请参阅“格式图片”对话框,属性部分,我也确定可以通过POI访问),它允许指定图片是否会在其周围的行/单元格上进行移动和/或调整其自身大小,但最终,图片仍然是一个与单元格非常松散相关的浮动对象,最多只能与单元格相关。

一个共同伎俩分配一个画面时的电池是评论方式。然后图片更正式地绑定到单元格上,但它不会显示为内容,而是显示评论数据。
参见例如this recipe。这个想法是使用评论的背景是一种具有特殊填充效果的颜色,这是我们希望与单元格相关联的图片。这里再次强调,NPOI必须以程序化的方式实现这一目标,但我无法直接肯定这一点。

5

这里的东西,你可以尝试一下:你看那个属性,anchor.AnchorType = 2;?

尝试将其设置为0或3并查看它的功能。在C#(NPOI)端口中,0会将图像与选项0的一个单元格相匹配。

下面是一些示例代码(用于C#Asp.net项目,以防有人在此需要它时徘徊):

HSSFWorkbook hssfworkbook = new HSSFWorkbook(); 
HSSFSheet sheet1 = hssfworkbook.CreateSheet(sheetName); 
//map the path to the img folder 
string imagesPath = System.IO.Path.Combine(Server.MapPath("~"), "img"); 
//grab the image file 
imagesPath = System.IO.Path.Combine(imagesPath, "image.png"); 
//create an image from the path 
System.Drawing.Image image = System.Drawing.Image.FromFile(imagesPath); 
MemoryStream ms = new MemoryStream(); 
//pull the memory stream from the image (I need this for the byte array later) 
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
//the drawing patriarch will hold the anchor and the master information 
HSSFPatriarch patriarch = sheet1.CreateDrawingPatriarch(); 
//store the coordinates of which cell and where in the cell the image goes 
HSSFClientAnchor anchor = new HSSFClientAnchor(20, 0, 40, 20, 3, 10, 4, 11); 
//types are 0, 2, and 3. 0 resizes within the cell, 2 doesn't 
anchor.AnchorType = 2; 
//add the byte array and encode it for the excel file 
int index = hssfworkbook.AddPicture(ms.ToArray(), HSSFPicture.PICTURE_TYPE_PNG); 
HSSFPicture signaturePicture = patriarch.CreatePicture(anchor, index); 
0

它可能通过三个步骤。 首先,你应该插入图片 二,ClientAnchor添加到文件中查找照片到一些 三细胞,用取巧的办法调整图片大小,否则很难使细胞内的图片

相关问题