如何解决C#Excel Word Interop-确定哪个范围正在激活
我正在研究如何识别使用C#Excel Interop激活的excel中的范围。
我正在使用Office interop将粘贴表从word复制到excel。粘贴后,我注意到正在选择/激活某个范围(例如您在excel中手动复制粘贴)。我想在移至下一个粘贴之前在该范围内进行一些进一步的处理。
我在Google上搜索了很多,但没有相关的答案。他们总是展示如何激活范围...
这里的任何人都知道我该怎么做?
非常感谢您。
最好的问候
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);
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,false,Excel.XlSaveAsAccessMode.xlNoChange,Excel.XlSaveConflictResolution.xlUserResolution,true,Missing.Value);
xlWorkBook.Close();
xlApp.Quit();
ExtensionUtility.releaSEObject(xlApp);
wordDoc.Close();
wordApp.Quit();
ExtensionUtility.releaSEObject(wordApp);
ExtensionUtility.ShowMessageBox("Done","Done");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。