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

前端 未结 5 1313
我寻月下人不归
我寻月下人不归 2020-12-02 14:02

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

相关标签:
5条回答
  • 2020-12-02 14:29

    This method opens already created excel file, Autofit all columns of all sheets based on 3rd Row. As you can see Range is selected From "A3 to K3" in excel.

     public static void AutoFitExcelSheets()
        {
            Microsoft.Office.Interop.Excel.Application _excel = null;
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook = null;
            try
            {
                string ExcelPath = ApplicationData.PATH_EXCEL_FILE;
                _excel = new Microsoft.Office.Interop.Excel.Application();
                _excel.Visible = false;
                object readOnly = false;
                object isVisible = true;
                object missing = System.Reflection.Missing.Value;
    
                excelWorkbook = _excel.Workbooks.Open(ExcelPath,
                       0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
                       true, false, 0, true, false, false);
                Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
                foreach (Microsoft.Office.Interop.Excel.Worksheet currentSheet in excelSheets)
                {
                    string Name = currentSheet.Name;
                    Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(Name);
                    Microsoft.Office.Interop.Excel.Range excelCells =
    (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("A3", "K3");
                    excelCells.Columns.AutoFit();
                }
            }
            catch (Exception ex)
            {
                ProjectLog.AddError("EXCEL ERROR: Can not AutoFit: " + ex.Message);
            }
            finally
            {
                excelWorkbook.Close(true, Type.Missing, Type.Missing);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                releaseObject(excelWorkbook);
                releaseObject(_excel);
            }
        }
    
    0 讨论(0)
  • 2020-12-02 14:36

    Add this at your TODO point:

    aRange.Columns.AutoFit();

    0 讨论(0)
  • 2020-12-02 14:46

    Also there is

    aRange.EntireColumn.AutoFit();
    

    See What is the difference between Range.Columns and Range.EntireColumn.

    0 讨论(0)
  • 2020-12-02 14:49

    This might be too late but if you add

     worksheet.Columns.AutoFit();
    

    or

     worksheet.Rows.AutoFit();
    

    it also works.

    0 讨论(0)
  • 2020-12-02 14:52

    Have a look at this article, it's not an exact match to your problem, but suits it:

    • Craig Murphy - Excel – wordwrap row autosize issue
    0 讨论(0)
提交回复
热议问题