VB C# listview 中的数据导出到excel 文件

前端之家收集整理的这篇文章主要介绍了VB C# listview 中的数据导出到excel 文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //先添加一个button1,text为export to excel
  2.  
  3. //添加一个saveFileDialog1,filter设置为"Excel 文件(*.xls)|*.xls";
  4.  
  5.  
  6.  
  7.  
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Windows.Forms;
  16. using Microsoft.Office.Interop.Excel;
  17. using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
  18. using System.Reflection;
  19. using System.IO;
  20.  
  21.  
  22. namespace TestExportExcel
  23. {
  24. public partial class Form1 : Form
  25. {
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. }
  30.  
  31. private void button1_Click(object sender,EventArgs e)
  32. {
  33.  
  34. //string saveFileName = "";
  35. //saveFileDialog1 = new SaveFileDialog();
  36. //saveFileDialog1.Filter = "Excel 文件(*.xls)|*.xls";
  37. // saveFileDialog1.RestoreDirectory = true;
  38. //saveFileName = saveFileDialog1.FileName;
  39.  
  40. saveFileDialog1.FileName = DateTime.Now.ToString("yyyy-MM-dd");
  41. if (saveFileDialog1.ShowDialog() ==DialogResult.OK)
  42. {
  43. TurnToExcel(listView1,"LOG");
  44.  
  45. }
  46. }
  47.  
  48. public void TurnToExcel(ListView listView1,string sheet1)
  49. {
  50. string Sheetname = sheet1;
  51. ListView listView = listView1;
  52. if (listView.Items.Count < 1)
  53. return;
  54. try
  55. {
  56. ExcelApplication MyExcel = new ExcelApplication();
  57.  
  58. MyExcel.Visible = true; //display excel application;if value set 'false',please enable the ' finally' code below;
  59. if (MyExcel == null)
  60. {
  61. return;
  62. }
  63.  
  64. Workbooks MyWorkBooks = (Workbooks)MyExcel.Workbooks;
  65.  
  66. Workbook MyWorkBook = (Workbook)MyWorkBooks.Add(Missing.Value);
  67.  
  68. Worksheet MyWorkSheet = (Worksheet)MyWorkBook.Worksheets[1];
  69.  
  70.  
  71. Range MyRange = MyWorkSheet.get_Range("A1","H1");
  72. MyRange = MyRange.get_Resize(1,listView.Columns.Count);
  73. object[] MyHeader = new object[listView.Columns.Count];
  74. for (int i = 0; i < listView.Columns.Count; i++)
  75. {
  76. MyHeader.SetValue(listView.Columns[i].Text,i);
  77. }
  78. MyRange.Value2 = MyHeader;
  79. MyWorkSheet.Name = Sheetname;
  80.  
  81. if (listView.Items.Count > 0)
  82. {
  83. MyRange = MyWorkSheet.get_Range("A2",Missing.Value);
  84. object[,] MyData = new Object[listView.Items.Count,listView.Columns.Count];
  85. for (int j = 0; j < listView1.Items.Count; j++)
  86. {
  87. ListViewItem lvi = listView1.Items[j];
  88. for (int k = 0; k < listView.Columns.Count; k++)
  89. {
  90.  
  91. MyData[j,k] = lvi.SubItems[k].Text;
  92. }
  93.  
  94. }
  95. MyRange = MyRange.get_Resize(listView.Items.Count,listView.Columns.Count);
  96. MyRange.Value2 = MyData;
  97. MyRange.EntireColumn.AutoFit();
  98. }
  99.  
  100. try
  101. {
  102. object missing = System.Reflection.Missing.Value;
  103. MyWorkBook.Saved = true;
  104. MyWorkBook.SaveAs(saveFileDialog1.FileName,Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,missing,false,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,missing);
  105. }
  106. catch (Exception e1)
  107. {
  108. MessageBox.Show("Export Error,Maybe the file is opened by other application!\n" + e1.Message);
  109. }
  110. /*
  111. finally
  112. {
  113. MyExcel.Quit();
  114. System.GC.Collect();
  115. }
  116. */
  117.  
  118. // MyExcel = null;
  119.  
  120. }
  121. catch (Exception Err)
  122. {
  123. MessageBox.Show(Err.Message);
  124. }
  125.  
  126. }
  127.  
  128.  
  129.  
  130. }
  131. }
  132.  

猜你在找的VB相关文章