1

我正在研究如何使用 C# Excel Interop 识别 excel 中的哪个范围正在激活。

我正在使用 office interop 将粘贴表从 word 复制到 excel。粘贴后,我注意到某些范围正在选择/激活(就像您在 excel 中手动复制粘贴一样)。在转到下一个粘贴之前,我想在该范围内进行一些进一步的处理。

我用谷歌搜索了很多,但没有相关的答案。他们总是展示如何激活一个范围......

这里有人知道我该怎么做吗?

太感谢了。

此致,

    using Word = Microsoft.Office.Interop.Word;
    using Excel = Microsoft.Office.Interop.Excel;

    private void CopyTableFromWordToExcel()
    {
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(Missing.Value);
        var vungLamViec = xlWorkBook.Worksheets[1];

        object miss = Missing.Value;
        object path = _fileWord;
        object readOnly = true;

        var wordApp = new Word.Application();
        wordApp.Visible = false;

        Word.Document wordDoc = wordApp.Documents.Open(ref path, ref miss, ref readOnly,
                                       ref miss, ref miss, ref miss, ref miss,
                                       ref miss, ref miss, ref miss, ref miss,
                                       ref miss, ref miss, ref miss, ref miss,
                                       ref miss);
        Excel.Range last = null;
        int lastUsedRow = 0; ;
        int lastUsedColumn = 0;

        foreach (Word.Table table in wordDoc.Tables)
        {
            last = vungLamViec.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
            lastUsedRow = last.Row;
            lastUsedColumn = last.Column;

            vungLamViec.Range["A" + (lastUsedRow + 2).ToString()].EntireRow.Interior.Color = System.Drawing.Color.Red;

            Word.Range rng = table.Range;
            rng.Copy();

            vungLamViec.Range["A" + (lastUsedRow + 3).ToString()].Activate();
            
            //there will be a range which is selected / activated by Interop after paste
            vungLamViec.Paste();
        }

        last = vungLamViec.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
        lastUsedRow = last.Row;
        lastUsedColumn = last.Column;

        last = vungLamViec.Range(vungLamViec.Cells[1, 1], vungLamViec.Cells[lastUsedRow, lastUsedColumn]);
        foreach (Excel.Range cell in last)
        {
            if (cell.MergeCells)
            {
                cell.MergeCells = false;
                cell.Style.WrapText = true;
            }
        }

        last.EntireColumn.AutoFit();
        last.EntireRow.AutoFit();

        xlWorkBook.SaveAs(@"C:\Users\Desktop\a.xlsx", Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value,
                                Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
                                Excel.XlSaveConflictResolution.xlUserResolution, true,
                                Missing.Value, Missing.Value, Missing.Value);

        xlWorkBook.Close();
        xlApp.Quit();
        ExtensionUtility.releaseObject(xlApp);

        wordDoc.Close();
        wordApp.Quit();
        ExtensionUtility.releaseObject(wordApp);

        ExtensionUtility.ShowMessageBox("Done", "Done");
    }
4

0 回答 0