5

I have code that is exporting to excel and i have an array of columns . .

  var colheaders = new string[] {"Name", "Age", "Total", "Date"}

right now i have code that looks like this to setup the headers

            excelExport.SetCell("A", 1, "Name");
            excelExport.SetCell("B", 1, "Age");
            excelExport.SetCell("C", 1, "Total");
            excelExport.SetCell("D", 1, "Date");

the issue is that if i have 50 columns and i want to add one at the beginnging, i have to go and updated the letter in each column "A", "B", "C", etc . .

since i have an array already of string headers i would like something like this:

  foreach (string colheader in colheaders)
  {
     excelExport.SetCell("A", 1, colheader);
  }

but i need to dynamically set the letter "A" in this case. Something like this:

  int i = 0;
  foreach (string colheader in colheaders)
  {
     excelExport.SetCell(GetCol(i), 1, colheader);
     i++;
  }

NOTE:

Also, after Z, i need to go to AA, then AB, then AC, etc . . to match the Excel columns so the logic would have to go beyond 26 columns

leora
  • 188,729
  • 360
  • 878
  • 1,366
  • 1
    take a look at here http://stackoverflow.com/questions/1011732/iterating-through-the-alphabet-c-sharp-a-caz it might be help you ... – Glory Raj Dec 28 '11 at 18:36
  • http://stackoverflow.com/questions/181596/how-to-convert-a-column-number-eg-127-into-an-excel-column-eg-aa – Jimmy Dec 28 '11 at 18:53
  • 1
    I don't know what that `SetCell` function is doing, but one little-known fact about Excel is that you can use numbers to refer to columns rather than letters: `Cells(1, "AA")` is the same as `Cells(1, 27)`. – phoog Dec 28 '11 at 18:57
  • I found a working solution [here][1]: [1]: http://stackoverflow.com/questions/837155/fastest-function-to-generate-excel-column-letters-in-c-sharp – leora Dec 28 '11 at 20:54
  • @leora I suggest you close your own question as a duplicate (rather than have other people flag it for closure). – Matthew Strawbridge Dec 28 '11 at 22:53
  • @Matthew Strawbridge - I did but i think it still needs a few more close votes – leora Dec 28 '11 at 23:21

4 Answers4

5

This isn't the most elegant solution but it does yield the result you are looking for.

const int maxAlpha = 26;
int charNum;
var timesMaxed = 0;
var counter = 0;
var asciiStartPoint = 65;

foreach (string colheader in colheaders)
{
    var result = String.Empty;

    if (counter == maxAlpha)
    {
        timesMaxed++;
        counter = 0;
    }

    if (timesMaxed > 0)
    {
        charNum = asciiStartPoint + (timesMaxed - 1);
        result += ((char)charNum).ToString();
    }

    charNum = asciiStartPoint + counter;
    result += ((char)charNum).ToString();

    excelExport.SetCell(result, 1, colheader);
    counter++;
}
Scott Boettger
  • 3,657
  • 2
  • 33
  • 44
4

I was able to find another question on stackoverflow that had a working solution:

Here is the question: Fastest function to generate Excel column letters in C#

and the answer is the: ExcelColumnFromNumber() method

Community
  • 1
  • 1
leora
  • 188,729
  • 360
  • 878
  • 1,366
1
    private List<string> GetCol()
    {
        var x = Enumerable.Range(65, 26).Select(p => (char)p).ToList();
        List<string> result = new List<string>();
        foreach (var first in x)
        {
            result.Add(first.ToString());
        }

        foreach (var first in x)
        {
            foreach (var second in x)
            {
                result.Add(first.ToString() + second.ToString());
            }
        }
        return result;
    }
Reza ArabQaeni
  • 4,848
  • 27
  • 46
0

Depending on the definition of SetCell, you may be able to do this:

int i = 0; 
foreach (string colheader in colheaders) 
{ 
    excelExport.SetCell(++i, 1, colheader);
} 

This would work, for example, if SetCell were defined thus:

void SetCell(object col, object row, object value)
{
    _excelApp.Cells(row, col).set_Value(value);
}
phoog
  • 42,068
  • 6
  • 79
  • 117