This project has moved and is read-only. For the latest updates, please go here.

Cell & Row AutoFit

Mar 28, 2013 at 12:23 PM
Hello,
I want to implement a AutoFit mechanism in my Excel in order to auto fit my columns are rows.
                    double excelmaxcolumncounter = 128;
                    for (double i = 1; i <= excelmaxcolumncounter; i++)
                    {
                        sheet.Cells.Columns[i].AutoFit();
                    }
                    double excelmaxrowcounter = 1000;
                    for (double i = 1; i <= excelmaxrowcounter; i++)
                    {
                        sheet.Cells.Rows[excelmaxrowcounter].AutoFit();
                    }
The issue is, that it requires LOT of execution time, 128*1.000. Taking in consideration that I might have 10-20 worksheets to auto fit, it will take a day to do that process.

Isn't there any other possibility to return the max used column number and max used row number?
Mar 28, 2013 at 2:25 PM
Nevermind,

Found it out with
                    Excel.Workbook workbook;
                    workbook = xlsApp.Workbooks.Open(myFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                Type.Missing, Type.Missing);
                    Excel.Worksheet workSheet;
                    Excel.Worksheet sheet in workbook.Sheets

                    sheet.Columns.AutoFit();
                    sheet.Rows.AutoFit()
But, when I write "sheet." it doesn't show me Columns or Rows as properties.
Mar 28, 2013 at 2:46 PM
Type.Missing is not necessary here. did you use the latest source state?
Rows and Colums are marked as NonBrowsable. this is a mistake (my bad).

i see you use some code like this:

Process[] processes = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (Process p in processes)
p.Kill();

this is not necessary in NetOffice.

please check this documentation page and the tutorial projects:
http://netoffice.codeplex.com/wikipage?title=Tec_Documentation_English_Management

Sebastian
Mar 28, 2013 at 4:21 PM
Yes,

At beginning I was getting some Exception errors with
p.Kill();
Had to remove it and put the .Dispose/.Quit into the finally part of my code in order to always close my application, either PPT or XLS.

Thanks for the information.