如何通过Excel互操作的对象做我自动调整大小列? [英] How do I auto size columns through the Excel interop objects?

查看:143
本文介绍了如何通过Excel互操作的对象做我自动调整大小列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code我使用将数据加载到Excel工作表,但我看汽车大小的列中的数据加载后。有谁知道最好的方式来自动调整大小的列?

 使用Microsoft.Office.Interop;公共类ExportReport
{
    公共无效导出()
    {
        Excel.Application excelApp =新Microsoft.Office.Interop.Excel.Application();
        Excel.Workbook WB;
        Excel.Worksheet WS;
        Excel.Range人气指数;
        物体M = Type.Missing;
        字符串[,]的数据;
        字符串的errorMessage =的String.Empty;
        尝试
        {
            如果(excelApp == NULL)
                抛出新的异常(EXCEL无法启动。);            //创建工作簿和工作表。
            WB = excelApp.Workbooks.Add(Office.Excel.XlWBATemplate.xlWBATWorksheet);
            WS =(Office.Excel.Worksheet)wb.Worksheets [1];            如果(WS == NULL)
                抛出新的异常(无法创建工作表);            //设置填补了范围。
            人气指数= ws.get_Range(A1,E100);            如果(人气指数== NULL)
                抛出新的异常(无法得到一个范围。);            //加载列标题。
            数据=新的字符串[100,5];
            数据[0,0] =列1;
            数据[0,1] =第2列;
            数据[0,2] =3列;
            数据[0,3] =第4栏;
            数据[0,4] =第5栏;            //加载数据。
            对于(INT行= 1;行< 100;排++)
            {
                对于(INT COL = 0;西小于5;西++)
                {
                    数据[行,列] =东西;
                }
            }            //所有的数据保存到工作表。
            aRange.set_Value(M,数据);
            // Atuo大小列
            // TODO:添加code到自动调整大小列。            //保存文件。
            wb.SaveAs(C:\\ TEST.XLS,Office.Excel.XlFileFormat.xlExcel8,M,M,M,M,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,男,M,M,M,M );
            //关闭文件。
            wb.Close(假的,假的,M);
        }
        赶上(例外){}
        最后
        {
            //关闭连接。
            cmd.Close();
            //关闭Excel。
            excelApp.Quit();
        }
    }
}


解决方案

在您的TODO点补充一点:

aRange.Columns.AutoFit();

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();
        }
    }
}

解决方案

Add this at your TODO point:

aRange.Columns.AutoFit();

这篇关于如何通过Excel互操作的对象做我自动调整大小列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆