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
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
Hi,
You can also do-
ws.Columns(2).ColumnWidth = 100;
You can also do-
ws.Columns(2).ColumnWidth = 100;
HI,
pls try
pls try
ws.Cells.EntireColumn.AutoFit();
Regards
ASKER
how would i code that...
ws.Cells.Style.EntireColum n.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";
ws.Cells.Style.EntireColum
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();
ASKER
This does not work... i get an error msg. please see attached.
c-error.JPG
c-error.JPG
Hi,
Try this-
Excel.Range r;
r = (Excel.Range)ws.Cells[1, i];
r.EntireColumn.AutoFit();
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 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 .ColumnWid th = 35;
or
ws.Cells[1,2].EntireColumn .AutoFit()
or
ws.Columns.AutoFit();
ws.Cells[1,2].EntireColumn
or
ws.Cells[1,2].EntireColumn
or
ws.Columns.AutoFit();
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ws.Cells.Style.EntireColum
Try this.