从数据网格视图看如何绑定到excel [英] Fom the data grid view how to bind to the excel

查看:102
本文介绍了从数据网格视图看如何绑定到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数据网格视图如何将数据绑定到excel;



输出如下数据网格视图



日期会议RK CMK CNN Gs VB JN(学院名称)



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想要教师在日期和会议上明确学习的报告;



例如RK(学院名称)



i希望从DATA GRID VIEW中输出如下EXCEL SHEET RK(教职员名称)





RK(学院名称)



日期课程

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

解决方案

希望这个例子可以帮助你....



使用System.ComponentModel;

private void btnGenerateExcel_Clickddd(object sender,EventArgs e)

{

//启动BackgroundWorker。



backgroundWorker1.RunWorkerAsync();





//创建Excel应用程序

Microsoft.Office.Interop.Excel ._Application app = new Microsoft.Office.Interop.Excel.Application();



//在Excel应用程序中创建新的WorkBook

Microsoft .Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);



//在工作簿中创建新的Excel表格

Microsoft.Office.Intero p.Excel._Worksheet worksheet = null;



//查看程序背后的excel表

app.Visible = false;



//获取第一张纸的参考。默认情况下,它的名称是Sheet1。

//存储它对工作表的引用

worksheet = workbook.Sheets [Sheet1];

工作表= workbook.ActiveSheet;



//更改活动表格的名称

worksheet.Name =检验订单明细;







//在Excel中存储标题部分

for(int i = 1 ; i< dataGridView1.Columns.Count + 1; i ++)

{

worksheet.Cells [1,i] = dataGridView1.Columns [i - 1] .HeaderText ; //您希望显示的Excel表格中的标题

}



//将每行和每列值存储到Excel表格
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(); //在你的情况下,你可以给前三个colums



}

}



//保存应用程序

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) ;



//退出应用程序

app.Quit();



releaseObject(工作表);



releaseObject(工作簿);

releaseObject(app);



MessageBox.Show(创建Excel文件,你可以找到文件C:\\Users \\xxxxxxx \\Desktop \\Temp \\ output。 xlsx);

}

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

解决方案

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");
}


这篇关于从数据网格视图看如何绑定到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆