4

I am trying to map a image into a excel spread sheet and i need to find what the coordinates of the current cell are?

My code at the moment loops though each cell till it finds a matching tag in the excel where it knows this is the cell to place the picture.

I have the cell i just have no idea how to obtain the cells top and left properties?

foreach (ExcelRangeBase cell in range1)
     {

     }

Please help!

Thanks in advance.

4 Answers 4

3
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);
0
1

Cell.Top and Cell.Left would be nice, but they doesn't exist in EPPlus. I was inspired by Pomster and made this function:

/// <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;
    }
1
  • This function works well, however be aware: the Rectangle constructor in GetCellTopLeftCoordinates's final return is used erroneously: the "top" and "left" arguments should be switched.
    – Kaganar
    Feb 10, 2015 at 16:40
0

Try cell.top and cell.left, they exist in the excel object model.

-1

How to get the upper-left corner of an excel range:

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]; 

where wsheet is a Worksheet object.

1
  • 1
    The OP is requesting information specific to EPPlus. Therefore Interop is not useful. Dec 13, 2017 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.