excel的流程仍然在运行 [英] Process of excel still running

查看:83
本文介绍了excel的流程仍然在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button6_Click(object sender, EventArgs e) // export to excel
      {

          int exportcount = 0;
          //SaveFileDialog sfd = new SaveFileDialog();
              //sfd.Filter = "Excel Documents (*.xlsx)|*.xlsx";
              //sfd.FileName = "Sheet1.xlsx";
              //if (sfd.ShowDialog() == DialogResult.OK)
              //{
              //    ToCsV(dataGridView1, @"c:\export.xls");
              //    ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is your grid view name
              //    MessageBox.Show("Excel Exported!");
              Excel.Application app = new Excel.Application();
              //Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
              Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
              Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null;
              //Microsoft.Office.Interop.Excel._Worksheet worksheet2 = null;
              try
              {
                  app.Visible = true;
                  worksheet1 = workbook.Sheets["Sheet1"];
                  worksheet1 = workbook.ActiveSheet;
                  Marshal.ReleaseComObject(worksheet1);
                  Marshal.ReleaseComObject(workbook);


                  worksheet1.Cells[1, 1] = "รางงานการคำนวน การจัดการโครงการโดยการใช้ Critical Path Method";
                  Excel.Range Chartrange;
                  Chartrange = worksheet1.get_Range("A1", "C1");
                  Chartrange.Font.Bold = true;
                  worksheet1.Cells[3, 1] = "ต้นทุนการทำงานต่อวันเท่ากับ " + Check + " บาท";
                  GC.Collect();
                  Marshal.ReleaseComObject(Chartrange);

                  for (int i = 1; i < 4 + 1; i++)
                  {
                      worksheet1.Cells[5, i] = dataGridView1.Columns[i - 1].HeaderText;
                  }
                  for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                  {
                      for (int j = 0; j < 4; j++)
                      {
                          int excel_cell = dataGridView1.Rows.Count - 1 + 5;
                          if (dataGridView1.Rows[i].Cells[j].Value != null)
                          {
                              worksheet1.Cells[i + 6, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                              Excel.Range _range;
                              _range = worksheet1.get_Range("A5", "D" + excel_cell);
                              Excel.Borders borders = _range.Borders;
                              borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                              borders.Weight = 3d;
                              GC.Collect();
                              Marshal.ReleaseComObject(borders);
                              Marshal.ReleaseComObject(_range);
                          }
                          else
                          {
                              worksheet1.Cells[i + 2, j + 1] = "";
                          }
                      }
                      exportcount = dataGridView1.Rows.Count + 2;
                  }
                  int report_count1 = Report.Count;
                  //worksheet1.Cells[exportcount + 3, 1] = "ตารางแสดงกิจกกรรมและค่าใช้จ่าย ที่เร่งรัด ใช้เวลาทั้งหมด " + report_count1 + " สัปดาห์";
                  worksheet1.Cells[exportcount + 4, 1] = "เร่งรัดครั้งที่ ";
                  worksheet1.Cells[exportcount + 4, 2] = "กิจกรรมที่เร่งรัด";
                  worksheet1.Cells[exportcount + 4, 3] = "Crtical Path";
                  worksheet1.Cells[exportcount + 4, 4] = "ต้นทุนเร่งรัด";
                  worksheet1.Cells[exportcount + 4, 5] = "ลดค่าใช้จ่ายไป";
                  Excel.Range range;
                  int DL = exportcount + 4;
                  range = worksheet1.get_Range("A" + DL, "E" + DL);
                  Excel.Borders DK = range.Borders;
                  DK.LineStyle = Excel.XlLineStyle.xlContinuous;
                  DK.Weight = 3d;
                  //Excel.Range kk = worksheet1.get_Range("A" + k, "E" + k);
                  //kk.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignGeneral;
                  //Excel.Borders borders = kk.Borders;
                  //borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                  //borders.Weight = 3d;
                  GC.Collect();
                  Marshal.ReleaseComObject(range);
                  Marshal.ReleaseComObject(DK);
                  GC.Collect();
                  GC.WaitForPendingFinalizers();


                  string tempreport;
                  string Proj_NameReport = "";
                  for (int i = 0; i < TCritical_ForReport2.Count; i++)
                  {
                      for (int ii = 0; ii < TCritical_ForReport2[i].Count; ii++)
                      {
                          Proj_NameReport = Proj_NameReport + TCritical_ForReport2[i][ii];

                      }
                      tempreport = Proj_NameReport;
                      namereport.Add(tempreport);
                      Proj_NameReport = "";
                  }
                  for (int i = 0; i < namereport.Count; i++)
                  {
                      //int kk = forlast + 6;
                      namereport[i] = namereport[i].Trim();
                      namereport[i] = namereport[i].Replace("\r\n", "\r\n");
                      namereport[i] = namereport[i].Replace("  ", " ");
                  }

                  int k = 0;
                  if (Report.Count != 0)
                  {
                      int forlast = 0;
                      for (int i = 0; i < Report.Count; i++)
                      {
                          int day = i + 1;
                          int excelrow = i + 1;
                          string excelname = "";
                          int CCost = 0;
                          for (int ii = 0; ii < Report[i].Count; ii++)
                          {
                              excelname = excelname + " " + Report[i][ii].PName;
                              CCost = CCost + Report[i][ii].Cost;
                          }
                          int excel_cell = dataGridView1.Columns.Count - 2;
                          k = exportcount + excelrow + 4;
                          int CostOFProject = Convert.ToInt32(Check);
                          int result = CostOFProject - CCost;
                          worksheet1.Cells[exportcount + excelrow + 4, 1] = day;
                          worksheet1.Cells[exportcount + excelrow + 4, 2] = excelname;
                          worksheet1.Cells[exportcount + excelrow + 4, 3] = namereport[i];
                          worksheet1.Cells[exportcount + excelrow + 4, 4] = CCost;
                          worksheet1.Cells[exportcount + excelrow + 4, 5] = CostOFProject + "-" + CCost + " = " + result + " บาท";
                          Excel.Range kk = worksheet1.get_Range("A" + k, "E" + k);
                          kk.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignGeneral;
                          kk.Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                          Excel.Borders borders = kk.Borders;
                          borders.LineStyle = Excel.XlLineStyle.xlContinuous;
                          borders.Weight = 3d;
                          excelrow = excelrow + 1;
                          GC.Collect();
                          Marshal.ReleaseComObject(kk);
                          Marshal.ReleaseComObject(borders);
                          GC.Collect();
                          GC.WaitForPendingFinalizers();
                          forlast = exportcount + excelrow + 3;
                      }
                      int show_report_Cost = 0;
                      for (int i = 0; i < Report.Count; i++)
                      {
                          for (int ii = 0; ii < Report[i].Count; ii++)
                          {
                              show_report_Cost = show_report_Cost + Report[i][ii].Cost;
                          }
                      }
                      int remain_report = 0;
                      int Report_ForCost = 0;
                      Report_ForCost = Convert.ToInt32(Check) * report_count1;
                      remain_report = keep_MaxTime - report_count1;
                      int calculateCost_report = 0;
                      calculateCost_report = Report_ForCost - show_report_Cost;
                      worksheet1.Cells[forlast + 2, 1] = "เวลาที่ใช้ก่อนที่่จะเร่งรัดกิจกรรมคือ " + keep_MaxTime + " สัปดาห์ หลังจากเร่งรัดกิจกกรมไปจำนวน " + report_count1 + " สัปดาห์";
                      worksheet1.Cells[forlast + 3, 1] = "จะทำให้โครงการลดระยะเวลาในการทำงานเหลือ " + remain_report + " สัปดาห์";
                      worksheet1.Cells[forlast + 4, 1] = "และสามารถลดค่าใช้จ่ายได้ " + calculateCost_report + " บาท";
                      int forlast2 = forlast + 6;


                  }
                  //Excel.Range kk = worksheet1.get_Range(k, 5);
                  //kk.Cells.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignGeneral;
                  Excel.Range er = worksheet1.get_Range("A:Z", System.Type.Missing);
                  er.EntireColumn.ColumnWidth = 17.0;
                  //workbook.Close();
                  //app.Quit();
                  //GC.Collect();
                  //GC.WaitForPendingFinalizers();
                  //Marshal.ReleaseComObject(er);
                  //Marshal.ReleaseComObject(workbook);
                  //Marshal.ReleaseComObject(worksheet1);
                  //app = null;
                  //GC.Collect();
                  //GC.WaitForPendingFinalizers();


              }


              catch { }
              finally
              {
                  Marshal.ReleaseComObject(workbook);
                  Marshal.ReleaseComObject(worksheet1);
                  if (app != null)
                  {
                      app.Quit();
                  }
                  Marshal.ReleaseComObject(app);
              }



      }//expoert



i真的没有任何线索可以做到这一点我尝试了很多我在谷歌找到的东西

但问题是解决任何人帮助我请项目需要在下周三发送给我的老师


i really have no clue to do this i try many of thing i found in google
but the issue ism't solve anyone Help me Please the project need to be send to my teacher on next wednesday

推荐答案

我明白你在运行代码而你正在获得错误说excel进程仍在运行。如果是这种情况,那么您可以在任务管理器中看到您的进程仍在后台运行。所以我建议安全地处理excel对象,为此你可以尝试在C#中使用Using block或者在finally块中销毁所有相关对象。



尝试首先释放所有对象,然后使用它,在finally块或使用块中销毁你的对象。

看到这个链接



finally块中的另一件事首先发布工作表对象然后再工作簿宾语。它可以解决问题,因为我可以看到你已经释放所有对象
What I understood is you are running the code and you are getting the error saying the excel process is still running. If this is the case then you can see in the task manager your process is still in the background running. so I would suggest that safely dispose the excel objects and for that you can try to use Using block in C# or in finally block destroy all the relative objects.

try to release all the object in first go and then use it and in finally block or in using block destroy your objects.
see this link

One more thing in finally block first release the sheet object and then workbook object. it may solve the issue as i can see that you already releases all the objects


我也遇到了这个问题。我关闭和释放对象的顺序似乎解决了它。将其修改为您的变量名称和对象。请注意我从工作表发布开始,然后是工作簿,然后是工作簿集合,最后是应用程序。如果您有一个未释放的COM对象,则excel进程将保持打开状态。得爱互操作。对于下面的代码,我在每个异常句柄中调用ReleaseObjects()函数(如果出现错误)并且我的进程完成了。



I had this problem too. The order which I closed and released objects seemed to fix it. Modify this to your variable names and objects. Notice I start with the worksheet release, then workbook, then workbooks collection then finally the app. If you have even one COM object that you don't release, the excel process will stay open. Gotta love interop. For the code below I put a call to the ReleaseObjects() function in every exception handle(in case of error) and when my process is done.

public void ReleaseObjects()
        {
            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(_currentSheet) > 0) { }
            _currentSheet = null;
            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelWorkBook) > 0) { }
            _excelWorkBook = null;
            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelWorkBooks) > 0) { }
            _excelWorkBooks = null;
            GarbageCollect();
            _excelApp.Quit();
            while (System.Runtime.InteropServices.Marshal.ReleaseComObject(_excelApp) > 0) { }
            _excelApp = null;
            GarbageCollect();
        }

        public static void GarbageCollect()
        {
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();

        }


这篇关于excel的流程仍然在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆