1

Hope you can help.

I'm looking for a way to build an excel sheet range by just passing in some parameters. My method is as follows:

private void TableCellformatting(int rowStart, int rowEnd, char colStart, char colEnd)
    {
        char temp;
        StringBuilder s = new StringBuilder();
        excelSheetRange = null;
        for (int i = rowStart; i <= rowEnd; i++)
        {
            temp = colStart;
            while (temp <= colEnd)
            {
                if (s.Length > 0)
                    s.Append(", ");
                s.Append(string.Format("{0}{1}", temp.ToString(), i));
                temp++;
            }
        }
        excelSheetRange = excelSheet.get_Range(s.ToString());
        excelSheetRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
    }

This runs ok until the excelSheet.get_Range part is called. When I pass in the cell numbers manually, I have to do it like this:

get_Range("A1", "A2" etc...)

Any ideas if this is even possible, or if there is another way?

Thanks!

1 Answer 1

2

Use worksheet.range property.

excelSheetRange = excelSheet.Range[excelsheet.Cells[rowstart, colstart], excelsheet.Cells[rowend, colend]];
Sign up to request clarification or add additional context in comments.

3 Comments

link is not valid anymore, do you know where I can find some Info about this? I'm confused with the fact that a Range variable can contain an xlsSheet.Range[xlsSheet.Cells[rowstart, colstart]] type but also an xlsSheet.Range[xlsSheet.Cells[rowstart, colstart], xlsSheet.Cells[rowstart, colstart]] variable
It is just like an overload, xlsSheet.Range[xlsSheet.Cells[rowstart, colstart]] == xlsSheet.Range[xlsSheet.Cells[rowstart, colstart], xlsSheet.Cells[rowstart, colstart]] It's like xlsSheet.Range(Cell) calls xlsSheet.Range(Cell, Cell), a Range is always from one Cell to another Cell a range of one cell is a unique case where the Range starts and stops on the same Cell.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.