Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I auto size columns through the Excel interop objects?

Below is the code I'm using to load the data into an Excel worksheet, but I'm look to auto size the column after the data is loaded. Does anyone know the best way to auto size the columns?

using Microsoft.Office.Interop;  public class ExportReport {     public void Export()     {         Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();         Excel.Workbook wb;         Excel.Worksheet ws;         Excel.Range aRange;         object m = Type.Missing;         string[,] data;         string errorMessage = string.Empty;         try         {             if (excelApp == null)                 throw new Exception("EXCEL could not be started.");              // Create the workbook and worksheet.             wb = excelApp.Workbooks.Add(Office.Excel.XlWBATemplate.xlWBATWorksheet);             ws = (Office.Excel.Worksheet)wb.Worksheets[1];              if (ws == null)                 throw new Exception("Could not create worksheet.");              // Set the range to fill.             aRange = ws.get_Range("A1", "E100");              if (aRange == null)                 throw new Exception("Could not get a range.");              // Load the column headers.             data = new string[100, 5];             data[0, 0] = "Column 1";             data[0, 1] = "Column 2";             data[0, 2] = "Column 3";             data[0, 3] = "Column 4";             data[0, 4] = "Column 5";              // Load the data.             for (int row = 1; row < 100; row++)             {                 for (int col = 0; col < 5; col++)                 {                     data[row, col] = "STUFF";                 }             }              // Save all data to the worksheet.             aRange.set_Value(m, data);             // Atuo size columns             // TODO: Add Code to auto size columns.              // Save the file.             wb.SaveAs("C:\Test.xls", Office.Excel.XlFileFormat.xlExcel8, m, m, m, m, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, m, m, m, m, m);             // Close the file.             wb.Close(false, false, m);         }         catch (Exception) { }         finally         {             // Close the connection.             cmd.Close();             // Close Excel.             excelApp.Quit();         }     } } 
like image 280
norlando Avatar asked May 21 '10 17:05

norlando


People also ask

What method do we use to automatically resize columns in Excel?

On the Home tab, in the Cells group, click Format. Under Cell Size, click AutoFit Column Width. Note: To quickly autofit all columns on the worksheet, click the Select All button, and then double-click any boundary between two column headings.

What method do we use to automatically resize columns?

Simply select the columns you want to AutoFit, then select the AutoFit Column Width command from the Format drop-down menu on the Home tab. This method can also be used for row height.

How do you auto size columns in Excel VBA?

In VBA, you can use the AutoFit method to auto fit rows, columns, and even an entire worksheet. You need to specify the range, and then you can use the AutoFit method.


2 Answers

Add this at your TODO point:

aRange.Columns.AutoFit();

like image 199
Igby Largeman Avatar answered Sep 20 '22 05:09

Igby Largeman


This might be too late but if you add

 worksheet.Columns.AutoFit(); 

or

 worksheet.Rows.AutoFit(); 

it also works.

like image 28
Matheus Shita Avatar answered Sep 23 '22 05:09

Matheus Shita