WPF 导出数据
1 //数据导出Excel功能 2 3 private void btnExcelZD_Click(object sender, RoutedEventArgs e) 4 { 5 if (zdDataGrid.Items.Count == 0) 6 { 7 Message_Box.Show("没有数据可以导出!"); 8 return; 9 } 10 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 11 if (xlApp == null) 12 { 13 Message_Box.Show("无法创建Excel对象,可能您的机子未安装Excel!"); 14 return; 15 } 16 Microsoft.Office.Interop.Excel.Workbooks books = xlApp.Workbooks; 17 Microsoft.Office.Interop.Excel.Workbook book = books.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); 18 Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Worksheets[1]; 19 sheet.Cells[1, 1] = "终端信息管理"; 20 sheet.Cells[2, 1] = "终端编号"; 21 sheet.Cells[2, 2] = "IP"; 22 sheet.Cells[2, 3] = "所属部门"; 23 sheet.Cells[2, 4] = "姓名"; 24 sheet.Cells[2, 5] = "工号"; 25 sheet.Cells[2, 6] = "终端名称"; 26 sheet.Cells[2, 7] = "更新时间"; 27 sheet.Cells[2, 8] = "终端类型"; 28 //设置Excel样式 29 Microsoft.Office.Interop.Excel.Range rang1; 30 Microsoft.Office.Interop.Excel.Range title1; 31 Microsoft.Office.Interop.Excel.Range merge1; 32 //数据导出 33 for (int i = 0; i < zdDataGrid.Items.Count; i++) 34 { 35 Model.Client client = (Model.Client)zdDataGrid.Items[i]; 36 sheet.Cells[i + 3, 1] = client.SN; 37 sheet.Cells[i + 3, 2] = client.IP; 38 sheet.Cells[i + 3, 3] = client.DName; 39 sheet.Cells[i + 3, 4] = client.Name; 40 sheet.Cells[i + 3, 5] = client.JobNumber; 41 sheet.Cells[i + 3, 6] = client.ClientName; 42 sheet.Cells[i + 3, 7] = client.UpDateTime.ToString(); 43 sheet.Cells[i + 3, 8] = client.Types; 44 } 45 merge1 = (Microsoft.Office.Interop.Excel.Range)sheet.get_Range("A1", "H1"); 46 merge1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; 47 merge1.Font.Name = "黑体"; 48 merge1.Font.Size = 16; 49 merge1.Merge(0); 50 merge1.Borders.LineStyle = 1; 51 title1 = (Microsoft.Office.Interop.Excel.Range)sheet.get_Range("A2", "H2"); 52 title1.Cells.Interior.Color = System.Drawing.Color.FromArgb(208, 206, 210).ToArgb(); 53 title1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; 54 title1.ColumnWidth = 15; 55 title1.Borders.LineStyle = 2; 56 title1.Font.Size = 13; 57 int Number = zdDataGrid.Items.Count + 2; 58 string HN = "H" + Number; 59 string A3 = "A3"; 60 rang1 = (Microsoft.Office.Interop.Excel.Range)sheet.get_Range(A3, HN); 61 rang1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; 62 rang1.ColumnWidth = 15; 63 rang1.Borders.LineStyle = 1; 64 //显示时间和日期 65 Microsoft.Office.Interop.Excel.Range time1; 66 int Num = zdDataGrid.Items.Count + 3; 67 string ANS = "A" + Num; 68 string HNS = "H" + Num; 69 time1 = (Microsoft.Office.Interop.Excel.Range)sheet.get_Range(ANS, HNS); 70 time1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight; 71 time1.RowHeight = 25; 72 time1.Merge(0); 73 time1.Value = DateTime.Now.ToString(); 74 xlApp.DefaultFilePath = "D:\\"; 75 SaveFileDialog sfd = new SaveFileDialog(); 76 sfd.Filter = "数据文件(*.xlsx)|*.xlsx"; 77 sfd.FilterIndex = 2; 78 sfd.FileName = (DateTime.Now.ToString().Replace(":", "-").Replace("/", "-") + "终端信息管理.xlsx"); 79 if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 80 { 81 book.Saved = true; 82 book.SaveAs(sfd.FileName); 83 book.Close(); 84 } 85 }