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 viewDate Session RK CMK CNN Gs VB JN (Faculty Name)7/1/2013 1 REO PH2 REO TAS AM VH7/1/2013 2 PH2 AM TAs AM VH REO7/1/2013 3 AM TAs REO PH2 VH TAS8/1/2013 1 PH2 REO TAS VH AM PH28/1/2013 2 AM PH2 REO AM VH TAs8/1/2013 3 TAs AM VH REO VH PH2i 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 Course7/1/2013 1 REO7/1/2013 2 PH27/1/2013 3 AM8/1/2013 1 H28/1/2013 2 AM8/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 19:14