Advertisement
Guest User

Untitled

a guest
May 13th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. private void exportToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             var appPath = AppDomain.CurrentDomain.BaseDirectory;
  4.             // creating Excel Application  
  5.             Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
  6.             // creating new WorkBook within Excel application  
  7.             Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
  8.             // creating new Excelsheet in workbook  
  9.             Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
  10.             // see the excel sheet behind the program  
  11.             app.Visible = false;
  12.             // get the reference of first sheet. By default its name is Sheet1.  
  13.             // store its reference to worksheet  
  14.             worksheet = workbook.ActiveSheet;
  15.             // changing the name of active sheet  
  16.             worksheet.Name = "Test";
  17.             // storing header part in Excel  
  18.             for (int i = 1; i < GRID.Columns.Count + 1; i++)
  19.             {
  20.                 worksheet.Cells[1, i] = GRID.Columns[i - 1].HeaderText;
  21.             }
  22.             // storing Each row and column value to excel sheet  
  23.             for (int i = 0; i < GRID.Rows.Count - 1; i++)
  24.             {
  25.                 for (int j = 0; j < GRID.Columns.Count; j++)
  26.                 {
  27.                     worksheet.Cells[i + 2, j + 1] = GRID.Rows[i].Cells[j].Value.ToString();
  28.                 }
  29.             }
  30.             // save the application  
  31.            
  32.             workbook.SaveAs(appPath + "test", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
  33.             false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
  34.             Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  35.             app.Quit();
  36.            
  37.         }
  38.  
  39.         private void importToolStripMenuItem_Click(object sender, EventArgs e)
  40.         {
  41.             var name = "Test";
  42.             var appPath = AppDomain.CurrentDomain.BaseDirectory;
  43.             var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
  44.                             appPath + "test.xlsx"+
  45.                             ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
  46.  
  47.             var oDbConnection = new OleDbConnection(connectionString);
  48.             var oDbCommand = new OleDbCommand("Select * From [" + name + "$]", oDbConnection);
  49.             oDbConnection.Open();
  50.  
  51.             var dataAdapter = new OleDbDataAdapter(oDbCommand);
  52.             var dataTable = new DataTable();
  53.             dataAdapter.Fill(dataTable);
  54.             GRID.DataSource = dataTable.DefaultView;
  55.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement