介绍
如何在c#中导出Excel文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
具体内容如下
using 系统; using System.Collections.Generic; using 包含; using System.Data; using System.Windows.Forms; using System.Reflection; namespace DMS {///,& lt; summary>///,c#操作Excel类///,& lt;/summary> class ExcelOperate {//法一//public bool  DataSetToExcel (DataSet 数据集,bool isShowExcle)//{//,,DataTable DataTable =, dataSet.Tables [0];//,,int rowNumber =, dataTable.Rows.Count;//,,int columnNumber =, dataTable.Columns.Count;//,,if (rowNumber ==, 0)//,,{//,,,,MessageBox.Show(“没有任何数据可以导入到Excel文件!“);//,,,,return 假;//,,}//,,//建立Excel对象//,,Microsoft.Office.Interop.Excel.Application excel =, new Microsoft.Office.Interop.Excel.Application ();//,,excel.Application.Workbooks.Add(真正的);//,,excel.Visible =, isShowExcle;//是否打开该Excel文件//,,//填充数据//,,for (int c =, 0;, c & lt;, rowNumber;, c++)//,,{//,,,,for (int j =, 0;, j & lt;, columnNumber;, j + +)//,,,,{//,,,,,,excel.Cells [+ c 1, j +, 1],=, dataTable.Rows [c] .ItemArray [j];//,,,,}//,,}//,,return 真实;//}//法二//public bool  DataSetToExcel (DataSet 数据集,bool isShowExcle)//{//,,DataTable DataTable =, dataSet.Tables [0];//,,int rowNumber =, dataTable.Rows.Count;//,,int rowIndex =, 1;//,,int colIndex =, 0;//,,if (rowNumber ==, 0)//,,{//,,,,return 假;//,,}//,,//建立Excel对象//,,Microsoft.Office.Interop.Excel.Application excel =, new Microsoft.Office.Interop.Excel.Application ();//,,excel.Application.Workbooks.Add(真正的);//,,excel.Visible =, isShowExcle;//,,//生成字段名称//,,foreach (DataColumn col  dataTable.Columns拷贝)//,,{//,,,,colIndex + +;//,,,,excel.Cells [1, colIndex],=, col.ColumnName;//,,}//,,//填充数据//,,foreach (DataRow row  dataTable.Rows拷贝)//,,{//,,,,rowIndex + +;//,,,,colIndex =, 0;//,,,,foreach (DataColumn col  dataTable.Columns拷贝)//,,,,{//,,,,,,colIndex + +;//,,,,,,excel.Cells [rowIndex, colIndex],=,行(col.ColumnName);//,,,,}//,,}//,,return 真实;//}//法三(速度最快)///,& lt; summary>///,将数据集中的数据导出到EXCEL文件///,& lt;/summary>///,& lt; param name=癲ataSet"在输入数据集& lt;/param>///,& lt; param name=癷sShowExcle"祝辞是否显示该EXCEL文件& lt;/param>///,& lt; returns> & lt;/returns> public bool  DataSetToExcel (DataSet 数据集,bool isShowExcle) { DataTable DataTable =, dataSet.Tables [0]; int rowNumber =, dataTable.Rows.Count;//不包括字段名 int columnNumber =, dataTable.Columns.Count; int colIndex =, 0; if (rowNumber ==, 0), { return 假; }//建立Excel对象, Microsoft.Office.Interop.Excel.Application excel =, new Microsoft.Office.Interop.Excel.Application ();//excel.Application.Workbooks.Add(真正的); Microsoft.Office.Interop.Excel.Workbook workbook =, excel.Workbooks.Add (Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet =, (Microsoft.Office.Interop.Excel.Worksheet) workbook.Worksheets [1]; 时间=excel.Visible isShowExcle;//Microsoft.Office.Interop.Excel.Worksheet worksheet =, (Microsoft.Office.Interop.Excel.Worksheet) excel.Worksheets [1]; Microsoft.Office.Interop.Excel.Range 范围;//生成字段名称, foreach (DataColumn col  dataTable.Columns拷贝) { colIndex + +; excel.Cells [1, colIndex],=, col.ColumnName; } 对象[,],objData =, new 对象[rowNumber, columnNumber];, 时间=for (int r 0;, r & lt;, rowNumber;, r + +), { 时间=for (int c 0;, c & lt;, columnNumber;, c++) { objData [c] r,,,=, dataTable.Rows [r] [c]; }//Application.DoEvents (); }//,写入Excel null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在c#中导出Excel文件