本文主要是介绍winfrom 快速 导出excel,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
此方法比较快,500条8列的数据不到1秒只需要给一个有数据的datagridview就可以了
private void ToExcel_Click(object sender, EventArgs e){SaveFileDialog sfd = new SaveFileDialog();sfd.Filter = "Execl files (*.xls)|*.xlsx";sfd.FilterIndex = 0;sfd.RestoreDirectory = true;sfd.CreatePrompt = false;sfd.Title = "导出文件保存路径";if (sfd.ShowDialog() == DialogResult.OK){string path = sfd.FileName; //时间计算DateTime dt1 = DateTime.Now;saveAsExcel(path, dataGridView1);DateTime dt2 = DateTime.Now;MessageBox.Show("导出成功!共用了:" + (dt2 - dt1));}}//这个代码速度特别快,基本达到导出csv的速度public static void SaveAsExcel(string path, DataGridView gridView){if (gridView.RowCount > 65536){MessageBox.Show("当前表格中记录太多,不能导出excel(excel最多可以存储65536行记录)", "提示");//return;}try{saveAsExcel(path, gridView);System.GC.Collect();}catch (Exception ex){MessageBox.Show("导出excel时发生错误: " + ex.Message, "提示");System.GC.Collect();}}//上面的 SaveAsExcel 调用 saveAsExcel 的目,估计是为了解决 excel进程销毁的问题private static void saveAsExcel(string path, DataGridView gridView){object objMissing = System.Reflection.Missing.Value;Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();app.Visible = false;Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Add(objMissing);Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[1];//以下3个对象在前面定义,为了能在异常中可以释放Microsoft.Office.Interop.Excel.Range ranges = null;Microsoft.Office.Interop.Excel.Range range = null;Microsoft.Office.Interop.Excel.Range head = null;try{for (int i = 0; i < gridView.Columns.Count; i++){//Microsoft.Office.Interop.Excel.Range head = (Microsoft.Office.Interop.Excel.Range)workSheet.Cells[1, i + 1];head = (Microsoft.Office.Interop.Excel.Range)workSheet.Cells[1, i + 1];head.Value2 = gridView.Columns[i].HeaderText;head = null;}//A2用于指定起始行的位置//Microsoft.Office.Interop.Excel.Range range = workSheet.get_Range("A2", objMissing);range = workSheet.get_Range("A2", objMissing);int rows = gridView.Rows.Count, cols = gridView.Columns.Count;//Microsoft.Office.Interop.Excel.Range ranges = range.get_Resize(rows, cols);ranges = range.get_Resize(rows, cols);object[,] datas = new object[rows, cols];for (int i = 0; i < rows; i++){for (int j = 0; j < cols; j++){DataGridViewCell cell = gridView[j, i];if (typeof(string) == cell.ValueType){datas[i, j] = string.Format("'{0}", cell.FormattedValue);}else{datas[i, j] = cell.FormattedValue.ToString();}}}ranges.Value2 = datas;workBook.SaveAs(path, objMissing, objMissing, objMissing, objMissing, objMissing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, objMissing,objMissing, objMissing, objMissing, objMissing);}catch (Exception ex){throw (ex);}finally{workBook.Close(false, objMissing, objMissing);app.Quit();range = null;ranges = null;workBook = null;workSheet = null;app = null;}}
ps:此方法只是从网上找到的,地址已经不记得了。里面的注释什么的可能不一定对。但是整体方法肯定是可以的。
这只是基于datagridview的导出,也没有sheet分页。如果想要其他的类型的,还要自己修改!
这篇关于winfrom 快速 导出excel的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!