Exemplo n.º 1
0
        private void btnDeidentify_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtDataSource.Text))
            {
                MessageBox.Show("The field Data Source must not be empty...");
                txtDataSource.Focus();
                return;
            }
            AccessUtils accessUtil = new AccessUtils(txtDataSource.Text);

            InsertUtils insertUtils = new InsertUtils();

               System.Data.DataTable   dt = insertUtils.deidenfyDataBase(accessUtil.getConexao,accessUtil.getOtherConexao);

               Excel.Application oXL;
               Excel.Workbook oWB;
               Excel.Worksheet oSheet;
               Excel.Range oRange;

               // Start Excel and get Application object.
               oXL = new Excel.ApplicationClass();

               // Set some properties
               oXL.Visible = true;
               oXL.UserControl = true;

            System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

               oXL.DisplayAlerts = false;

               // Get a new workbook.

               oWB = oXL.Workbooks.Add(Missing.Value);

               //System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

               // Get the active sheet
               oSheet = (Excel.Worksheet)oWB.ActiveSheet;
               oSheet.Name = "NIDS";

               // Process the DataTable
               // BE SURE TO CHANGE THIS LINE TO USE *YOUR* DATATABLE
               //DataTable dt = Customers.RetrieveAsDataTable();

               int rowCount = 1;

               foreach (DataRow dr in dt.Rows)
               {
               rowCount += 1;
               for (int i = 1; i < dt.Columns.Count + 1; i++)
               {
                   // Add the header the first time through
                   if (rowCount == 2)
                   {
                       oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
                   }
                   oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
               }
               }

               // Resize the columns
               oRange = oSheet.get_Range(oSheet.Cells[1, 1],
                         oSheet.Cells[rowCount, dt.Columns.Count]);
               oRange.EntireColumn.AutoFit();

               // Save the sheet and close
               oSheet = null;
               oRange = null;
               oWB.SaveAs("Deidentify_"+cboDistrito.Text+".xls", Excel.XlFileFormat.xlWorkbookNormal,
               Missing.Value, Missing.Value, Missing.Value, Missing.Value,
               Excel.XlSaveAsAccessMode.xlExclusive,
               Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value);
               oWB.Close(Missing.Value, Missing.Value, Missing.Value);
               oWB = null;
               oXL.Quit();

               // Clean up
               // NOTE: When in release mode, this does the trick
               GC.WaitForPendingFinalizers();
               GC.Collect();
               GC.WaitForPendingFinalizers();
               GC.Collect();
               System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
            MessageBox.Show("Completed");
        }