using HKLib.Dto; using HKLib.Interfaces; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace HKCardManager.UserPages { public partial class ReportFormPage : UserControl { public ReportFormPage() { InitializeComponent(); dataGridView1.AutoGenerateColumns = false; button2.Enabled = false; #region 禁止更改宽高 // 禁止用户改变DataGridView的所有列的列宽 //dataGridView1.AllowUserToResizeColumns = false; //禁止用户改变DataGridView所有行的行高 dataGridView1.AllowUserToResizeRows = false; // 禁止用户改变列头的高度 dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; // 禁止用户改变列头的宽度 dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing; #endregion } private void ExportDataToExcel(DataGridView TableName, string FileName) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "导出Excel文件"; //设置文件标题 saveFileDialog.Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls"; //设置文件类型 saveFileDialog.FilterIndex = 1; //设置默认文件类型显示顺序 saveFileDialog.AddExtension = true; //是否自动在文件名中添加扩展名 saveFileDialog.RestoreDirectory = true; //是否记忆上次打开的目录 saveFileDialog.FileName = FileName; //设置默认文件名 if (saveFileDialog.ShowDialog() == DialogResult.OK) { string localFilePath = saveFileDialog.FileName.ToString(); //数据初始化 int TotalCount; //总行数 int RowRead = 0; //已读行数 int Percent = 0; //百分比 TotalCount = TableName.Rows.Count; Stream myStream = saveFileDialog.OpenFile(); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding("gb2312")); string strHeader = ""; //秒钟 Stopwatch timer = new Stopwatch(); timer.Start(); try { //写入标题 for (int i = 0; i < TableName.Columns.Count; i++) { if (i > 0) { strHeader += "\t"; } strHeader += TableName.Columns[i].HeaderText.ToString(); } sw.WriteLine(strHeader); for (int i = 0; i < TableName.Rows.Count; i++) { RowRead++; Percent = (int)(100 * RowRead / TotalCount); Application.DoEvents(); string strData = ""; for (int j = 0; j < TableName.Columns.Count; j++) { if (j > 0) strData += "\t"; if (TableName.Columns[j].Visible) strData += TableName.Rows[i].Cells[j].Value.ToString(); } sw.WriteLine(strData); } sw.Close(); myStream.Close(); timer.Reset(); timer.Stop(); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { sw.Close(); myStream.Close(); timer.Stop(); } MessageLogNotify.GetInstance.Show("数据导出成功"); } } List reportCountDtos = new List(); private void button1_Click(object sender, EventArgs e) { string TagName = this.Tag.ToString(); int type = 0; if (TagName == "月餐段汇总表") type = 2; else if (TagName == "记次就餐天消费表") type = 1; else if (TagName == "记次就餐消费明细表") type = 3; Task.Factory.StartNew(() => { this.Invoke(() => { button1.Enabled = false; }); var res = HKLibHelper.Report(new ReportDto() { StartTime = dateTimePicker1.Value, EndTime = dateTimePicker2.Value }, type); if (res != null) { if (res.Count > 0) { reportCountDtos = res; this.Invoke(() => { dataGridView1.DataSource = reportCountDtos; button2.Enabled = true; }); MessageLogNotify.GetInstance.Show("获取表数据成功"); } else if (res.Count <= 0) { MessageLogNotify.GetInstance.ShowWarning("未查询到数据"); } } else { MessageLogNotify.GetInstance.ShowError("查询失败,请输入正确的时间段"); } this.Invoke(() => { button1.Enabled = true; }); }); } private void button2_Click(object sender, EventArgs e) { this.Invoke(() => { button2.Enabled = false; }); ExportDataToExcel(dataGridView1, this.Tag.ToString()); this.Invoke(() => { button2.Enabled = true; }); } } }