|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- 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;
- DataGridViewInit();
-
- }
-
- private void DataGridViewInit()
- {
- dataGridView1.AllowUserToResizeRows = false;
- dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
- dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
- }
-
- 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 && !string.IsNullOrEmpty(TableName.Rows[i].Cells[j].Value.ToString()))
- 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<ReportCountDto> reportCountDtos = new List<ReportCountDto>();
-
- //查询
- 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.Columns[0].Visible = false;
- 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)
- {
- try
- {
- this.Invoke(() => { button2.Enabled = false; });
- ExportDataToExcel(dataGridView1, this.Tag.ToString());
- }
- catch (Exception ex)
- {
- MessageLogNotify.GetInstance.ShowEx(ex.ToString());
- }
- finally
- {
- this.Invoke(() => { button2.Enabled = true; });
- }
- }
- }
- }
|