Link to home
Start Free TrialLog in
Avatar of MrMay
MrMay

asked on

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
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

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


Try this.
Hi,

You can also do-

ws.Columns(2).ColumnWidth = 100;
Avatar of Rgonzo1971
Rgonzo1971

HI,

pls try

ws.Cells.EntireColumn.AutoFit();

Open in new window

Regards
Avatar of MrMay

ASKER

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";
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

Avatar of MrMay

ASKER

This does not work... i get an error msg. please see attached.
c-error.JPG
Hi,

Try this-

Excel.Range r;
r = (Excel.Range)ws.Cells[1, i];
r.EntireColumn.AutoFit();
Hi,

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

ws.Columns.AutoFit();

or

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

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

or
ws.Columns.AutoFit();
Avatar of MrMay

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Prakash Samariya
Prakash Samariya
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial