一、封装控件导出Excel
/// <summary> /// 将一个DataGridView导入Excel(YS) /// </summary> /// <param name="dgv"></param> public void ExportDataToExcel(MzGridControl dgv) { //string fileName = ""; string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xls"; saveDialog.Filter = "Excel文件|*.xls"; //saveDialog.FileName = fileName; saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; //导出Excel dgv.GridExport(saveFileName, GridExportType.Excel); //导出Word // dgv.GridExport(saveFileName, GridExportType.Excel); }
内部封装
/// <summary> /// 文档导出 /// </summary> public void GridExport(string filename, GridExportType exportType) { if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException(nameof(filename), "导出文件名为空"); } if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(filename))) { throw new ArgumentException(nameof(filename), "导出路径不存在"); } string extension = System.IO.Path.GetExtension(filename); switch (exportType) { case GridExportType.Excel: if (extension != ".xls" && extension != ".xlsx") { throw new ArgumentException(nameof(filename), "导出文件扩展名不正确"); } gridExcelExporter.Export(Grid, filename); break; } }
二、原生态控件DGV导出Excel
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace DataGridView_Excel { /// <summary> /// 先添加引用 Microsoft.Office.Interop.Excel(在.Net) /// </summary> public class DGVExcel { /// <summary> /// 将多个DataGridView 导入到一个Excel里 /// </summary> /// <param name="dgvlist"></param> public void ALLdgvExportDataExcel(List<DataGridView> dgvlist) { if (dgvlist.Count > 0) { string fileName = ""; string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xls"; saveDialog.Filter = "Excel文件|*.xls"; saveDialog.FileName = fileName; saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; if (saveFileName.IndexOf(":") < 0) { //被点了取消 return; } Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application(); if (xlapp == null) { MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel"); return; } Microsoft.Office.Interop.Excel.Workbooks workbooks = xlapp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); for (int i = 0; i < dgvlist.Count; i++) { //取得sheet Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; worksheet.Name = dgvlist[i].Name; //worksheet写入标题 for (int j = 0; j < dgvlist[j].ColumnCount; j++) { worksheet.Cells[1, j + 1] = dgvlist[i].Columns[j].HeaderText; } //worksheet写入数值 for (int r = 0; r < dgvlist[i].Rows.Count; r++) { for (int k = 0; k < dgvlist[i].ColumnCount; k++) { worksheet.Cells[r + 2, k + 1] = dgvlist[k].Rows[r].Cells[k].Value; } System.Windows.Forms.Application.DoEvents(); } //列宽自适应 worksheet.Columns.EntireColumn.AutoFit(); } MessageBox.Show(fileName + "保存成功!", "提示", MessageBoxButtons.OK); if (saveFileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(saveFileName); //fileSaved = true; } catch (Exception ex) { //fileSaved = false; MessageBox.Show("导出文件时出错,文件可能正被打开! " + ex.Message); } } xlapp.Quit(); //强行销毁(强制回收) GC.Collect(); } } /// <summary> /// 将一个DataGridView导入Excel /// </summary> /// <param name="dgv"></param> public void OnedgvExportDataExcel(DataGridView dgv) { string fileName = ""; string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xls"; saveDialog.Filter = "Excel文件|*.xls"; saveDialog.FileName = fileName; saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; if (saveFileName.IndexOf(":") < 0) { //被点了取消 return; } Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application(); if (xlapp == null) { MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel"); return; } Microsoft.Office.Interop.Excel.Workbooks workbooks = xlapp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); //取得sheet1 Microsoft.Office.Interop.Excel.Worksheet worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; worksheet1.Name = dgv.Name; //worksheet1写入标题 for (int i = 0; i < dgv.ColumnCount; i++) { worksheet1.Cells[1, i + 1] = dgv.Columns[i].HeaderText; } //worksheet1写入数值 for (int r = 0; r < dgv.Rows.Count; r++) { for (int i = 0; i < dgv.ColumnCount; i++) { worksheet1.Cells[r + 2, i + 1] = dgv.Rows[r].Cells[i].Value; } System.Windows.Forms.Application.DoEvents(); } //列宽自适应 worksheet1.Columns.EntireColumn.AutoFit(); MessageBox.Show(fileName + "保存成功!", "提示", MessageBoxButtons.OK); if (saveFileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(saveFileName); //fileSaved = true; } catch (Exception ex) { //fileSaved = false; MessageBox.Show("导出文件时出错,文件可能正被打开! " + ex.Message); } } xlapp.Quit(); //强行销毁(强制回收) GC.Collect(); } } }