本文介绍了错误:在C#中将datagridview导出到excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 使用VS 2008,c#windows表格。 我已经一直在尝试将我的datagridview导出到excel文件。但是excel文件只有gridview上的第一个数据。我已经添加了Microsoft.Office.Interop.Excel的引用。 这是我的导出按钮上的代码: 使用 Microsoft.Office.Interop; 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 = 0 ; i < dataGridView2.Rows.Count; i ++) { DataGridViewRow row = dataGridView2.Rows [i] ; for ( int j = 0 ; j < row.Cells.Count; j ++) { ExcelApp.Cells [i + 1 ,j + 1 ] = row.Cells [j] .ToString(); } } ExcelApp.ActiveWorkbook.SaveCopyAs( C:\\\ \\reports.xls); ExcelApp.ActiveWorkbook.Saved = true ; ExcelApp.Quit(); MessageBox.Show( 创建的Excel文件,你可以找到文件C:\\reports。 xls); 感谢您的回复。解决方案 Hi all,Using VS 2008, c# windows forms.I''ve been trying to export my datagridview to an excel file. But the excel file has only the first data on the gridview. I''ve already add the reference which is Microsoft.Office.Interop.Excel.Here''s my code on my Export button:using Microsoft.Office.Interop; 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 = 0; i < dataGridView2.Rows.Count; i++) { DataGridViewRow row = dataGridView2.Rows[i]; for (int j = 0; j < row.Cells.Count; j++) { ExcelApp.Cells[i + 1, j + 1] = row.Cells[j].ToString(); } } ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\reports.xls"); ExcelApp.ActiveWorkbook.Saved = true; ExcelApp.Quit(); MessageBox.Show("Excel file created,you can find the file C:\\reports.xls");Thanks for the reply. 解决方案 这篇关于错误:在C#中将datagridview导出到excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 03:29