Excel Column Width

Discussion in 'ASP .Net' started by =?Utf-8?B?SmltIEhlYXZleQ==?=, Oct 11, 2004.

  1. I am creating an excel file and a I am wanting to set a column with to
    something, say 9.20. When I run the Macro recorder, I get code which looks
    like the following:
    Columns("C:C").Select
    Selection.ColumnWidth = 8.6

    If I create an instance the the Sheet object, I would have expected that I
    could just say...

    mySheet.Columns["C:C"].Select
    mySheet.ColumnWidth = 9.20

    But it does not seem to like that. I am also guessing that I should not use
    the "Select", as there is no reason to activate a selection, I believe that
    this inefficient. What is the proper way to do this... And why is it that
    the code above will not work. I am using C#, so I guess I should be using
    [] instead of ( ).

    Thanks in advance for your assistnace!!!
    =?Utf-8?B?SmltIEhlYXZleQ==?=, Oct 11, 2004
    #1
    1. Advertising

  2. Hi,

    Try this:

    using Microsoft.Office.Interop.Excel;

    ApplicationClass NewExcelApplication =
    new ApplicationClass();
    NewExcelApplication.DisplayAlerts = false;
    Workbook book =
    NewExcelApplication.Workbooks.Open(@"C:\Book1.xls",
    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, Type.Missing);
    Worksheet sheet1 =
    (Worksheet)book.Worksheets.get_Item(1);
    ((Range)sheet1.Columns["C", Type.Missing]).ColumnWidth = 30;
    book.Save();
    NewExcelApplication.Quit();


    Greetings
    Martin
    "Jim Heavey" <> wrote in message
    news:...
    > I am creating an excel file and a I am wanting to set a column with to
    > something, say 9.20. When I run the Macro recorder, I get code which

    looks
    > like the following:
    > Columns("C:C").Select
    > Selection.ColumnWidth = 8.6
    >
    > If I create an instance the the Sheet object, I would have expected that I
    > could just say...
    >
    > mySheet.Columns["C:C"].Select
    > mySheet.ColumnWidth = 9.20
    >
    > But it does not seem to like that. I am also guessing that I should not

    use
    > the "Select", as there is no reason to activate a selection, I believe

    that
    > this inefficient. What is the proper way to do this... And why is it that
    > the code above will not work. I am using C#, so I guess I should be

    using
    > [] instead of ( ).
    >
    > Thanks in advance for your assistnace!!!
    Martin Dechev, Oct 11, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?SmltIEhlYXZleQ==?=

    rightsideofwrong

    Joined:
    Oct 11, 2010
    Messages:
    1
    @Martin: It solved my problem as well.. Spot on!
    rightsideofwrong, Oct 11, 2010
    #3
  4. =?Utf-8?B?SmltIEhlYXZleQ==?=

    qvo178

    Joined:
    Aug 28, 2008
    Messages:
    19
    Thanks. excellent work.
    qvo178, Oct 20, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndrewF
    Replies:
    1
    Views:
    766
    Bruce Barker
    Oct 10, 2005
  2. ssk
    Replies:
    5
    Views:
    5,501
    Jukka K. Korpela
    Oct 30, 2006
  3. David Basford
    Replies:
    3
    Views:
    2,419
    BootNic
    Sep 30, 2007
  4. Replies:
    0
    Views:
    929
  5. Carl Howarth

    DG Column Width vs Table Column Width

    Carl Howarth, Oct 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    263
    Eliyahu Goldin
    Oct 24, 2004
Loading...

Share This Page