No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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