Excel的12.0互操作Excel的2016随机行事使用C#

问题描述 投票:0回答:1

我已经使用Excel互操作12.0,关于Excel 2007和Excel 2010中工作得很好写C#应用程序,但它正在随机Excel的2016年它给Range类失败错误的激活方法的时候。当我重新运行该程序就ok了。我再次运行它抛出错误。该程序打开约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 ```
c# excel office-interop excel-2016
1个回答
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对象释放正确,所以你最好看看成和激活()调用给出了一个含糊的警告但除此之外,希望这有助于。

© www.soinside.com 2019 - 2024. All rights reserved.