【问题标题】:Writing Images to Excel file using EPPlus使用 EPPlus 将图像写入 Excel 文件
【发布时间】:2015-11-19 00:55:55
【问题描述】:

我正在尝试让应用程序从字符串类型列表和图像类型列表生成 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 文件看起来正确,除了没有可以看到的图像,但是它正在将行的大小重新调整为图片的高度。我以前从未使用过图像,所以我想我可能会遗漏一些重要的东西,但不确定是什么。

集合从调用方法传递到此方法 字符串集合命名为“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 如您所见,就像图像没有被渲染。

【问题讨论】:

  • Pixel2MTU 在做什么?

标签: c# excel image list epplus


【解决方案1】:

您的问题肯定与这一行有关:

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 文档的最右下角。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    相关资源
    最近更新 更多