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

前端 未结 5 1333
我寻月下人不归
我寻月下人不归 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);
            }
        }
    

提交回复
热议问题