使用 EPPlus 将图像写入 Excel 文件
Writing Images to Excel file using 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 文件看起来不错,只是看不到任何图像,但是它正在将行的大小调整为图片的高度。我以前从未使用过图像,所以我想我可能会遗漏一些重要但不确定是什么的东西。
集合从调用方法传递到此方法
字符串集合被命名为 "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
如您所见,图像好像没有被渲染。
您的问题肯定与此行有关:
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);
我建议只为 RowOffestPixels
和 ColumnOffsetPixels
参数传递较小的值,例如 2:
img.SetPosition(pRowIndex, 2, 1, 2);
我通过快速 google 搜索找到了一个名为 Pixel2MTU(int pixels) on codeproject 的方法。方法如下:
public int Pixel2MTU(int pixels)
{
int mtus = pixels * 9525;
return mtus;
}
如果这与您使用的方法相同,则您的图像可能位于 excel 文档的最右下角。
我试图让应用程序从字符串类型列表和图像类型列表生成一个 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 如您所见,图像好像没有被渲染。
您的问题肯定与此行有关:
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);
我建议只为 RowOffestPixels
和 ColumnOffsetPixels
参数传递较小的值,例如 2:
img.SetPosition(pRowIndex, 2, 1, 2);
我通过快速 google 搜索找到了一个名为 Pixel2MTU(int pixels) on codeproject 的方法。方法如下:
public int Pixel2MTU(int pixels)
{
int mtus = pixels * 9525;
return mtus;
}
如果这与您使用的方法相同,则您的图像可能位于 excel 文档的最右下角。