C# Excel.\u工作表.Columns.Autofit()方法挂起

C# Excel.\u工作表.Columns.Autofit()方法挂起,c#,excel,C#,Excel,我的sqldatareader正在返回约200行数据。其中大约一半的行有一个包含整个xml文档的列。我假设此列导致autofit()方法挂起。我如何才能让它抛出异常或成功完成。我不管是哪一个,我只需要这个[自动]程序来完成 Excel.Application xl = null; Excel._Workbook wb; Excel._Worksheet ws; try {

我的sqldatareader正在返回约200行数据。其中大约一半的行有一个包含整个xml文档的列。我假设此列导致autofit()方法挂起。我如何才能让它抛出异常或成功完成。我不管是哪一个,我只需要这个[自动]程序来完成

Excel.Application xl = null;
            Excel._Workbook wb;
            Excel._Worksheet ws;

            try
            {
                xl = new Microsoft.Office.Interop.Excel.Application();
                xl.Visible = false;

                while (reader.HasRows && !done)
                {
                    wb = (Excel._Workbook)(xl.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet));
                    ws = (Excel._Worksheet)wb.ActiveSheet;

                    // Insert column headers
                    for (int counter = 0; counter < reader.FieldCount; counter++)
                        ws.Cells[1, counter + 1] = reader.GetName(counter);

                    // Write the data to the excel file
                    while (reader.Read())
                    {
                        for (int counter = 1; counter <= reader.FieldCount; counter++)
                        {
                            // Need to format this column so excel doesn't change how it looks
                            if (report.ReportName == "RPTAAMVANetBatch" && reader.GetName(counter - 1) == "CorrelationID")
                                ws.Cells[rowCounter, counter] = String.Format("''{0}'", reader.GetValue(counter - 1));
                            else
                                ws.Cells[rowCounter, counter] = reader.GetValue(counter - 1);
                        }
                        rowCounter++;
                    }

                    RecordsProcessed = rowCounter - 1;

                    // Format the excel file to liking
                    ws.get_Range(ws.Cells[1, 1], ws.Cells[rowCounter - 1, reader.FieldCount]);
                    ws.Columns.AutoFit();
Excel.Application xl=null;
Excel.\u工作簿wb;
Excel.\u工作表ws;
尝试
{
xl=新的Microsoft.Office.Interop.Excel.Application();
xl.可见=假;
while(reader.HasRows&&!完成)
{
wb=(Excel.\u工作簿)(xl.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet));
ws=(Excel.\u工作表)wb.ActiveSheet;
//插入列标题
用于(int计数器=0;计数器对于(int counter=1;counter,除了我上面的评论之外,我正在使用我几天前创建的一个示例文件,其中有100行,在
Col E
中,我有几封电子邮件的整个正文(我放了一个红色框以保护电子邮件的身份和内容)

这是一个简单的VBA代码,我运行它来检查自动调整
E
列所需的时间

Sub Test()
    Dim startTime As String
    Dim endTime As String

    startTime = Now

    Columns("E:E").EntireColumn.AutoFit

    endTime = Now

    Debug.Print "The process started at " & startTime & " and got over at " & endTime
End Sub
屏幕截图

您的程序需要更多的时间,因为有200行,而且每个Excel单元格中的数据可能比我的要多

那么解决方案是什么?

我能想到的最好的解决方案是,在向列中写入大量数据之前,将列的范围扩大到(254.86)

        Excel.Range Rng = ws.get_Range("E:E",System.Type.Missing);

        Rng.EntireColumn.ColumnWidth = 254;

注意:不要对该列或整个工作表使用自动拟合。如果确实需要使用
自动拟合
,请将其分解。例如,在我的情况下,我会从第1列到第4列,然后从第6列到最后一列进行自动拟合

如何显示一些代码?您是否通过C#的互操作使用Excel?不完整,但重新发布nt代码是从方法中添加的。您应该在这里进行更多的谷歌搜索。这里是一篇类似的文章,看起来您缺少范围内的内容。Columns.AutoFit这里是不是可以从工作表中删除该列?@rbaryyoung是的,当该列不存在或为空时,一切正常