c# - C# Excel Word Interop - 识别正在激活的范围
问题描述
我正在研究如何使用 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");
}
解决方案
推荐阅读
- node.js - AWS Lambda - 无法访问捆绑在层 (Node.js) 中的 JS 文件
- angular - 在 flatMap 中访问响应标头角度 7
- python-3.x - 如何使用来自 Airflow OracleOperator 的多个语句执行 sql 脚本
- javascript - 为什么数组实际上是排序的,但在 HTML 上看起来不同?
- java - Intellij IDEA - 为特征文件提供 2 种类型的步骤片段
- python - 无法使用 category_encoders 的 fit_transform 对我的数据进行序数编码
- javascript - Vue多选不添加新标签
- r - 如何忽略缺失值以在数据框中进行进一步计算
- javascript - 从 WKWebView 获取 JSON 文本
- javascript - 如何将变量值放在进度条中?