Guest User

CPC

a guest
Jun 6th, 2018
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using System.Xml;
  13.  
  14. namespace Exportação
  15. {
  16.     public partial class Table : Form
  17.     {
  18.         string servidor;
  19.         string db;
  20.         string username;
  21.         string password;
  22.         string connectionString;
  23.         string nomeTabela;
  24.  
  25.         public Table()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.  
  30.         private void LoadConf()
  31.         {
  32.             XmlDocument FXML = new XmlDocument();
  33.             FXML.Load("conf.xml");
  34.  
  35.             foreach (XmlNode node in FXML.SelectNodes("//Configuracao"))
  36.             {
  37.                 servidor = node.Attributes[0].Value.ToString();
  38.                 db = node.Attributes[1].Value.ToString();
  39.                 username = node.Attributes[2].Value.ToString();
  40.                 password = node.Attributes[3].Value.ToString();
  41.             }
  42.  
  43.         }
  44.  
  45.         private void GetTables()
  46.         {
  47.             connectionString = @"server=" + servidor + ";" + " Initial Catalog="
  48.             + db + "; User ID:" + username + "; Password=" + password;
  49.             // connectionString = "Server=localhost\\SQLEXPRESS;Database=teste;Trusted_Connection=True;";
  50.             connectionString = "Server=localhost\\SQLEXPRESS;Database=Base Dados Demo NAV;Trusted_Connection=True;";
  51.             SqlConnection cnn = new SqlConnection(connectionString);
  52.  
  53.             cnn.Open();
  54.  
  55.             List<string> result = new List<string>();
  56.             SqlCommand cmd = new SqlCommand("SELECT name FROM sys.Tables", cnn);
  57.             SqlDataReader reader = cmd.ExecuteReader();
  58.             while (reader.Read())
  59.                 result.Add(reader["name"].ToString());
  60.  
  61.             int n = 0;
  62.             foreach (string tableName in result)
  63.             {
  64.                 n++;
  65.                 dgvTables.Rows.Add(n,tableName);
  66.             }
  67.            
  68.             cnn.Close();
  69.         }
  70.  
  71.         private void Table_Load(object sender, EventArgs e)
  72.         {
  73.             LoadConf();
  74.             GetTables();
  75.         }
  76.  
  77.         private void button2_Click(object sender, EventArgs e)
  78.         {
  79.             int rowindex = dgvTables.CurrentCell.RowIndex;
  80.             int columnindex = dgvTables.CurrentCell.ColumnIndex;
  81.             nomeTabela = dgvTables.Rows[rowindex].Cells[columnindex].Value.ToString();
  82.  
  83.             SqlConnection cnn = new SqlConnection(connectionString);
  84.                 SqlDataAdapter sda = new SqlDataAdapter("Select * FROM " + nomeTabela,cnn);
  85.                 DataTable dt = new DataTable();
  86.                 sda.Fill(dt);
  87.                 expExcel(dt);
  88.         }
  89.  
  90.             void expExcel(DataTable dt)
  91.             {
  92.                 object MissingValue = System.Reflection.Missing.Value;
  93.                 Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application
  94.                 {
  95.                     Visible = false
  96.                  };
  97.                 Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
  98.                 Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
  99.  
  100.                 for (int i = 0; i < dt.Columns.Count; i++)
  101.                 {
  102.                     ws.Cells[1,i + 1] = dt.Columns[i].ColumnName;
  103.                 }
  104.  
  105.  
  106.                 ws.Name = nomeTabela;
  107.  
  108.                 SFD.ShowDialog();
  109.                 wb.SaveAs(SFD.FileName,Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,MissingValue,MissingValue,MissingValue,MissingValue,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,MissingValue,MissingValue,MissingValue,MissingValue,MissingValue);
  110.                 wb.Close(true, MissingValue, MissingValue);
  111.                 app.Quit();
  112.                
  113.             }
  114.         }
  115.     }
Add Comment
Please, Sign In to add comment