Sunday, April 7, 2013

// // Leave a Comment

dataset to excel c#

here is the solution for that.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Office.Interop;
private void btn_ETexcel_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);
ExcelApp.Columns.ColumnWidth = 10;
for (int i = 1; i < GridView1.Columns.Count + 1; i++)
{
ExcelApp.Cells[1, i] = GridView1.Columns[i - 1].HeaderText;
ExcelApp.Cells.Font.Bold = true;
}
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < GridView1.Columns.Count; j++)
{
ExcelApp.Cells[i + 2, j + 1] = GridView1.Rows.Cells[j].Value.ToString();
}
}
ExcelApp.ActiveWorkbook.SaveCopyAs ("D:\\reports.xls");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Excel file created,you can find the file D:\\reports.xls");
}

0 comments:

Post a Comment