c# coding

Good-morning,
need some help, please.
I've written a small c# application that basically takes data from an access database and dumps it into a gridview. One of the features that the user has is the ability to export that data into an excel file.
I need help formatting the columns in the excel file.
As you can see from the attached jpegs, all the fields are the same length(size). As you can also see the date does not fit into the view. I have to manually grab the side of the column and drag it. Is there a way that I can code what size I want the columns to be in C# as the data gets exported? please & thank you.
c-1.JPG
c-2.JPG
MrMayAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

ws.Cells.Style.EntireColumn.AutoFit();


Try this.
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

You can also do-

ws.Columns(2).ColumnWidth = 100;
Rgonzo1971Commented:
HI,

pls try

ws.Cells.EntireColumn.AutoFit();

Open in new window

Regards
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

MrMayAuthor Commented:
how would i code that...


           ws.Cells.Style.EntireColumn.Autofit();
            ws.Cells[1, 1] = "Dept";
            ws.Cells[1, 2] = "Date";
            ws.Cells[1, 3] = "Machine";
            ws.Cells[1, 4] = "Symptons";
            ws.Cells[1, 5] = "Issue";
            ws.Cells[1, 6] = "Root Cause";
            ws.Cells[1, 7] = "Remedy";
            ws.Cells[1, 8] = "Maint. Time";
            ws.Cells[1, 9] = "MRDT";
            ws.Cells[1, 10] = "Wrench Time";
Rgonzo1971Commented:
After inserting the text in the columns (EDIT at best not after inserting your header but after inserting the data)

           
             ws.Cells[1, 1] = "Dept";
             ws.Cells[1, 2] = "Date";
             ws.Cells[1, 3] = "Machine";
             ws.Cells[1, 4] = "Symptons";
             ws.Cells[1, 5] = "Issue";
             ws.Cells[1, 6] = "Root Cause";
             ws.Cells[1, 7] = "Remedy";
             ws.Cells[1, 8] = "Maint. Time";
             ws.Cells[1, 9] = "MRDT";
             ws.Cells[1, 10] = "Wrench Time";
             ws.Cells.Style.EntireColumn.Autofit();

Open in new window

MrMayAuthor Commented:
This does not work... i get an error msg. please see attached.
c-error.JPG
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

Try this-

Excel.Range r;
r = (Excel.Range)ws.Cells[1, i];
r.EntireColumn.AutoFit();
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

You also would have some more options... Try-

ws.Columns.AutoFit();

or

Excel.Range oRng = ws.get_Range("A1", "J1");
oRng.EntireColumn.AutoFit();
Prakash SamariyaIT ProfessionalCommented:
You can try below
ws.Cells[1,2].EntireColumn.ColumnWidth = 35;

or
ws.Cells[1,2].EntireColumn.AutoFit()

or
ws.Columns.AutoFit();
MrMayAuthor Commented:
is it possible to put this excel macro into my c# code;

  Cells.Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub
Prakash SamariyaIT ProfessionalCommented:
// Create a new VBA code module. from (Excel.workbook) wb object
VBIDE.VBComponent oModule;
oModule = wb.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);

// write your macro code in string like      
sCode =
      "sub VBAMacro()\r\n" +
      "   msgbox \"VBA Macro called\"\r\n" +
      "end sub";

      
// Add the VBA macro to the new code module.
oModule.CodeModule.AddFromString(sCode);

//Run the named VBA Sub that we just added. "VBAMacro" (Parameters list by comma separated)
wb.Application.Run("VBAMacro",Param1,Param2,....);


For more in details please check link: https://support.microsoft.com/en-us/kb/303872

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.