将应用程序中的图像添加到excel单元格c#

时间:2017-06-12 08:47:17

标签: c# asp.net asp.net-mvc

我应该写什么来查看来自所有行的数据库的图像

我想在[i,2]行中看到图像。

这是我的代码

int i = 2;
foreach (var item in chartProduct)
{
        ws.Cells[i, 1].Value = item.Id;
        ws.Cells[i, 1].Style.Font.Bold = true;
        ws.Cells[i, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

      string imagePath = _hostingEnvironment.WebRootPath + $@"\images\product\" + item.ImageUrl;
      FileInfo img = new FileInfo(imagePath);
     ExcelPicture pic = ws.Drawings.AddPicture("img", img);
     pic.SetSize(50,50);

      ws.cells[i,2].value = ??

       ws.Cells[i, 3].Value = item.Material.Code;
        ws.Cells[i, 3].Style.Font.Bold = true;
        ws.Cells[i, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

        ws.Cells[i, 4].Value = item.Material.Name;
        ws.Cells[i, 4].Style.Font.Bold = true;
        ws.Cells[i, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

        ws.Cells[i, 5].Value = item.Code;
        ws.Cells[i, 5].Style.Font.Bold = true;
        ws.Cells[i, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

        ws.Cells[i, 6].Value = item.Name;
        ws.Cells[i, 6].Style.Font.Bold = true;
        ws.Cells[i, 6].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
      i++;
}

在这部分我该怎么办?

 string imagePath = _hostingEnvironment.WebRootPath + $@"\images\product\" + item.ImageUrl;
          FileInfo img = new FileInfo(imagePath);
         ExcelPicture pic = ws.Drawings.AddPicture("img", img);
         pic.SetSize(50,50);

          ws.cells[i,2].value = ??

1 个答案:

答案 0 :(得分:0)

尝试这个解决方案,希望它有所帮助。

M = ones(100); %data
M(1:50,:)=0;
pos=[50.2,50];
r = 2;
%calculate what the result should be
h=50-pos(2)+0.5;
A=pi*r^2; 
wedge = acos(h/r)/pi;
triangle = h*sqrt(r^2-h^2);
res=(A*wedge-triangle)/A

S=0;N=0;
for i = 1:size(M,1)
    for j = 1:size(M,2)
        x=abs(j-pos(1));
        y=abs(i-pos(2));
        n=incirc( x,y,r );
        M_(i,j)=n;
        S = S+M(i,j)*n;
        N = N+n;
    end
end
result = S/N

单独添加图片的方法

      int i = 2;
  foreach (var item in chartProduct)
  {
    ws.Cells[i, 1].Value = item.Id;
    ws.Cells[i, 1].Style.Font.Bold = true;
    ws.Cells[i, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
     ws.Column(2).Width = 50;
                ws.Row(i).Height = 50;
 // string imagePath = _hostingEnvironment.WebRootPath + 
//$@"\images\product\" + item.ImageUrl;
string filePath= "~/images/product/" + item.ImageUrl;
 AddPictures(ws, filePath,i,2);

   ws.Cells[i, 3].Value = item.Material.Code;
    ws.Cells[i, 3].Style.Font.Bold = true;
    ws.Cells[i, 3].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

    ws.Cells[i, 4].Value = item.Material.Name;
    ws.Cells[i, 4].Style.Font.Bold = true;
    ws.Cells[i, 4].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

    ws.Cells[i, 5].Value = item.Code;
    ws.Cells[i, 5].Style.Font.Bold = true;
    ws.Cells[i, 5].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;

    ws.Cells[i, 6].Value = item.Name;
    ws.Cells[i, 6].Style.Font.Bold = true;
    ws.Cells[i, 6].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
  i++;
  }