123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.ComponentModel;
- using System.Data;
- namespace LYFZ.ComponentLibrary
- {
- public class ListViewEx : System.Windows.Forms.ListView
- {
- public ListViewEx()
- {
- SetStyle(ControlStyles.DoubleBuffer |
- ControlStyles.OptimizedDoubleBuffer |
- ControlStyles.AllPaintingInWmPaint, true);
- UpdateStyles();
- }
- private bool _isDefaultShowGroups;
- /// <summary>
- /// 默认是否显示分组
- /// </summary>
- public bool IsDefaultShowGroups
- {
- get
- {
- return _isDefaultShowGroups;
- }
- set
- {
- _isDefaultShowGroups = value;
- }
- }
- /// <summary>
- /// 默认显示分组
- /// </summary>
- public bool DefaultShowGroups = true;
- /// <summary>
- /// 默认不显示分组
- /// </summary>
- public bool DefaultNotShowGroups = false;
- const int WM_HSCROLL = 0x0114;
- const int WM_VSCROLL = 0x0115;
- /// <summary>
- /// 重写滚动条滚动事件
- /// </summary>
- /// <param name="m"></param>
- protected override void WndProc( ref Message m )
- {
- if ( m.Msg == WM_HSCROLL )
- {
- OnHScroll( this, new EventArgs() );
- }
- else if ( m.Msg == WM_VSCROLL )
- {
- OnVScroll( this, new EventArgs() );
- }
- base.WndProc( ref m );
- }
- /// <summary>
- /// 滚动条滚动事件
- /// </summary>
- public event EventHandler Scroll;
- /// <summary>
- /// 重写滚动条滚动事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- virtual protected void OnHScroll( object sender, EventArgs e )
- {
- if ( Scroll != null )
- Scroll( this, e );
- }
- /// <summary>
- /// 重写滚动条滚动事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- virtual protected void OnVScroll( object sender, EventArgs e )
- {
- if ( Scroll != null )
- Scroll( this, e );
- }
- /// <summary>
- /// 导出Excel
- /// </summary>
- public void ListViewToDataTable( string StrFileName = "" )
- {
- DataTable NewTable = new DataTable();
- for ( int i = 0; i < this.Columns.Count; i++ )
- {
- if ( this.Columns[i] == null || this.Columns[i].Text == "" )
- continue;
- NewTable.Columns.Add( this.Columns[i].Text.Trim(), typeof( string ) );
- }
- if ( this.Items.Count > 0 )
- {
- for ( int i = 0; i < this.Items.Count; i++ )
- {
- DataRow newRow = NewTable.NewRow();
- for ( int j = 0; j < this.Columns.Count; j++ )
- {
- if ( this.Columns[j] == null || this.Columns[j].Text == "" )
- continue;
- if ( j == 0 )
- {
- newRow[this.Columns[j].Text.Trim()] = this.Items[i].Text.ToString().Trim();
- }
- else
- {
- newRow[this.Columns[j].Text.Trim()] = this.Items[i].SubItems[this.Columns[j].Text.Trim()].Text.Trim();
- }
- }
- NewTable.Rows.Add( newRow );
- }
- }
- SaveFileDialog saveFile = new SaveFileDialog();
- saveFile.FileName = StrFileName;
- saveFile.Title = "导出数据报表文件";
- saveFile.Filter = "EXECL文件(*.xls) |*.xls |文本文件(*.txt) |*.txt |所有文件(*.*) |*.*";
- saveFile.FilterIndex = 1;
- DialogResult drst = saveFile.ShowDialog();
- if ( drst == DialogResult.No || drst == DialogResult.Cancel || drst == DialogResult.No || drst == DialogResult.Abort )
- {
- return;
- }
- LYFZ.ComponentLibrary.FrmLoadHandling.ExecutionDoWorkMethod(delegate (object obj, System.ComponentModel.BackgroundWorker backgroundWorker)
- {
- try
- {
- if (LYFZ.WinAPI.CustomPublicMethod.DataGridViewToExcel(saveFile.FileName.Trim(), NewTable, null, backgroundWorker))
- {
- MessageBoxCustom.Show("数据导出成功!");
- }
- else
- {
- MessageBoxCustom.Show("数据导出失败!");
- }
- }
- catch (Exception ex)
- {
- MessageBoxCustom.Show(String.Format("导出数据出错:{0}", ex.Message));
- }
- });
- }
- }
- }
|