1

I am attempting to add a couple of new columns to an excel file. I can add the column pretty easily, but I can't figure out how to add a column header to the excel file. How on earth do you add a column header to a newly added column? Here is how I add 3 new columns to a An excel worksheet.

    Range rng = (Range)wkSheet.get_Range("A1", Type.Missing);

        rng.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
                                XlInsertFormatOrigin.xlFormatFromRightOrBelow);
        Range rng2 = (Range)wkSheet.get_Range("A1", Type.Missing);

        rng2.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
                                XlInsertFormatOrigin.xlFormatFromRightOrBelow);
        Range rng3 = (Range)wkSheet.get_Range("A1", Type.Missing);

        rng3.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
                                XlInsertFormatOrigin.xlFormatFromRightOrBelow);
        wkSheet.SaveAs("C:\\Code\\UPMC\\ISD\\EADIS\\UPMC.ISD.EADIS.ACO.ACOBackLoad\\App_Data\\test", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
2
  • Headers are just data in X1 where X is the column code. Commented Sep 27, 2011 at 18:00
  • That is what I figured. But I don't understand how you can access the Column objects and change or set the value to something else. I can't figure out if it is in the range object, the worksheet objeect or what? Is there some property that I can set like wksheet.Column or someething like hat?. Commented Sep 27, 2011 at 19:35

1 Answer 1

1

You can write to any cell like so (this just writes to the "header" as an example):

// Type _ = Type.Missing;
wkSheet.Range["A1", _].Value2 = "Heading 1";
wkSheet.Range["B1", _].Value2 = "Heading 2";
wkSheet.Range["C1", _].Value2 = "Heading 3";
Sign up to request clarification or add additional context in comments.

Comments

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.