You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.3 KiB

  1. using HKLib.Dto;
  2. using HKLib.Interfaces;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace HKCardManager.UserPages
  14. {
  15. public partial class ReportFormPage : UserControl
  16. {
  17. public ReportFormPage()
  18. {
  19. InitializeComponent();
  20. dataGridView1.AutoGenerateColumns = false;
  21. button2.Enabled = false;
  22. DataGridViewInit();
  23. }
  24. private void DataGridViewInit()
  25. {
  26. dataGridView1.AllowUserToResizeRows = false;
  27. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  28. dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
  29. }
  30. private void ExportDataToExcel(DataGridView TableName, string FileName)
  31. {
  32. SaveFileDialog saveFileDialog = new SaveFileDialog();
  33. saveFileDialog.Title = "导出Excel文件"; //设置文件标题
  34. saveFileDialog.Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls"; //设置文件类型
  35. saveFileDialog.FilterIndex = 1; //设置默认文件类型显示顺序
  36. saveFileDialog.AddExtension = true; //是否自动在文件名中添加扩展名
  37. saveFileDialog.RestoreDirectory = true; //是否记忆上次打开的目录
  38. saveFileDialog.FileName = FileName; //设置默认文件名
  39. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  40. {
  41. string localFilePath = saveFileDialog.FileName.ToString();
  42. //数据初始化
  43. int TotalCount; //总行数
  44. int RowRead = 0; //已读行数
  45. int Percent = 0; //百分比
  46. TotalCount = TableName.Rows.Count;
  47. Stream myStream = saveFileDialog.OpenFile();
  48. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  49. StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding("gb2312"));
  50. string strHeader = "";
  51. //秒钟
  52. Stopwatch timer = new Stopwatch();
  53. timer.Start();
  54. try
  55. {
  56. //写入标题
  57. for (int i = 0; i < TableName.Columns.Count; i++)
  58. {
  59. if (i > 0)
  60. {
  61. strHeader += "\t";
  62. }
  63. strHeader += TableName.Columns[i].HeaderText.ToString();
  64. }
  65. sw.WriteLine(strHeader);
  66. for (int i = 0; i < TableName.Rows.Count; i++)
  67. {
  68. RowRead++;
  69. Percent = (int)(100 * RowRead / TotalCount);
  70. Application.DoEvents();
  71. string strData = "";
  72. for (int j = 0; j < TableName.Columns.Count; j++)
  73. {
  74. if (j > 0) strData += "\t";
  75. if (TableName.Columns[j].Visible && !string.IsNullOrEmpty(TableName.Rows[i].Cells[j].Value.ToString()))
  76. strData += TableName.Rows[i].Cells[j].Value.ToString();
  77. }
  78. sw.WriteLine(strData);
  79. }
  80. sw.Close();
  81. myStream.Close();
  82. timer.Reset();
  83. timer.Stop();
  84. }
  85. catch (Exception ex)
  86. {
  87. MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  88. }
  89. finally
  90. {
  91. sw.Close();
  92. myStream.Close();
  93. timer.Stop();
  94. }
  95. MessageLogNotify.GetInstance.Show("数据导出成功");
  96. }
  97. }
  98. List<ReportCountDto> reportCountDtos = new List<ReportCountDto>();
  99. //查询
  100. private void button1_Click(object sender, EventArgs e)
  101. {
  102. string TagName = this.Tag.ToString();
  103. int type = 0;
  104. if (TagName == "月餐段汇总表") type = 2;
  105. else if (TagName == "记次就餐天消费表") type = 1;
  106. else if (TagName == "记次就餐消费明细表") type = 3;
  107. Task.Factory.StartNew(() =>
  108. {
  109. this.Invoke(() => { button1.Enabled = false; });
  110. var res = HKLibHelper.Report(new ReportDto() { StartTime = dateTimePicker1.Value, EndTime = dateTimePicker2.Value }, type);
  111. if (res != null)
  112. {
  113. if (res.Count > 0)
  114. {
  115. reportCountDtos = res;
  116. this.Invoke(() =>
  117. {
  118. dataGridView1.Columns[0].Visible = false;
  119. dataGridView1.DataSource = reportCountDtos;
  120. button2.Enabled = true;
  121. });
  122. MessageLogNotify.GetInstance.Show("获取表数据成功");
  123. }
  124. else if (res.Count <= 0)
  125. {
  126. MessageLogNotify.GetInstance.ShowWarning("未查询到数据");
  127. }
  128. }
  129. else
  130. {
  131. MessageLogNotify.GetInstance.ShowError("查询失败,请输入正确的时间段");
  132. }
  133. this.Invoke(() => { button1.Enabled = true; });
  134. });
  135. }
  136. //导出
  137. private void button2_Click(object sender, EventArgs e)
  138. {
  139. try
  140. {
  141. this.Invoke(() => { button2.Enabled = false; });
  142. ExportDataToExcel(dataGridView1, this.Tag.ToString());
  143. }
  144. catch (Exception ex)
  145. {
  146. MessageLogNotify.GetInstance.ShowEx(ex.ToString());
  147. }
  148. finally
  149. {
  150. this.Invoke(() => { button2.Enabled = true; });
  151. }
  152. }
  153. }
  154. }