ListViewEx.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.ComponentModel;
  9. using System.Data;
  10. namespace LYFZ.ComponentLibrary
  11. {
  12. public class ListViewEx : System.Windows.Forms.ListView
  13. {
  14. public ListViewEx()
  15. {
  16. SetStyle(ControlStyles.DoubleBuffer |
  17. ControlStyles.OptimizedDoubleBuffer |
  18. ControlStyles.AllPaintingInWmPaint, true);
  19. UpdateStyles();
  20. }
  21. private bool _isDefaultShowGroups;
  22. /// <summary>
  23. /// 默认是否显示分组
  24. /// </summary>
  25. public bool IsDefaultShowGroups
  26. {
  27. get
  28. {
  29. return _isDefaultShowGroups;
  30. }
  31. set
  32. {
  33. _isDefaultShowGroups = value;
  34. }
  35. }
  36. /// <summary>
  37. /// 默认显示分组
  38. /// </summary>
  39. public bool DefaultShowGroups = true;
  40. /// <summary>
  41. /// 默认不显示分组
  42. /// </summary>
  43. public bool DefaultNotShowGroups = false;
  44. const int WM_HSCROLL = 0x0114;
  45. const int WM_VSCROLL = 0x0115;
  46. /// <summary>
  47. /// 重写滚动条滚动事件
  48. /// </summary>
  49. /// <param name="m"></param>
  50. protected override void WndProc( ref Message m )
  51. {
  52. if ( m.Msg == WM_HSCROLL )
  53. {
  54. OnHScroll( this, new EventArgs() );
  55. }
  56. else if ( m.Msg == WM_VSCROLL )
  57. {
  58. OnVScroll( this, new EventArgs() );
  59. }
  60. base.WndProc( ref m );
  61. }
  62. /// <summary>
  63. /// 滚动条滚动事件
  64. /// </summary>
  65. public event EventHandler Scroll;
  66. /// <summary>
  67. /// 重写滚动条滚动事件
  68. /// </summary>
  69. /// <param name="sender"></param>
  70. /// <param name="e"></param>
  71. virtual protected void OnHScroll( object sender, EventArgs e )
  72. {
  73. if ( Scroll != null )
  74. Scroll( this, e );
  75. }
  76. /// <summary>
  77. /// 重写滚动条滚动事件
  78. /// </summary>
  79. /// <param name="sender"></param>
  80. /// <param name="e"></param>
  81. virtual protected void OnVScroll( object sender, EventArgs e )
  82. {
  83. if ( Scroll != null )
  84. Scroll( this, e );
  85. }
  86. /// <summary>
  87. /// 导出Excel
  88. /// </summary>
  89. public void ListViewToDataTable( string StrFileName = "" )
  90. {
  91. DataTable NewTable = new DataTable();
  92. for ( int i = 0; i < this.Columns.Count; i++ )
  93. {
  94. if ( this.Columns[i] == null || this.Columns[i].Text == "" )
  95. continue;
  96. NewTable.Columns.Add( this.Columns[i].Text.Trim(), typeof( string ) );
  97. }
  98. if ( this.Items.Count > 0 )
  99. {
  100. for ( int i = 0; i < this.Items.Count; i++ )
  101. {
  102. DataRow newRow = NewTable.NewRow();
  103. for ( int j = 0; j < this.Columns.Count; j++ )
  104. {
  105. if ( this.Columns[j] == null || this.Columns[j].Text == "" )
  106. continue;
  107. if ( j == 0 )
  108. {
  109. newRow[this.Columns[j].Text.Trim()] = this.Items[i].Text.ToString().Trim();
  110. }
  111. else
  112. {
  113. newRow[this.Columns[j].Text.Trim()] = this.Items[i].SubItems[this.Columns[j].Text.Trim()].Text.Trim();
  114. }
  115. }
  116. NewTable.Rows.Add( newRow );
  117. }
  118. }
  119. SaveFileDialog saveFile = new SaveFileDialog();
  120. saveFile.FileName = StrFileName;
  121. saveFile.Title = "导出数据报表文件";
  122. saveFile.Filter = "EXECL文件(*.xls) |*.xls |文本文件(*.txt) |*.txt |所有文件(*.*) |*.*";
  123. saveFile.FilterIndex = 1;
  124. DialogResult drst = saveFile.ShowDialog();
  125. if ( drst == DialogResult.No || drst == DialogResult.Cancel || drst == DialogResult.No || drst == DialogResult.Abort )
  126. {
  127. return;
  128. }
  129. LYFZ.ComponentLibrary.FrmLoadHandling.ExecutionDoWorkMethod(delegate (object obj, System.ComponentModel.BackgroundWorker backgroundWorker)
  130. {
  131. try
  132. {
  133. if (LYFZ.WinAPI.CustomPublicMethod.DataGridViewToExcel(saveFile.FileName.Trim(), NewTable, null, backgroundWorker))
  134. {
  135. MessageBoxCustom.Show("数据导出成功!");
  136. }
  137. else
  138. {
  139. MessageBoxCustom.Show("数据导出失败!");
  140. }
  141. }
  142. catch (Exception ex)
  143. {
  144. MessageBoxCustom.Show(String.Format("导出数据出错:{0}", ex.Message));
  145. }
  146. });
  147. }
  148. }
  149. }