Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void exportToolStripMenuItem_Click(object sender, EventArgs e)
- {
- var appPath = AppDomain.CurrentDomain.BaseDirectory;
- // creating Excel Application
- Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
- // creating new WorkBook within Excel application
- Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
- // creating new Excelsheet in workbook
- Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
- // see the excel sheet behind the program
- app.Visible = false;
- // get the reference of first sheet. By default its name is Sheet1.
- // store its reference to worksheet
- worksheet = workbook.ActiveSheet;
- // changing the name of active sheet
- worksheet.Name = "Test";
- // storing header part in Excel
- for (int i = 1; i < GRID.Columns.Count + 1; i++)
- {
- worksheet.Cells[1, i] = GRID.Columns[i - 1].HeaderText;
- }
- // storing Each row and column value to excel sheet
- for (int i = 0; i < GRID.Rows.Count - 1; i++)
- {
- for (int j = 0; j < GRID.Columns.Count; j++)
- {
- worksheet.Cells[i + 2, j + 1] = GRID.Rows[i].Cells[j].Value.ToString();
- }
- }
- // save the application
- workbook.SaveAs(appPath + "test", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
- false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
- Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
- app.Quit();
- }
- private void importToolStripMenuItem_Click(object sender, EventArgs e)
- {
- var name = "Test";
- var appPath = AppDomain.CurrentDomain.BaseDirectory;
- var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
- appPath + "test.xlsx"+
- ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
- var oDbConnection = new OleDbConnection(connectionString);
- var oDbCommand = new OleDbCommand("Select * From [" + name + "$]", oDbConnection);
- oDbConnection.Open();
- var dataAdapter = new OleDbDataAdapter(oDbCommand);
- var dataTable = new DataTable();
- dataAdapter.Fill(dataTable);
- GRID.DataSource = dataTable.DefaultView;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement