Winform 导出Excel
private void 导出excelToolStripMenuItem_Click(object sender, EventArgs e) { if (dgvData.RowCount > 0) { var saveFileDialog1 = new SaveFileDialog { Filter = @"Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx", RestoreDirectory = true }; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { var result = _excel.SetExportExcel(saveFileDialog1.FileName, dgvData, 2); if (result != null) { MessageBox.Show(result.HasError ? "文件导出成功!!" : result.ErrorMessage); } } } else { MessageBox.Show(@"数据为空!!"); } } /// <summary> /// DataGridView 导出Excel方法 /// </summary> /// <param name="fileName">导出路径</param> /// <param name="gridView">数据源</param> /// <param name="num">从第几列开始导</param> public Result SetExportExcel(string fileName, DataGridView gridView, int num) { var result = new Result(); if (fileName.IndexOf(":") > 0) { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { result.ErrorMessage = "无法创建Excel对象,可能您的机子未安装Excel"; result.HasError = false; } Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; //取得sheet1 int a = 1, b = 1; for (var i = 0; i < gridView.RowCount; i++) { if (i == 0) { for (var j = num; j < gridView.Columns.Count; j++) { worksheet.Cells[1, a] = gridView.Columns[j].HeaderText; a++; } } for (var j = num; j < gridView.Columns.Count; j++) { worksheet.Cells[i + 2, b] = gridView.Rows[i].Cells[j].Value; b++; } b = 1; System.Windows.Forms.Application.DoEvents(); } worksheet.Columns.EntireColumn.AutoFit(); //列宽自适应 if (fileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(fileName); } catch (Exception ex) { result.ErrorMessage = "文件导出时出错,文件可能正被打开!" + ex.Message; result.HasError = false; } } xlApp.Quit(); GC.Collect(); //强行销毁 result.HasError = true; } return result; } /// <summary> /// ListView 导出Excel方法 /// </summary> /// <param name="fileName">导出路径</param> /// <param name="listView">数据源</param> /// <param name="num">从第几列开始导</param> public Result SetListViewExportExcel(string fileName, ListView listView, int num) { var result = new Result(); if (fileName.IndexOf(":") > 0) { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { result.ErrorMessage = "无法创建Excel对象,可能您的机子未安装Excel"; result.HasError = false; } Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; //取得sheet1 int a = 1, b = 1; for (var i = 0; i < listView.Items.Count; i++) { if (i == 0) { for (var j = num; j < listView.Columns.Count; j++) { worksheet.Cells[1, a] = listView.Columns[j].Text; a++; } } for (var j = num; j < listView.Columns.Count; j++) { worksheet.Cells[i + 2, b] = listView.Items[i].SubItems[j].Text; b++; } b = 1; System.Windows.Forms.Application.DoEvents(); } worksheet.Columns.EntireColumn.AutoFit(); //列宽自适应 if (fileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(fileName); } catch (Exception ex) { result.ErrorMessage = "文件导出时出错,文件可能正被打开!" + ex.Message; result.HasError = false; } } xlApp.Quit(); GC.Collect(); //强行销毁 result.HasError = true; } return result; }