如何自动关闭Excel窗口?

时间:2013-04-22 13:09:51

标签: c# cell worksheet excel

我想使用C#

打开并更改excel中的单元格

它可以工作,但如果我保存文件我从excel -.-获得一个窗口,每次我必须点击按钮。见图。

这是我的代码:

foreach (string file in Directory.GetFiles(path))
                         {
                             MSExcel.Workbook book1 = null;
                             MSExcel.Worksheet sheet1;
                             MSExcel.Range range_A1;

                             try
                             {
                                 book1 = app.Workbooks.Open(file);
                                 sheet1 = (MSExcel.Worksheet)book1.Worksheets[1];
                                 range_A1 = sheet1.GetRange(cell);

                                 string content = range_A1.Value2.ToString();

                                 if (content == value || content == value2)
                                 {

                                     range_A1.Value2 = string.Empty;
                                     book1.Save(); // here i save the value
                                     Console.WriteLine(count +" - OPEN AND CHANGE - " + file + " SAVE");
                                 }
                                 else
                                 {

                                     Console.WriteLine(count + " - OPEN - " + file + " NOT SAVE");
                                 }

                                 count++;
                             }
                             catch (Exception)
                             {
                                 count++;
                                 Console.WriteLine(count + " - ERROR FILE " + file);
                                 liste.Add(file); 
                             }
                             finally
                             {
                                 book1.Close(false);

                             }

                         }

enter image description here

如何让它自动化?

1 个答案:

答案 0 :(得分:1)

你试过这个:

app.DisplayAlerts = false;

也许还有这两个用于额外隐藏(如果需要):

app.Visible = false;
app.ScreenUpdating = false;