Beispiel #1
0
      private void button14_Click(object sender, EventArgs e)
      {
          button13_Click(null, null);
          DataView dv = new DataView();

          dv.Table = dsRep.Tables[0];
          if (DgvRep.RowCount == 0)
          {
              return;
          }

          string appProgID = "Excel.Application";
          Type   excelType = Type.GetTypeFromProgID(appProgID);
          object xlapp1    = Activator.CreateInstance(excelType);
          object workbooks = xlapp1.GetType().InvokeMember("Workbooks", System.Reflection.BindingFlags.GetProperty, null, xlapp1, null);

          object[] args = new object[1];
          args[0] = "C:\\cis\\Сервис\\Отчет по занесению смет.xlt";
          //Пробуем открыть книгу
          //Application xlapp = (Application)xlapp1;
          Microsoft.Office.Interop.Excel.Workbook    WrkBk  = (Microsoft.Office.Interop.Excel.Workbook)workbooks.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, workbooks, args);
          Microsoft.Office.Interop.Excel.Worksheet   WrkSht = (Microsoft.Office.Interop.Excel.Worksheet)WrkBk.Sheets["Отчет"];
          Microsoft.Office.Interop.Excel.Application ExlApp;
          ExlApp = (Microsoft.Office.Interop.Excel.Application)xlapp1;

          ExlApp.Visible = true;

          Microsoft.Office.Interop.Excel.Range rngActive = (Microsoft.Office.Interop.Excel.Range)WrkSht.Cells[2, 1];
          rngActive.Select();
          rngActive.get_Offset(-1, 1).Value2 = Login.Text.ToString();
          rngActive.get_Offset(0, 1).Value2  = "Отчет  по занесению смет по ПСС-4 в программу АО. C 22." + (Convert.ToInt16(MM.Text) < 11 ? "0" : "") + (Convert.ToInt16(MM.Text) - 1).ToString() + " - 20." + (Convert.ToInt16(MM.Text) < 10 ? "0" : "") + MM.Text.ToString();
          for (int i = 0; i < 38; i++)
          {
              if (i >= dv.Count)
              {
                  break;
              }
              rngActive.get_Offset(i + 3, 1).Value2 = dv[i][1];
              rngActive.get_Offset(i + 3, 2).Value2 = dv[i][2];
              rngActive.get_Offset(i + 3, 3).Value2 = dv[i][3];
          }

          for (int i = 38; i < 76; i++)
          {
              if (i >= dv.Count)
              {
                  break;
              }
              rngActive.get_Offset(i + 3 - 38, 1 + 4).Value2 = dv[i][1];
              rngActive.get_Offset(i + 3 - 38, 2 + 4).Value2 = dv[i][2];
              rngActive.get_Offset(i + 3 - 38, 3 + 4).Value2 = dv[i][3];
          }


          WrkSht = null; WrkBk = null;     /*ExlApp.Quit();*/
          ExlApp = null; GC.Collect();
      }