Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I'm working with Excel files on this days.

I need to insert a column beetween some others.

I'm using this code and it's works fine.

Excel.Range araAt = raporWrkS.Range["K1"];
                                            araAt.EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight,
                    Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow);


On this code i wrote K1 just for sample. And works in a if loop.
I'm starting from the first column and going to right with every new data. But sometimes i need to insert a new column beetween this columns.And using this code than.

But how can i write column name automatically. For example if new row must to be added on F1 than K1 must be F1. So i need to get column name from that. But i can't get column name. Looked lot of documents on internet but all of them has NUMERICAL column name.

Could you show me a way, about how can i get the column name ?

Thank you.

What I have tried:

Looked the internet for ColumnName property or something like that.
Posted
Updated 21-Jun-17 22:04pm
Comments
kozmikadam 22-Jun-17 4:02am    
I solved my problem. Get column count ( i get with for loop ) and then i convert it to alphabetical version.

private string GetExcelColumnName(int columnNumber)
{
int dividend = columnNumber;
string columnName = String.Empty;
int modulo;

while (dividend > 0)
{
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = (int)((dividend - modulo) / 26);
}

return columnName;
}

Here is the solution.

First of all i get column count with a for loop. I count every column in this loop. Already have this loop also.

And then convert the count of column to alphabetical version with this code.

private string GetExcelColumnName(int columnNumber)
{
    int dividend = columnNumber;
    string columnName = String.Empty;
    int modulo;

    while (dividend > 0)
    {
        modulo = (dividend - 1) % 26;
        columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
        dividend = (int)((dividend - modulo) / 26);
    } 

    return columnName;
}
 
Share this answer
 
I wonder why you are using those coordinates which is a GUI thing for users ?
Why don't you tell your program to use directly the column number ?
C#
    Excel.Range araAt = raporWrkS.cells(columnNumber,1);
    araAt.EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight,
Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow);
 
Share this answer
 
Comments
kozmikadam 23-Jun-17 2:42am    
Hi, accually i looked like an answer like this one. And couldn't find. I will try this suggestion today. Thank you.
You can get column name from the bellow code. Then you can put new column name.

Excel.Application xlApp = new Excel.Application();
    Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("workbookname");
    Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // assume it is the first sheet
    int columnCount = xlWorksheet.UsedRange.Columns.Count;
    List<string> columnNames = new List<string>();
    for (int c = 1; c < columnCount; c++)
    {
        if (xlWorksheet.Cells[1, c].Value2 != null)
        {
            string columnName = xlWorksheet.Columns[c].Address;
            Regex reg = new Regex(@"(\$)(\w*):");
            if (reg.IsMatch(columnName))
            {
                Match match = reg.Match(columnName);
                columnNames.Add(match.Groups[2].Value);
            }
       }
    }
 
Share this answer
 
Comments
kozmikadam 22-Jun-17 4:05am    
Thank you ! This is works also. But nearly 1 hour ago i solved it another way. Thank you for answer anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900