4

我正在使用npoi生成 excel 文档。我需要将图像添加到单元格。使用以下代码,我可以将图像插入到我的文档中。然而,图像跨越许多单元格。我怎样才能确保图像只适合一次单元格。

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");
    }

}
4

3 回答 3

6

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

将图片分配给单元格的常用技巧 是通过注释。然后图片更正式地绑定到单元格,但它不显示为内容,而是显示为评论数据。 例如见这个食谱。这个想法是使用评论的背景作为具有特殊填充效果的颜色,这是我们希望与单元格关联的图片。再一次,必须有一种方法可以通过 NPOI 以编程方式实现这一点,但我无法直接确认这一点。

于 2011-04-18T23:55:26.590 回答
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);
于 2011-12-12T20:24:39.133 回答
0

可以通过三个步骤。首先,您应该插入图片其次,将ClientAnchor添加到文件中以将图片定位到某些单元格第三,使用棘手的方式调整图片的大小,否则很难在单元格内制作图片

于 2013-11-06T06:11:20.700 回答