Exemple #1
0
 private void Proc(object LockWatingThread)
 {
     Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
     try
     {
         app.Visible = false;
         Workbook wBook = app.Workbooks.Add(true);
         wp.SetCursorStyle(Cursors.Default);
         ExcelWorkbookCallbackProc(wBook, -1);//用于回调函数执行列标题定义操作
         int percent = 0;
         if (Count == -1)
         {
             Count = 0;
             while (true)
             {
                 percent++;
                 if (percent == 10000)
                 {
                     percent = 0;
                 }
                 wp.SetProcessBar(percent);
                 lock (LockWatingThread)
                 {
                     if (!ExcelWorkbookCallbackProc(wBook, Count++))
                     {
                         break;
                     }
                     wp.SetProcessBar(100);
                     if (Count > 65530)
                     {
                         MessageBox.Show("条目过多,只导出前65530条");
                         break;
                     }
                 }
                 if (wp.HasBeenCancelled())
                 {
                     app.DisplayAlerts = false;
                     app.Quit();
                     return;
                 }
             }
         }
         else
         {
             for (int i = 0; i < Count; i++)
             {
                 percent++;
                 wp.SetProcessBar(percent * 100 / Count);
                 lock (LockWatingThread)
                 {
                     if (!ExcelWorkbookCallbackProc(wBook, i))
                     {
                         break;
                     }
                 }
                 if (wp.HasBeenCancelled())
                 {
                     app.DisplayAlerts = false;
                     app.Quit();
                     return;
                 }
                 Thread.Sleep(50);
             }
         }
         ExcelWorkbookCallbackProc(wBook, int.MaxValue); //全部导出完成,用于回调函数执行整体界面设定
         MessageBox.Show("数据导出完成");
         app.Visible     = true;
         app.WindowState = XlWindowState.xlMaximized;
     }
     catch (System.Exception ex)
     {
         MessageBox.Show("导出Excel出错!错误原因" + ex.Message);
         app.DisplayAlerts = false;
         app.Quit();
         return;
     }
 }