gpt4 book ai didi

excel - Epplus SetPosition图片问题

转载 作者:行者123 更新时间:2023-12-02 02:15:43 27 4
gpt4 key购买 nike

我正在使用Epplus library在 Asp.Net C# 中生成 Excel 2010 及更高版本兼容的文件。我使用的是目前最新的版本 3.1.2。

在添加任何图片之前,我首先设置行高:

ExcelPackage pck = new ExcelPackage();
var ws = pck.Workbook.Worksheets.Add("sheet 1");
while (i < dt.Rows.Count + offset)
{
ws.Row(i).Height = 84;
i++;
}

dt 是我的带有 DataRows 的 DataTable。设置高度后,我再次循环遍历行以添加图片

while (i < dt.Rows.Count + offset)
{
var prodImg = ws.Drawings.AddPicture(dr["code"].ToString(), new FileInfo(path));
prodImg.SetPosition(i - 1, 0, 14, 0);
prodImg.SetSize(75);
}

这可行,但不行:

var prodImg = ws.Drawings.AddPicture(dr["code"].ToString(), new FileInfo(path));
int w = prodImg.Image.Width;
int h = prodImg.Image.Height;

if (h > 140) // because height of 84 is 140 pixels in excel
{
double scale = h / 140.0;
w = (int)Math.Floor(w / scale);
h = 140;
}

int xOff = (150 - w) / 2;
int yOff = (140 - h) / 2;

prodImg.SetPosition(i - 1, xOff, 11, yOff);
prodImg.SetSize(w, h);

这会导致图片偏离中心且图像大小未调整。然后这段代码位于同一个循环中:

var prodImgDm = ws.Drawings.AddPicture("bcdm" + dr["code"].ToString(), new FileInfo(pathDm));
prodImgDm.SetPosition(i - 1, 25, 15, 40);
prodImgDm.SetSize(100);

这有时确实有效。图片 prodImgDm 是具有静态宽度和高度的数据矩阵图像,不需要调整大小,因为它们总是很小/很小。因此,在某些行中如果没有 SetSize ,它也可以工作,而在其他一些行中,它就不起作用。真的很奇怪,因为代码是相同的。它可能是图书馆和/或 Excel 中的东西。也许我使用方法不对?有epplus图片专家吗?

提前致谢!!

编辑有时一张图片胜过一千个文字,所以这里是屏幕截图。正如您所看到的,产品图像在单元格中未水平和垂直对齐。即使我设置了 SetSize(100),最右侧的数据矩阵有时也会缩放约 120%,所以这对我来说真的很奇怪。所以最后一个数据矩阵具有正确的大小...我已经找到 this SO thread但我认为这对我没有帮助。

epplus images

编辑 2013/04/09 Essenpillai 给了我设置的提示

pck.DoAdjustDrawings = false;

但这给了我更奇怪的图像:

doadjustdrawings

数据矩阵仍在按行变化。一行没问题,另一行不行。并且 ean13 代码太宽。

最佳答案

public static void CreatePicture(ExcelWorksheet worksheet, string name, Image image, int firstColumn, int lastColumn, int firstRow, int lastRow, int defaultOffsetPixels)
{
int columnWidth = GetWidthInPixels(worksheet.Cells[firstRow, firstColumn]);
int rowHeight = GetHeightInPixels(worksheet.Cells[firstRow, firstColumn]);

int totalColumnWidth = columnWidth * (lastColumn - firstColumn + 1);
int totalRowHeight = rowHeight * (lastRow - firstRow + 1);
double cellAspectRatio = Convert.ToDouble(totalColumnWidth) / Convert.ToDouble(totalRowHeight);

int imageWidth = image.Width;
int imageHeight = image.Height;
double imageAspectRatio = Convert.ToDouble(imageWidth) / Convert.ToDouble(imageHeight);

int pixelWidth;
int pixelHeight;
if (imageAspectRatio > cellAspectRatio)
{
pixelWidth = totalColumnWidth - defaultOffsetPixels * 2;
pixelHeight = pixelWidth * imageHeight / imageWidth;
}
else
{
pixelHeight = totalRowHeight - defaultOffsetPixels * 2;
pixelWidth = pixelHeight * imageWidth / imageHeight;
}

int rowOffsetPixels = (totalRowHeight - pixelHeight) / 2;
int columnOffsetPixels = (totalColumnWidth - pixelWidth) / 2;

int rowOffsetCount = 0;
int columnOffsetCount = 0;

if (rowOffsetPixels > rowHeight)
{
rowOffsetCount = (int)Math.Floor(Convert.ToDouble(rowOffsetPixels) / Convert.ToDouble(rowHeight));
rowOffsetPixels -= rowHeight * rowOffsetCount;
}

if (columnOffsetPixels > columnWidth)
{
columnOffsetCount = (int)Math.Floor(Convert.ToDouble(columnOffsetPixels) / Convert.ToDouble(columnWidth));
columnOffsetPixels -= columnWidth * columnOffsetCount;
}

int row = firstRow + rowOffsetCount - 1;
int column = firstColumn + columnOffsetCount - 1;

ExcelPicture pic = worksheet.Drawings.AddPicture(name, image);
pic.SetPosition(row, rowOffsetPixels, column, columnOffsetPixels);
pic.SetSize(pixelWidth, pixelHeight);
}

public static int GetHeightInPixels(ExcelRange cell)
{
using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
{
float dpiY = graphics.DpiY;
return (int)(cell.Worksheet.Row(cell.Start.Row).Height * (1 / 72.0) * dpiY);
}
}

public static float MeasureString(string s, Font font)
{
using (var g = Graphics.FromHwnd(IntPtr.Zero))
{
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
return g.MeasureString(s, font, int.MaxValue, StringFormat.GenericTypographic).Width;
}
}

public static int GetWidthInPixels(ExcelRange cell)
{
double columnWidth = cell.Worksheet.Column(cell.Start.Column).Width;
Font font = new Font(cell.Style.Font.Name, cell.Style.Font.Size, FontStyle.Regular);
double pxBaseline = Math.Round(MeasureString("1234567890", font) / 10);
return (int)(columnWidth * pxBaseline);
}

enter image description here

关于excel - Epplus SetPosition图片问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15634709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com