2011-04-18 111 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添加到文件中查找照片到一些 三細胞,用取巧的辦法調整圖片大小,否則很難使細胞內的圖片

相關問題