導出至Excel是非常常見,我們可以用很多類庫,例如Aspose、NOPI、Interop,在這裡我們使用微軟自家的工具。我的WPF綁定的ObservableCollection<T>集合。 該方法還不止這麼多,還涉及到了1-27 = A-Z 的適配,如以下方法。 最後我們獲取數據給方法,下麵是設計 ...
導出至Excel是非常常見,我們可以用很多類庫,例如Aspose、NOPI、Interop,在這裡我們使用微軟自家的工具。我的WPF綁定的ObservableCollection<T>集合。
public string ExcelExport(System.Data.DataTable DT, string title) { try { //創建Excel Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook ExcelBook = ExcelApp.Workbooks.Add(System.Type.Missing); //創建工作表(即Excel里的子表sheet) 1表示在子表sheet1里進行數據導出 Microsoft.Office.Interop.Excel.Worksheet ExcelSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExcelBook.Worksheets[1]; //如果數據中存在數字類型 可以讓它變文本格式顯示 ExcelSheet.Cells.NumberFormat = "@"; //設置工作表名 ExcelSheet.Name = title; //設置Sheet標題 string start = "A1"; string end = ChangeASC(DT.Columns.Count) + "1"; Microsoft.Office.Interop.Excel.Range _Range = (Microsoft.Office.Interop.Excel.Range)ExcelSheet.get_Range(start, end); _Range.Merge(0); //單元格合併動作(要配合上面的get_Range()進行設計) _Range = (Microsoft.Office.Interop.Excel.Range)ExcelSheet.get_Range(start, end); _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; _Range.Font.Size = 22; //設置字體大小 _Range.Font.Name = "宋體"; //設置字體的種類 ExcelSheet.Cells[1, 1] = title; //Excel單元格賦值 _Range.EntireColumn.AutoFit(); //自動調整列寬 //寫表頭 for (int m = 1; m <= DT.Columns.Count; m++) { ExcelSheet.Cells[2, m] = DT.Columns[m - 1].ColumnName.ToString(); start = "A2"; end = ChangeASC(DT.Columns.Count) + "2"; _Range = (Microsoft.Office.Interop.Excel.Range)ExcelSheet.get_Range(start, end); _Range.Font.Size = 14; //設置字體大小 _Range.Font.Name = "宋體"; //設置字體的種類 _Range.EntireColumn.AutoFit(); //自動調整列寬 _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; } //寫數據 for (int i = 0; i < DT.Rows.Count; i++) { for (int j = 1; j <= DT.Columns.Count; j++) { //Excel單元格第一個從索引1開始 // if (j == 0) j = 1; ExcelSheet.Cells[i + 3, j] = DT.Rows[i][j - 1].ToString(); } } //表格屬性設置 for (int n = 0; n < DT.Rows.Count + 1; n++) { start = "A" + (n + 3).ToString(); end = ChangeASC(DT.Columns.Count) + (n + 3).ToString(); //獲取Excel多個單元格區域 _Range = (Microsoft.Office.Interop.Excel.Range)ExcelSheet.get_Range(start, end); _Range.Font.Size = 12; //設置字體大小 _Range.Font.Name = "宋體"; //設置字體的種類 _Range.EntireColumn.AutoFit(); //自動調整列寬 _Range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //設置字體在單元格內的對其方式 _Range.EntireColumn.AutoFit(); //自動調整列寬 } ExcelApp.DisplayAlerts = false; //保存Excel的時候,不彈出是否保存的視窗直接進行保存 ////彈出保存對話框,並保存文件 Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog(); sfd.DefaultExt = ".xlsx"; sfd.Filter = "Office 2007 File|*.xlsx|Office 2000-2003 File|*.xls|所有文件|*.*"; if (sfd.ShowDialog() == true) { if (sfd.FileName != "") { ExcelBook.SaveAs(sfd.FileName); //將其進行保存到指定的路徑 System.Windows.MessageBox.Show("導出文件已存儲為: " + sfd.FileName, "溫馨提示"); } } //釋放可能還沒釋放的進程 ExcelBook.Close(); ExcelApp.Quit(); return sfd.FileName; } catch { //System.Windows.MessageBox.Show("導出文件保存失敗,可能原因該文件已打開!", "警告!"); return null; } }
該方法還不止這麼多,還涉及到了1-27 = A-Z 的適配,如以下方法。
/// <summary> /// 獲取當前列列名,並得到EXCEL中對應的列 /// </summary> /// <param name="count"></param> /// <returns></returns> private string ChangeASC(int count) { string ascstr = ""; switch (count) { case 1: ascstr = "A"; break; case 2: ascstr = "B"; break; case 3: ascstr = "C"; break; case 4: ascstr = "D"; break; case 5: ascstr = "E"; break; case 6: ascstr = "F"; break; case 7: ascstr = "G"; break; case 8: ascstr = "H"; break; case 9: ascstr = "I"; break; case 10: ascstr = "J"; break; case 11: ascstr = "K"; break; case 12: ascstr = "L"; break; case 13: ascstr = "M"; break; case 14: ascstr = "N"; break; case 15: ascstr = "O"; break; case 16: ascstr = "P"; break; case 17: ascstr = "Q"; break; case 18: ascstr = "R"; break; case 19: ascstr = "S"; break; case 20: ascstr = "T"; break; default: ascstr = "U"; break; } return ascstr; }
最後我們獲取數據給方法,下麵是設計了數據轉換到datatable的一個過程。
private void Export(object sender, RoutedEventArgs e) { DataTable newTB = new DataTable(); ObservableCollection<XModel.STORE_IN> instore = this.dataGrid.ItemsSource as ObservableCollection<XModel.STORE_IN>; List<XModel.STORE_IN> list = new List<XModel.STORE_IN>(instore.ToList()); newTB = XTools.XHelper.Datavalidation.CopyToDataTable<XModel.STORE_IN>(list); ExcelExport(newTB, "入庫記錄"); }
public static class Datavalidation { public static DataTable CopyToDataTable<T>(this IEnumerable<T> array) { var ret = new DataTable(); foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T))) ret.Columns.Add(dp.Name); foreach (T item in array) { var Row = ret.NewRow(); foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T))) Row[dp.Name] = dp.GetValue(item); ret.Rows.Add(Row); } return ret; } }