【问题标题】:Find the Coordinates of a cell top left ? Working with EPPlus找到左上角单元格的坐标?使用 EPPlus
【发布时间】:2017-04-30 20:47:22
【问题描述】:

我正在尝试将图像映射到 Excel 电子表格中,我需要找到当前单元格的坐标是什么?

我的代码目前循环遍历每个单元格,直到它在 Excel 中找到匹配的标签,它知道这是放置图片的单元格。

我有单元格,只是不知道如何获取单元格的顶部和左侧属性?

foreach (ExcelRangeBase cell in range1)
     {

     }

请帮忙!

提前致谢。

【问题讨论】:

    标签: c# excel epplus


    【解决方案1】:
    int row = cellAddress.Start.Row;
    int column = cellAddress.Start.Column;
    
    
    if (mCellsResult.Count() > 0)
    {
    var mCells = mCellsResult.First();
    
    //if the cell and the merged cell do not share a common start address then skip this cell as it's already been covered by a previous item
    if (mCells.Start.Address != cellAddress.Start.Address)
                                                            continue;
    
    if (mCells.Start.Column != mCells.End.Column)
    {
    colSpan += mCells.End.Column - mCells.Start.Column;
    }
    
    if (mCells.Start.Row != mCells.End.Row)
    {
       rowSpan += mCells.End.Row - mCells.Start.Row;
    }
    }
    double height = 0, width = 0;
    for (int h = 0; h < rowSpan; h++)
    {
    height += xlWorkSeet1[k].Row(row + h).Height;
    }
    for (int w = 0; w < colSpan; w++)
    {
     width += xlWorkSeet1[k].Column(column + w).Width;
    }
    
    double pointToPixel = 0.75;
    
    height /= pointToPixel;
    width /= 0.1423;
    
    
    picture = xlWorkSeet1[k].Drawings.AddPicture(System.Guid.NewGuid().ToString() + row.ToString() + column.ToString(), image);
    picture.From.Column = column - 1;
    picture.From.Row = row - 1;
    picture.SetSize((int)width, (int)height);
    

    【讨论】:

      【解决方案2】:

      Cell.Top 和 Cell.Left 会很好,但它们在 EPPlus 中不存在。 我受到 Pomster 的启发,制作了这个函数:

      /// <summary>Get top, left, width and height of a given cell.</summary>
          Rectangle GetCellTopLeftCoordinates(ExcelWorksheet worksheet, ExcelRangeBase cell)
          {
              double top = 0, left = 0, width = 0, height = 0;
      
              //Get top and left position:
              for (int i = 1; i < cell.Start.Row; i++)
                  top += worksheet.Row(i).Height;
      
              for (int i = 1; i < cell.Start.Column; i++)
                  left += worksheet.Column(i).Width;
      
              //Get width and height:
              if (cell.Merge)  //Then the cell are merged with others:
              {
                  foreach (string address in worksheet.MergedCells)  //Loop through all merged cells:
                      if (Intersect(worksheet.Cells[address], cell))  //Then we have found the particular, merged range:
                      {
                          ExcelRange range = worksheet.SelectedRange[address];
      
                          for (int i = range.Start.Row; i <= range.End.Row; i++)
                              height += worksheet.Row(i).Height;
      
                          for (int i = range.Start.Column; i <= range.End.Column; i++)
                              width += worksheet.Column(i).Width;
      
                          break;
                      }
              }
              else //No merges - just get dimensions:
              {
                  width = worksheet.Column(cell.Start.Column).Width;
                  height = worksheet.Row(cell.Start.Row).Height;
              }
      
              //Convert point to pixels:
              top /= 0.75;
              left /= 0.1423;
              height /= 0.75;
              width /= 0.1423;
              return new Rectangle((int)left, (int)top, (int)width, (int)height);
          }
      
          bool Intersect(ExcelRange range, ExcelRangeBase cell)
          {
              foreach (ExcelRangeBase item in range)
                  if (item.Address == cell.Address)
                      return true;
      
              return false;
          }
      

      【讨论】:

      • 此函数运行良好,但请注意:GetCellTopLeftCoordinates 最终返回的 Rectangle 构造函数使用错误:“top”和“left”参数应切换。
      【解决方案3】:

      试试cell.topcell.left,它们存在于excel对象模型中。

      【讨论】:

        【解决方案4】:

        如何获取excel范围的左上角:

        using Excel = Microsoft.Office.Interop.Excel;
        ...
        // get upper left corner of range defined by a RangeStr like "B2:D4"
        Excel.Range UpperLeftCell = (Excel.Range)wsheet.get_Range(RangeStr).Cells[1,1]; 
        

        其中 wsheet 是 Worksheet 对象。

        【讨论】:

        • OP 正在请求特定于 EPPlus 的信息。因此互操作没有用。
        猜你喜欢
        • 2021-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多