使用EPPlus将图像写入Excel文件

时间:2015-11-16 19:22:32

标签: c# excel image list epplus

我正在尝试使应用程序从字符串类型列表和图像类型列表生成Excel文件。我让用户输入一行然后让他们截取屏幕截图等等,直到按下G,然后它应该生成一个格式如下的Excel文件:

Row 1- Title-0-
Row 2- Header-1-
Row 3- Image-0-
Row 4- Header-2-
Row 5- Image-1-
Row 6- Header-3-
Row 7- Image-2-
Row 8- Header-4-
Row 9- Image-3-
Row 10- Header-5-
Row 11- Image-4-

......依此类推,直到它在集合中全部完成。

我已经创建了List和List,我知道在我点击G之前它们都包含字符串和图像,因为我已经检查了集合调试模式。

这是我到目前为止的代码,excel文件看起来正确,除了没有要查看的图像,但它正在重新调整行的大小到图片高度。我以前从未使用过Image,因此我认为我可能会遗漏一些重要的东西,但不确定是什么。

集合从调用方法传递到此方法 字符串集合命名为" withHeadersList", 图像集合命名为" withImgList"。

生成Excel方法:

public static bool GenerateTestPlan(List<String> withHeadersList, List<Image> withImgList, string stringOutputPath)
        {
            ExcelPackage newExcelPackage = CreateExcelPackage(withHeadersList[0]);
            ExcelWorksheet newExcelWorksheet = CreateWorkSheet(newExcelPackage, "Sheet1");

            SetCellValue(newExcelWorksheet, 1, 1, withHeadersList[0]); //Title
            newExcelWorksheet.Row(1).Style.Font.Size = 35;
            newExcelWorksheet.Row(1).Style.Font.Bold = true;

            int pRowIndex = 3;
            int hRowIndex = 2;
            for (int i = 1; i < withHeadersList.Count; i++)
            {
                SetCellValue(newExcelWorksheet, hRowIndex, 1, withHeadersList[i]);
                newExcelWorksheet.Row(hRowIndex).Style.Font.Size = 20;

                newExcelWorksheet.Row(pRowIndex).Height = withImgList[i - 1].Height;           //Set row height to height of screenshot


                var img = newExcelWorksheet.Drawings.AddPicture(withHeadersList[i], withImgList[i - 1]);    //Add Images (THINK THIS LAST PARAMETER IS THE PROBLEM)
                img.SetPosition(pRowIndex, Pixel2MTU(2), 1, Pixel2MTU(2));
                img.SetSize(withImgList[i - 1].Width, withImgList[i - 1].Height);

                hRowIndex += 2;
                pRowIndex += 2;
            }

            SaveExcelPackage(newExcelPackage, stringOutputPath);

            return true;
        }

Excel File here 正如你所看到的那样,图像并没有被渲染。

1 个答案:

答案 0 :(得分:0)

你的问题肯定在这一行:

img.SetPosition(pRowIndex, Pixel2MTU(2), 1, Pixel2MTU(2));

我不确定为什么要将像素转换为任何内容,而SetPosition正在寻找以像素为单位的偏移量。来自元数据:

    // Summary:
    //     Set the top left corner of a drawing. Note that resizing columns / rows after
    //     using this function will effect the position of the drawing
    //
    // Parameters:
    //   Row:
    //     Start row
    //
    //   RowOffsetPixels:
    //     Offset in pixels
    //
    //   Column:
    //     Start Column
    //
    //   ColumnOffsetPixels:
    //     Offset in pixels
    public void SetPosition(int Row, int RowOffsetPixels, int Column, int ColumnOffsetPixels);

我建议只为RowOffestPixelsColumnOffsetPixels参数传递小值,例如2:

img.SetPosition(pRowIndex, 2, 1, 2);

我从快速谷歌搜索中找到了一个名为Pixel2MTU(int pixels) on codeproject的方法。方法如下:

public int Pixel2MTU(int pixels)
{
    int mtus = pixels * 9525;
    return mtus;
}

如果这与您使用的方法相同,则您的图片可能位于Excel文档的最右下方。