32

I am using standard library

using Excel = Microsoft.Office.Interop.Excel;

And this is how I create Excel, just small part of code:

//Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
Excel._Application xlApp = new Excel.Application();

xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//add data 
xlWorkSheet.Cells[1, 1] = "";
xlWorkSheet.Cells[1, 2] = "Student1";
xlWorkSheet.Cells[1, 3] = "Student2";
xlWorkSheet.Cells[1, 4] = "Student3";

The problem is that sometimes size of cell can be smaller that text`s size. I tried this one:

Excel.Range chartRange;
chartRange.EntireColumn.ColumnWidth = 31.43;

It works fine, but I need to set this property for each column separately. How I can I do that?

2
  • 1
    better learn to save macros. that way you will have the code of ALL you need. I dont know it what I did in my answer I saved a macro and give you my result. it is really easy you just start recording, do what you want to record in excel, and then you got the code of waht you did. its so powerfull and usefull
    – bto.rdz
    Dec 15, 2013 at 16:30
  • Thank you for advise, I found pretty nice article about it blogs.msdn.com/b/csharpfaq/archive/2010/09/28/…
    – Rocketq
    Dec 15, 2013 at 17:54

7 Answers 7

53

I normally do this in VB and its easier because Excel records macros in VB. So I normally go to Excel and save the macro I want to do.

So that's what I did now and I got this code:

Columns("E:E").ColumnWidth = 17.29;

Range("E3").Interior.Pattern = xlSolid;
Range("E3").Interior.PatternColorIndex = xlAutomatic;
Range("E3").Interior.Color = 65535;
Range("E3").Interior.TintAndShade = 0;
Range("E3").Interior.PatternTintAndShade = 0;

I think you can do something like this:

xlWorkSheet.Columns[5].ColumnWidth = 18;

For your last question what you need to do is loop trough the columns you want to set their width:

for (int i = 1; i <= 10; i++) // this will apply it from col 1 to 10
{
    xlWorkSheet.Columns[i].ColumnWidth = 18;
}
4
  • 2
    xlWorkSheet.Columns[5].ColumnWidth = 18; this is work as should ))
    – Rocketq
    Dec 15, 2013 at 16:05
  • Doesn't work for me; this code (_xlSheet is my Worksheet) recognizes no "ColumnWidth" member: xlSheet.Columns[ITEMDESC_COL]. Nov 12, 2015 at 23:27
  • And now it works (compiles, anyway - haven't tested it yet); I've got this in a for loop: _xlSheet.Columns[i].ColumnWidth = 12.75; Why the difference between these two instances, I don't know... Feb 4, 2016 at 16:49
  • for me, its strange. xlWorkSheet.Columns[rowindex, columnindex] ask for row and column index
    – Krish
    Feb 8, 2017 at 11:18
37

I did it this way:

var xlApp = new Excel.Application();
var xlWorkBook = xlApp.Workbooks.Add(System.Reflection.Missing.Value);
var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Item[1];
xlWorkSheet.Columns.AutoFit();

With this way, columns always fit to text width inside cells.

Hope it helps to someone!

2
  • Exactly what I wanted :)
    – Rama
    Feb 29, 2016 at 9:16
  • 1
    I like this but take note it depends on existing content so add your content before calling AutoFit() .
    – eric1825
    Jun 3, 2020 at 17:03
5

Use this One

 ((Excel.Range)oSheet.Cells[1, 1]).EntireColumn.ColumnWidth = 10;
4

This link explains how to apply a cell style to a range of cells: http://msdn.microsoft.com/en-us/library/f1hh9fza.aspx

See this snippet:

Microsoft.Office.Tools.Excel.NamedRange rangeStyles =
this.Controls.AddNamedRange(this.Range["A1"], "rangeStyles");

rangeStyles.Value2 = "'Style Test";
rangeStyles.Style = "NewStyle";
rangeStyles.Columns.AutoFit();
4

I have change all columns width in my case as

            worksheet.Columns[1].ColumnWidth = 7;
            worksheet.Columns[2].ColumnWidth = 15;
            worksheet.Columns[3].ColumnWidth = 15;
            worksheet.Columns[4].ColumnWidth = 15;
            worksheet.Columns[5].ColumnWidth = 18;
            worksheet.Columns[6].ColumnWidth = 8;
            worksheet.Columns[7].ColumnWidth = 13;
            worksheet.Columns[8].ColumnWidth = 17;
            worksheet.Columns[9].ColumnWidth = 17;

Note: Columns in worksheet start with 1 not from 0 as in Arrary.

1

Excel ColumnWidth from dataGridView:

           foreach (DataGridViewColumn co in dataGridView1.Columns)
           { worksheet.Columns[co.Index + 1].ColumnWidth = co.Width/8; }
0

See this snippet: (C#)

    private Microsoft.Office.Interop.Excel.Application xla;
    Workbook wb;
    Worksheet ws;
    Range rg;
    ..........

        xla = new Microsoft.Office.Interop.Excel.Application();
        wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
        ws = (Worksheet)xla.ActiveSheet;
        rg = (Range)ws.Cells[1, 2];
        rg.ColumnWidth = 10;
        rg.Value2 = "Frequency";
        rg = (Range)ws.Cells[1, 3];
        rg.ColumnWidth = 15;
        rg.Value2 = "Impudence";
        rg = (Range)ws.Cells[1, 4];
        rg.ColumnWidth = 8;
        rg.Value2 = "Phase";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.