方法一、利用微软的excel 操作类
引用:using Excel = Microsoft.Office.Interop.Excel;
代码如下:
#region导出数据表:Excle (微软的excel 操作类) ///<summary> ///导出数据表:Excle ///</summary> ///<param name="myDGV"></param> public void _ToExcel(DataGridView myDGV) { string path = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xlsx"; saveDialog.Filter = "Excel文件|*.xlsx"; saveDialog.ShowDialog(); path = saveDialog.FileName; if (path.IndexOf(":") < 0) return; //判断是否点击取消 try { Excel.Application xlApp = new Excel.Application(); if (xlApp == null) { MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel"); return; } Excel.Workbooks workbooks = xlApp.Workbooks; Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet); Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 int colIndex = 0; //写入标题 for (int i = 0; i < myDGV.ColumnCount; i++) { if (myDGV.Columns[i].Visible)//用作于不导出隐藏列 { colIndex++; worksheet.Cells[1, colIndex] = myDGV.Columns[i].HeaderText; //worksheet.Cells[1, i] = myDGV.Columns[i].HeaderText; } } //写入数值 for (int r = 0; r < myDGV.Rows.Count - 1; r++) { colIndex = 0; for (int i = 0; i < myDGV.ColumnCount; i++) { if (myDGV.Columns[i].Visible) { colIndex++; worksheet.Cells[r + 2, colIndex] = myDGV.Rows[r].Cells[i].Value; } } System.Windows.Forms.Application.DoEvents(); } worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 if (path != "") { try { workbook.Saved = true; workbook.SaveCopyAs(path); //fileSaved = true; } catch (Exception ex) { //fileSaved = false; MessageBox.Show("导出文件时出错,文件可能正被打开!\n\r" + ex.Message); } } xlApp.Quit(); GC.Collect();//强行销毁 // if (fileSaved && System.IO.File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); //打开EXCEL MessageBox.Show(path + ",导出成功", "系统提示", MessageBoxButtons.OK); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion
方法二、IO基础流操作类
引用:using System.IO;
using System.Threading;
代码如下:
#region导出数据表:Excle (io流操作类) ///<summary> ///导出数据表:Excle (io流操作类) ///</summary> ///<param name="myDGV"></param> public void _ToExcel2(DataGridView myDGV) { string path = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xls"; saveDialog.Filter = "Excel97-2003 (*.xls)|*.xls|All Files (*.*)|*.*"; saveDialog.ShowDialog(); path = saveDialog.FileName; if (path.IndexOf(":") < 0) return; //判断是否点击取消 try { Thread.Sleep(1000); StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312")); StringBuilder sb = new StringBuilder(); //写入标题 for (int k = 0; k < myDGV.Columns.Count; k++) { if (myDGV.Columns[k].Visible)//导出可见的标题 { //"\t"就等于键盘上的Tab,加个"\t"的意思是: 填充完后进入下一个单元格. sb.Append(myDGV.Columns[k].HeaderText.ToString().Trim() + "\t"); } } sb.Append(Environment.NewLine);//换行 //写入每行数值 for (int i = 0; i < myDGV.Rows.Count - 1; i++) { System.Windows.Forms.Application.DoEvents(); for (int j = 0; j < myDGV.Columns.Count; j++) { if (myDGV.Columns[j].Visible)//导出可见的单元格 { //注意单元格有一定的字节数量限制,如果超出,就会出现两个单元格的内容是一模一样的. //具体限制是多少字节,没有作深入研究. sb.Append(myDGV.Rows[i].Cells[j].Value.ToString().Trim() + "\t"); } } sb.Append(Environment.NewLine); //换行 } sw.Write(sb.ToString()); sw.Flush(); sw.Close(); MessageBox.Show(path + ",导出成功", "系统提示", MessageBoxButtons.OK); } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion
二种方法对比,利用IO基础流导出到Excel表格中的速度最快。特别是上10万行以上的数据,更是能体现其速度。
本文出自 “9212950” 博客,请务必保留此出处http://9222950.blog.51cto.com/9212950/1607070