how can i export the data from gridview to excel in windows forms? [Resolved]

Posted by Harikrishna.veluru under C# on 12/21/2011 | Points: 10 | Views : 8837 | Status : [Member] | Replies : 2
Hi,

how can i export the data from gridview to excel in windows forms?please do the needful.here is my requirment.in excel sheet i want to display the column names.how can i do this.please do the needful.here i have to display tbl_report1 columns in gridview also

best,
hari



Responses

Posted by: Sudheep.grandhe on: 12/21/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,

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[i].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");
}

Best,
Sudheep.

Harikrishna.veluru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Daisywhite on: 2/24/2012 [Member] Starter | Points: 25

Up
0
Down
Sorry for that .I suggest you to go to th w3school for help. Hope everything will come well soon. And you may download a EAN 128/ GS1 128 Barcode Add-In for Excel to make your own barcode so as to make sure your excel is ok again.http://www.keepautomation.com/products/excel_barcode/barcodes/ean_128.html

Harikrishna.veluru, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response