C#中ListView數據導出excel表
from C#.net2.0--CDRapp--appMain.cs private void cdrToExcel() { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Excel(*.xls)|*.xls"; dialog.FileName = "CDR.xls"; if (dialog.ShowDialog() == DialogResult.OK) { Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); object missing = System.Reflection.Missing.Value; try { if (xlApp == null) { MessageBox.Show("無法創建Excel對象,可能您的機子未安裝Excel"); return; } Microsoft.Office.Interop.Excel.Workbooks xlBooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook xlBook = xlBooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlBook.Worksheets[1]; Microsoft.Office.Interop.Excel.Range range = null; //擡頭 range = xlSheet.get_Range("A1", "P1"); range.Merge(Missing.Value); // 合併單元格
range.Columns.AutoFit(); // 設置列寬爲自動適
應 // 設置單元格左邊框加粗 range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick; // 設置單元格右邊框加粗 range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick; range.HorizontalAlignment = XlHAlign.xlHAlignCenter;// 設置單元格水平居中
range.Value2 = "CDR"; range.Font.Size = 18; // 設置字體大小
//range.Font.ColorIndex = 5; // 設置字體顏色
//range.Interior.ColorIndex = 6; // 設置單元格背景色
//range.RowHeight = 25; // 設置行高
//range.ColumnWidth = 20; // 設置列寬
xlSheet.Cells[2, 1] = "Cdr_Id"; xlSheet.Cells[2, 2] = "Create_time"; xlSheet.Cells[2, 3] = "Setup_time"; xlSheet.Cells[2, 4] = "Alerting_time"; xlSheet.Cells[2, 5] = "Connect_timeconnect_time"; xlSheet.Cells[2, 6] = "During"; xlSheet.Cells[2, 7] = "Caller"; xlSheet.Cells[2, 8] = "Called"; xlSheet.Cells[2, 9] = "Caller_ip"; xlSheet.Cells[2, 10] = "Called_ip"; xlSheet.Cells[2, 11] = "Src_info"; xlSheet.Cells[2, 12] = "Dest_info"; xlSheet.Cells[2, 13] = "Release"; xlSheet.Cells[2, 14] = "Release_reason"; xlSheet.Cells[2, 15] = "Ring_time"; xlSheet.Cells[2, 16] = "Usernametype"; int rowIndex = 3;//這個用來標記數據從多少行開始
//設置單元格 for (int i = 1; i <= lvCDR.Columns.Count; i++) { range = xlSheet.get_Range(xlSheet.Cells[3, i], xlSheet.Cells[rowIndex + this.lvCDR.Items.Count, i]); range.HorizontalAlignment = XlHAlign.xlHAlignLeft;// 設置單元格水平居左
range.NumberFormatLocal = "@";//文本格式
range.ColumnWidth = 12; } //標題欄 range = xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[2, 16]); range.Interior.ColorIndex = 45;//設置標題背景色爲 淺橙色
range.Font.Bold = true;//標題字體加粗
int isOut = 0; pbProglass.Visible = true; foreach (ListViewItem objItem in this.lvCDR.Items) { for (int i = 0; i < lvCDR.Columns.Count ; i++) { xlSheet.Cells[rowIndex, i + 1] = objItem.SubItems[i].Text; } rowIndex += 1; isOut++; if (isOut > pbProglass.Maximum) { isOut = 0; } pbProglass.Value = isOut; } pbProglass.Visible = false; //數據區域 range = xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[rowIndex, 16]); range.Borders.LineStyle = 1; range.Font.Size = 10; range = xlSheet.get_Range(xlSheet.Cells[rowIndex, 1], xlSheet.Cells[rowIndex, 16]); range.Merge(Missing.Value); // 合併單元格 // range.Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;
// 設置單元格右邊框加粗 // range.Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;
range.RowHeight = 20; //range.Value2 = " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
range.HorizontalAlignment = XlHAlign.xlHAlignRight;// 設置單元格水平居中
if (xlSheet != null) { xlSheet.SaveAs(dialog.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing); xlApp.Visible = true; } } catch (Exception) { xlApp.Quit(); throw; } } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.