Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void btnExport_Click(object sender, EventArgs e)
- {
- try
- {
- Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
- excel.Application.Workbooks.Add(true);
- Dataset myDS = new Dataset();
- System.Data.DataTable table = myDS.Tables[0];
- int ColumnIndex = 0;
- foreach (System.Data.DataColumn col in table.Columns)
- {
- ColumnIndex++;
- excel.Cells[1, ColumnIndex] = col.ColumnName;
- }
- int rowIndex = 0;
- foreach (DataRow row in table.Rows)
- {
- rowIndex++;
- ColumnIndex = 0; foreach (DataColumn col in table.Columns)
- {
- ColumnIndex++;
- excel.Cells[rowIndex + 1, ColumnIndex] = row[col.ColumnName];
- }
- }
- excel.Visible = true;
- Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)excel.A ctiveSheet;
- worksheet.Activate();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Export Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement