使用C#

时间:2019-02-07 03:18:03

标签: c# excel office-interop excel-2016

我已经使用Excel Interop 12.0编写了C#应用程序,该程序在Excel 2007和Excel 2010上运行良好。但是在Excel 2016上却可以随机运行。它有时会给出Range类失败错误的Activate方法。当我重新运行该程序就可以了。我再次运行它会引发错误。该程序将打开约30个工作簿,并从C#中填充该工作簿。我在许多地方都使用Range.get_ReSize()和Range.Activate()。为什么行为是随机的?感谢您的帮助

xlWb.Activate(); 
xlWsSummary.Activate(); 
Excel.Range ra = xlWsSummary.UsedRange;//append to the last cell in the used range ra.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, System.Type.Missing).Activate(); 

Excel.Range Range; 
string cellToPaste = "A" + (xlApp.ActiveCell.Row + 1).ToString();//go to the next row 
Range = xlWsSummary.get_Range(cellToPaste, System.Type.Missing); 
Range = Range.get_Resize(1, index); 
Range.Value2 = data; 
Range.Font.Size = 8;//set the fontsize ```

1 个答案:

答案 0 :(得分:0)

我不确定这是否正是您要查找的内容,但这是一些示例代码,可通过2个Excel工作簿工作,每个工作簿都有一个包含6个数字的工作表:

         A  B  C
       ----------
    1 |  3  4  5
    2 |  6  7  8

从每个工作簿中复制此数据,并将其附加到摘要工作簿中的数据中,从而创建:

         A  B  C
       ----------
    1 |  3  4  5
    2 |  6  7  8
    3 |  3  4  5
    4 |  6  7  8

最后,在保存摘要工作簿之前,选择数据之后的下一个“ A”列单元格:

string file = @"C:\Users\me\Desktop\Summary.xlsx";
List<string> fileData = new List<string>{@"C:\Users\me\Desktop\Book1.xlsx", 
                                         @"C:\Users\me\Desktop\Book2.xlsx"};      
Excel.Application xlApp = new Excel.Application();

// Open summary workbook and init data workbook and worksheet
Excel.Workbook xlWb = xlApp.Workbooks.Open(file);
Excel.Worksheet xlWsSummary = xlWb.Sheets[1];
Excel.Workbook xlWbData = null;
Excel.Worksheet xlWsData = null;

// Work through your data workbooks
for ( int i = 0; i < fileData.Count; i++ )
{
    // Open data workbook
    xlWbData = xlApp.Workbooks.Open(fileData[i]);
    xlWsData = xlWbData.Sheets[1];

    // Get specific range of data. I didn't use UsedRange in case only a subset of data is required
    string dataStart = "A1";
    string dataEnd = "C2";
    Excel.Range rangeSource = xlWsData.get_Range(dataStart, dataEnd);

    // Determine next available 'A' cell after used range in summary worksheet
    Excel.Range ra = xlWsSummary.UsedRange;
    Excel.Range rangeDest = ra.get_Range("A" + (ra.Rows.Count + 1));

    // Copy data from data workbook to summary workbook
    rangeSource.Copy(rangeDest);

    // Select the range just copied to the summary workbook and format as required
    Excel.Range rangeFormat = rangeDest.Resize[rangeSource.Rows.Count, rangeSource.Columns.Count];
    rangeFormat.Font.Size = 8;//set the fontsize ```
}

// Release data workbook objects and close before doing anything else
Marshal.ReleaseComObject(xlWsData);
xlWbData.Close();
Marshal.ReleaseComObject(xlWbData);

// Select next available cell in A column
xlWsSummary.Activate();
Excel.Range used = xlWsSummary.UsedRange;
Excel.Range next = xlWsSummary.get_Range("A" + (used.Rows.Count + 1));
next.Select();

// Save the file
xlWb.SaveAs(file);

// And, release!
Marshal.ReleaseComObject(xlWsSummary);
xlWb.Close();
Marshal.ReleaseComObject(xlWb);

我可能没有正确处理Excel对象的释放,因此您最好调查一下,Activate()调用会给出歧义性警告,但否则,希望对您有所帮助。