Click here to Skip to main content
15,894,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
From the Data grid view how to bind the data in to the excel;

output as follows in data grid view

Date Session RK CMK CNN Gs VB JN (Faculty Name)

7/1/2013 1 REO PH2 REO TAS AM VH
7/1/2013 2 PH2 AM TAs AM VH REO
7/1/2013 3 AM TAs REO PH2 VH TAS
8/1/2013 1 PH2 REO TAS VH AM PH2
8/1/2013 2 AM PH2 REO AM VH TAs
8/1/2013 3 TAs AM VH REO VH PH2

i want the report which faculty has taken course on date and session wise ;

for example RK(Faculty Name)

i want output as follows in EXCEL SHEET RK(Faculty Name) from the DATA GRID VIEW


RK(Faculty Name)

Date Session Course
7/1/2013 1 REO
7/1/2013 2 PH2
7/1/2013 3 AM
8/1/2013 1 H2
8/1/2013 2 AM
8/1/2013 3 TAs
Posted

1 solution

Hope this example help you....

using System.ComponentModel;
private void btnGenerateExcel_Clickddd(object sender, EventArgs e)
{
// Start the BackgroundWorker.

backgroundWorker1.RunWorkerAsync();


// 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.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;

// changing the name of active sheet
worksheet.Name = "Inspection Order Detail";



// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText; // Header in Excel Sheet which u want to show
}

// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < 3; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); // in ur case you can give first three colums

}
}

// save the application
workbook.SaveAs("C:\\Users\\xxxxxx\\Desktop\\Temp\\output.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

// Exit from the application
app.Quit();

releaseObject(worksheet);

releaseObject(workbook);
releaseObject(app);

MessageBox.Show("Excel file created , you can find the file C:\\Users\\xxxxxxx\\Desktop\\Temp\\output.xlsx");
}
 
Share this answer
 
Comments
Hemant Singh Rautela 11-Jan-13 5:17am    
Hi,

I found only

Microsoft.Win32
Microsoft.VisualBasic
Microsoft.SqlServer
Microsoft.CSharp
Microsoft.Contracts

for Microsoft.Office.Interop.Excel._Application which namespace requires
Ask Dj 11-Jan-13 5:44am    
You need to add this ( Microsoft.Office.Interop.Excel)reference to your solution you can find it in Add Reference-->.Net Tab

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900