Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

146 righe
6.0 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. #region 禁止更改宽高
  23. // 禁止用户改变DataGridView的所有列的列宽
  24. //dataGridView1.AllowUserToResizeColumns = false;
  25. //禁止用户改变DataGridView所有行的行高
  26. dataGridView1.AllowUserToResizeRows = false;
  27. // 禁止用户改变列头的高度
  28. dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  29. // 禁止用户改变列头的宽度
  30. dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
  31. #endregion
  32. }
  33. private void ExportDataToExcel(DataGridView TableName, string FileName)
  34. {
  35. SaveFileDialog saveFileDialog = new SaveFileDialog();
  36. saveFileDialog.Title = "导出Excel文件"; //设置文件标题
  37. saveFileDialog.Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls"; //设置文件类型
  38. saveFileDialog.FilterIndex = 1; //设置默认文件类型显示顺序
  39. saveFileDialog.AddExtension = true; //是否自动在文件名中添加扩展名
  40. saveFileDialog.RestoreDirectory = true; //是否记忆上次打开的目录
  41. saveFileDialog.FileName = FileName; //设置默认文件名
  42. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  43. {
  44. string localFilePath = saveFileDialog.FileName.ToString();
  45. //数据初始化
  46. int TotalCount; //总行数
  47. int RowRead = 0; //已读行数
  48. int Percent = 0; //百分比
  49. TotalCount = TableName.Rows.Count;
  50. Stream myStream = saveFileDialog.OpenFile();
  51. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
  52. StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding("gb2312"));
  53. string strHeader = "";
  54. //秒钟
  55. Stopwatch timer = new Stopwatch();
  56. timer.Start();
  57. try
  58. {
  59. //写入标题
  60. for (int i = 0; i < TableName.Columns.Count; i++)
  61. {
  62. if (i > 0)
  63. {
  64. strHeader += "\t";
  65. }
  66. strHeader += TableName.Columns[i].HeaderText.ToString();
  67. }
  68. sw.WriteLine(strHeader);
  69. for (int i = 0; i < TableName.Rows.Count; i++)
  70. {
  71. RowRead++;
  72. Percent = (int)(100 * RowRead / TotalCount);
  73. Application.DoEvents();
  74. string strData = "";
  75. for (int j = 0; j < TableName.Columns.Count; j++)
  76. {
  77. if (j > 0) strData += "\t";
  78. if (TableName.Columns[j].Visible)
  79. strData += TableName.Rows[i].Cells[j].Value.ToString();
  80. }
  81. sw.WriteLine(strData);
  82. }
  83. sw.Close();
  84. myStream.Close();
  85. timer.Reset();
  86. timer.Stop();
  87. }
  88. catch (Exception ex)
  89. {
  90. MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  91. }
  92. finally
  93. {
  94. sw.Close();
  95. myStream.Close();
  96. timer.Stop();
  97. }
  98. MessageLogNotify.GetInstance.Show("数据导出成功");
  99. }
  100. }
  101. List<ReportCountDto> reportCountDtos = new List<ReportCountDto>();
  102. private void button1_Click(object sender, EventArgs e)
  103. {
  104. string TagName = this.Tag.ToString();
  105. int type = 0;
  106. if (TagName == "月餐段汇总表") type = 2;
  107. else if (TagName == "记次就餐天消费表") type = 1;
  108. else if (TagName == "记次就餐消费明细表") type = 3;
  109. Task.Factory.StartNew(() =>
  110. {
  111. this.Invoke(() => { button1.Enabled = false; });
  112. var res = HKLibHelper.Report(new ReportDto() { StartTime = dateTimePicker1.Value, EndTime = dateTimePicker2.Value }, type);
  113. if (res != null)
  114. {
  115. if (res.Count > 0)
  116. {
  117. reportCountDtos = res;
  118. this.Invoke(() => { dataGridView1.DataSource = reportCountDtos; button2.Enabled = true; });
  119. MessageLogNotify.GetInstance.Show("获取表数据成功");
  120. }
  121. else if (res.Count <= 0)
  122. {
  123. MessageLogNotify.GetInstance.ShowWarning("未查询到数据");
  124. }
  125. }
  126. else
  127. {
  128. MessageLogNotify.GetInstance.ShowError("查询失败,请输入正确的时间段");
  129. }
  130. this.Invoke(() => { button1.Enabled = true; });
  131. });
  132. }
  133. private void button2_Click(object sender, EventArgs e)
  134. {
  135. this.Invoke(() => { button2.Enabled = false; });
  136. ExportDataToExcel(dataGridView1, this.Tag.ToString());
  137. this.Invoke(() => { button2.Enabled = true; });
  138. }
  139. }
  140. }