|
楼主 |
发表于 2015-11-21 22:44
|
显示全部楼层
三、C#中,为什么用foreach (FileInfo file in dir.GetFiles("*.xlsx", SearchOption.AllDirectories))没反应?
以下两段代码分别为批量判断工作表是否存在保护及是否存在公式的。可一用foreach (FileInfo file indir.GetFiles(...))循环,程序则没了反应。一旦注释掉揗环代码,程序会有反应,但只能显示指定工作薄中所有工作表是否被保护或存在公式。而无法显示指定路径中所有工作薄的所有工作表是否被保护或存在公式。不知为何?
- private void mb2_Click(object sender, RibbonControlEventArgs e)
- {
- //fn判断工作表是否被保护(Pa.SuperTip.Substring(0, Pa.SuperTip.LastIndexOf("\") + 1));
- fn判断工作表是否被保护(Pa.SuperTip);
- }
- private static void fn判断工作表是否被保护(string v_strDir)
- {
- String msg = "";
- Application app = new Application();
- //DirectoryInfo dir = new DirectoryInfo(v_strDir);
- try
- {
- //递归查找所有Excel 2007/2010的文件
- //foreach (FileInfo f in dir.GetFiles("*.xlsx", SearchOption.AllDirectories))
- //{
- //Workbook book = app.Workbooks.Open(f.FullName);
- Workbook book = app.Workbooks.Open(v_strDir);
- msg = book.Name + "受保护的工作表有:\n";
- foreach (Worksheet sheet in book.Worksheets)
- {
- if (book.Worksheets[sheet.Name].ProtectContents==true)
- {
- msg = msg + sheet.Name + "\n";
- }
- }
- app.Application.DisplayAlerts = false;
- book.Close();
- //}
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- app.Quit();
- System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
- app = null;
- GC.Collect();
- }
- System.Windows.Forms.MessageBox.Show(msg);
- }
- private void mb3_Click(object sender, RibbonControlEventArgs e)
- {
- //fn判断单元格是否包含公式(Pa.SuperTip.Substring(0, Pa.SuperTip.LastIndexOf("\") + 1));
- fn判断单元格是否包含公式(Pa.SuperTip);
- }
- private static void fn判断单元格是否包含公式(string v_strPath)
- {
- //StringBuilder sb = new StringBuilder();
- Application app = new Application();
- //DirectoryInfo dir = new DirectoryInfo(v_strPath);
- string msg = "";
- try
- {
- //foreach (FileInfo file in dir.GetFiles("*.xlsx", SearchOption.AllDirectories))
- //{
- //Workbook book = app.Workbooks.Open(file.FullName);
- Workbook book = app.Workbooks.Open(v_strPath);
- msg = book.Name + "含有公式的单元格:\n";
- foreach (Worksheet sheet in book.Worksheets)
- {
- msg = msg + "\n" + sheet.Name + ": \n";
- foreach (Range cel in sheet.UsedRange)
- {
- string src = cel.Value == null ? string.Empty : cel.Value.ToString();
- //string dest = destsheet.Cells[cel.Row, cel.Column].Value == null ? string.Empty : destsheet.Cells[cel.Row, cel.Column].Value.ToString();
- if (cel.HasFormula==true)
- {
- msg = msg + cel.Address + "→" + cel.Value + " ";
- }
- }
- //for (int i = 1; i < sheet.UsedRange.Rows.Count; i++)
- //{
- //for (int j = 1; j < sheet.UsedRange.Columns.Count; j++)
- //{
- //if (sheet.Cells[i, j].Interior.Color != ColorTranslator.ToOle(Color.FromArgb(255, 204, 153)))
- //{
- //if (sheet.Cells[i, j].HasFormula==true)
- //{
- //msg=msg+book.Name + " " + sheet.Name + " 第" + i.ToString() + "行第" + j.ToString() + "列包含公式!\n";
- //}
- //}
- //}
- //}
- }
- book.Close();
- //}
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- //app.Quit();
- System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
- app = null;
- GC.Collect();
- }
- System.Windows.Forms.MessageBox.Show(msg);
- }
复制代码
|
|