VSTO C# Excel 工作簿所有工作表
VSTO C# Excel workbook all worksheets
我正在使用 C# VSTO 对目录中的所有 Excel 文件进行文本替换。目前,我的代码有效,但仅适用于每个 Excel 文件的活动工作sheet(第一个工作sheet)。我如何对 Excel 文件中的所有现有工作 sheet 执行此操作:
public void runFiles(string _path)
{
string path = _path;
object m = Type.Missing;
var xlApp = new Microsoft.Office.Interop.Excel.Application();
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] listOfFiles_1 = d.GetFiles("*.xlsx*").ToArray();
FileInfo[] listOfFiles_2 = d.GetFiles("*.xls*").ToArray();
FileInfo[] listOfFiles = listOfFiles_1.Concat(listOfFiles_2).ToArray();
xlApp.DisplayAlerts = false;
foreach (FileInfo file in listOfFiles)
{
var xlWorkBook = xlApp.Workbooks.Open(file.FullName);
Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets;
// get the used range.
Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;
// call the replace method to replace instances.
bool success = (bool)r.Replace(
"Engineer",
"Designer",
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
true, m, m, m);
xlWorkBook.Save();
xlWorkBook.Close();
}
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
xlWorkBook.ActiveSheet
只抢唯一的第一个sheet。我试过 xlWorkBook.Worksheets
,但出现 Cannot implicitly convert type 'Microsoft.Office.Interop.Excel.Sheets' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?)
错误
Worksheets
属性 是工作表的集合。所以你只需要遍历它。
foreach (Excel.Worksheet xlWorkSheet in xlWorkBook.Worksheets)
{
// get the used range.
Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;
// call the replace method to replace instances.
bool success = (bool)r.Replace(
"Engineer",
"Designer",
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
true, m, m, m);
}
Worksheets
方法 returns 工作表数组。我必须通过这样做来编辑数组中的每个元素
foreach (Excel.Worksheet xlWorkSheet in xlWorkBook.Worksheets)
{
// get the used range.
Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;
// call the replace method to replace instances.
bool success = (bool)r.Replace(
"Engineer",
"Designer",
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
true, m, m, m);
}
xlWorkBook.Save();
xlWorkBook.Close();
我正在使用 C# VSTO 对目录中的所有 Excel 文件进行文本替换。目前,我的代码有效,但仅适用于每个 Excel 文件的活动工作sheet(第一个工作sheet)。我如何对 Excel 文件中的所有现有工作 sheet 执行此操作:
public void runFiles(string _path)
{
string path = _path;
object m = Type.Missing;
var xlApp = new Microsoft.Office.Interop.Excel.Application();
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] listOfFiles_1 = d.GetFiles("*.xlsx*").ToArray();
FileInfo[] listOfFiles_2 = d.GetFiles("*.xls*").ToArray();
FileInfo[] listOfFiles = listOfFiles_1.Concat(listOfFiles_2).ToArray();
xlApp.DisplayAlerts = false;
foreach (FileInfo file in listOfFiles)
{
var xlWorkBook = xlApp.Workbooks.Open(file.FullName);
Excel.Worksheet xlWorkSheet = xlWorkBook.Worksheets;
// get the used range.
Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;
// call the replace method to replace instances.
bool success = (bool)r.Replace(
"Engineer",
"Designer",
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
true, m, m, m);
xlWorkBook.Save();
xlWorkBook.Close();
}
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
xlWorkBook.ActiveSheet
只抢唯一的第一个sheet。我试过 xlWorkBook.Worksheets
,但出现 Cannot implicitly convert type 'Microsoft.Office.Interop.Excel.Sheets' to 'Microsoft.Office.Interop.Excel.Worksheet'. An explicit conversion exists (are you missing a cast?)
Worksheets
属性 是工作表的集合。所以你只需要遍历它。
foreach (Excel.Worksheet xlWorkSheet in xlWorkBook.Worksheets)
{
// get the used range.
Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;
// call the replace method to replace instances.
bool success = (bool)r.Replace(
"Engineer",
"Designer",
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
true, m, m, m);
}
Worksheets
方法 returns 工作表数组。我必须通过这样做来编辑数组中的每个元素
foreach (Excel.Worksheet xlWorkSheet in xlWorkBook.Worksheets)
{
// get the used range.
Excel.Range r = (Excel.Range)xlWorkSheet.UsedRange;
// call the replace method to replace instances.
bool success = (bool)r.Replace(
"Engineer",
"Designer",
Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows,
true, m, m, m);
}
xlWorkBook.Save();
xlWorkBook.Close();