我有从 excel 文件读取和写入的 ac# .net 应用程序。我的搜索比较每个文件中 20 个单元格的数据,因此在 10000 个文件中搜索需要 70 秒,这非常长!任何关于更好解决方案的想法,以使其更快。这是读取功能:
public static void OpenExcel(string fileName, bool visibility, FunctionToExecute fn = null)
{
string addInPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft\\AddIns\\mDF_XLcalendar.xla");
deleg = fn;
app = new Excel.Application();
app.Workbooks.Open(addInPath);
app.Workbooks.Open(fileName);
app.ScreenUpdating = true;
app.DisplayAlerts = true;
app.Visible = visibility;
app.UserControl = true;
app.WindowState = Excel.XlWindowState.xlMaximized;
EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(application_WorkbookBeforeClose);
EventSave_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Open_ExcelApp_WorkbookBeforeSave);
app.WorkbookBeforeClose += EventDel_BeforeBookClose;
app.WorkbookBeforeSave += EventSave_BeforeBookClose;
}
这是进行搜索的代码:
string searchString = ((RichTextBox)searchObject.LstObject[0]).Text.Trim();
bool contain = ExcelFunctions
.RemoveDiacritics(ExcelFunctions.Read(GetSummaryXl, coord))
.ToLower()
.Contains(ExcelFunctions.RemoveDiacritics(searchString).ToLower());
return string.IsNullOrEmpty(searchString) || (!string.IsNullOrEmpty(searchString) && contain);
我做了很多测试,似乎我的读取功能在一个文件中占用了 90% 的搜索时间。