我怎么能使用NPOI放置一个图像只在一个单元格 [英] how I can place an image just in one cell using npoi

查看:936
本文介绍了我怎么能使用NPOI放置一个图像只在一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NPOI 生成Excel文档。我有一个要求,图像添加到细胞。使用下面code I可以插入图片到我的文档。然而,图像跨越多个单元。我如何能确保图像只适合内一次电池。

 公众的ActionResult NPOICreate()
{
    尝试
    {
        的FileStream FS =新的FileStream(使用Server.Mappath(@\\内容\\ NPOITemplate.xls),FileMode.Open,FileAccess.ReadWrite);
        HSSFWorkbook templateWorkbook =新HSSFWorkbook(FS,真);
        无功表= templateWorkbook.GetSheet(工作表Sheet1);
        VAR族长= sheet.CreateDrawingPatriarch();
        HSSFClientAnchor锚;
        锚=新HSSFClientAnchor(0,0,0,0,(短)4,2,(短)6,5);
        anchor.AnchorType = 2;
        VAR照片= patriarch.CreatePicture(锚,的LoadImage(@D:\\ dev的\\网站/ HumpbackWhale.jpg,templateWorkbook));
        picture.Resize();
        picture.LineStyle = HSSFPicture.LINESTYLE_DASHDOTGEL;
        sheet.ForceFormulaRecalculation =真;
        MemoryStream的毫秒=新的MemoryStream();
        templateWorkbook.Write(毫秒);
        TempData的[消息] =Excel报表创建成功!;
        返回文件(ms.ToArray(),应用程序/ vnd.ms-EXCEL,NPOINewFile.xls);
    }
    赶上(异常前)
    {
        TempData的[消息] =!哎呀出事了。        返回RedirectToAction(NPOI);
    }}


解决方案

要尽我所知的,这是不可能的图像对象分配给特定的细胞在Excel中。

这不是POI / NPOI的限制,而是方式的Excel工作原理:插入为preadsheet图片只是浮动的(超过在S preadsheet电网的本身)...结果
在最好的一个可以使相信这是在细胞中通过确保尺寸和单元和图像匹配完美的位置。有图片的属性(请参阅设置图片格式对话框,属性部分,也可以通过POI访问我敢肯定),它允许指定画面是否会移动和/或调整自身周围之后它行/单元格的动作,但最终, 图片仍然非常松散的联系小区浮动对象是在最好的。

有一个共同的以一个单元是通过评论办法指定图片。图像然后更正式结合于细胞,但它没有示出作为内容而是注释的数据。结果,
例如,见这个配方。该想法是使用注释的背景是一个特殊的填充效果这是我们希望与该小区相关联的图像的颜色。在这里,我们有了与NPOI编程方式实现这一目标的一种方式,但我不能肯定第一手此。

I'm using npoi to generate excel docs. I have a requirement to add images to cells. Using the following code i can insert images into my doc. However the image span many cells. How can i ensure that the image just fits inside once cell.

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

}

解决方案

To the best of my knowledge, it isn't possible to assign an image object to a particular cell within Excel.
This is not a limitation of POI/NPOI, but rather the way Excel works: Pictures inserted into a spreadsheet just float (over the spreadsheet grid per se)...
At best one can make believe it is in the cell by ensuring that the size and position of the cell and of the picture match perfectly. There is a property of the picture (See "Format Picture" dialog, Properties section, also accessible through POI I'm sure) which allows to specify whether the picture will move and/or resize itself following actions on rows/cells surrounding it, but in the end, pictures remain a floating object very loosely related to a cell at best.

A common trick to assign a picture to a cell is by way of comments. The picture is then more formally bound to the cell but it is not shown as content but rather a comment data.
See for example this recipe. The idea is to use the background of the comment to be a color with a special fill effect which is the picture we wish to associate with the cell. Here again, there's got to be a way of achieving this programmatically with NPOI, but I cannot affirm this firsthand.

这篇关于我怎么能使用NPOI放置一个图像只在一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆