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.

ReportFormPage.cs 5.0 KiB

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