C#导出Excel设置单元格样式

C# 导出Excel

 1 //导出Excel
 2         private void ExportExcel(string fileName, System.Data.DataTable myDGV, string title)
 3         {
 4             string saveFileName = "";
 5             System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
 6             saveDialog.DefaultExt = "xlsx";
 7             saveDialog.Filter = "Excel文件|*.xlsx";
 8             saveDialog.FileName = fileName;
 9             saveDialog.ShowDialog();
10             saveFileName = saveDialog.FileName;
11             if (saveFileName.IndexOf(":") < 0) return; //被点了取消
12             Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
13             if (xlApp == null)
14             {
15                 System.Windows.MessageBox.Show("无法创建Excel对象,可能您的电脑未安装Excel");
16                 return;
17             }
18             Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
19             Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
20             Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
21 
22             //操作单元格
23             Range rangeLecture = worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[1, myDGV.Columns.Count]];
24             rangeLecture.Application.DisplayAlerts = false;
25             rangeLecture.Merge(Missing.Value);
26             rangeLecture.Application.DisplayAlerts = true;
27             worksheet.Cells[1, 1] = title;
28            
29 
30             //写入标题
31             for (int i = 0; i < myDGV.Columns.Count; i++)
32             {
33                 worksheet.Cells[2, i + 1] = myDGV.Columns[i].ColumnName;
34             }
35             //写入数值
36             for (int r = 0; r < myDGV.Rows.Count; r++)
37             {
38                 for (int i = 0; i < myDGV.Columns.Count; i++)
39                 {
40                     worksheet.Cells[r + 3, i + 1] = myDGV.Rows[r][i].ToString();
41                 }
42                 System.Windows.Forms.Application.DoEvents();
43             }
44             worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
45             if (saveFileName != "")
46             {
47                 try
48                 {
49                     workbook.Saved = true;
50                     workbook.SaveCopyAs(saveFileName);
51                 }
52                 catch (Exception ex)
53                 {
54                     System.Windows.MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
55                 }
56             }
57             xlApp.Quit();
58             GC.Collect();//强行销毁
59             //System.Windows.Forms.MessageBox.Show("文件保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
60         }

本文链接:https://blog.csdn.net/weixin_43343835/article/details/90713294

posted @ 2021-09-13 10:43  dexin  阅读(611)  评论(0编辑  收藏  举报