DRGGroupSelect.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Newtonsoft.Json.Linq;
  2. using OfficeOpenXml;
  3. using OfficeOpenXml;
  4. using PTMedicalInsurance.Business;
  5. using PTMedicalInsurance.FormSetter;
  6. using PTMedicalInsurance.Helper;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. using System.Xml.Linq;
  19. namespace PTMedicalInsurance.Forms
  20. {
  21. public partial class DRGGroupSelect : Form
  22. {
  23. private GridViewSetter grdSetter = new GridViewSetter();
  24. HisMainBusiness hBus = new HisMainBusiness();
  25. public DRGGroupSelect()
  26. {
  27. InitializeComponent();
  28. }
  29. private void uiButton1_Click(object sender, EventArgs e)
  30. {
  31. string certno = uiTextBox1.Text;
  32. string begntime = uiDatePicker1.Value.ToString("yyyy-MM-dd");
  33. string endtime = uiDatePicker2.Value.ToString("yyyy-MM-dd");
  34. string page_num = uiTextBox2.Text;
  35. string page_size = uiTextBox3.Text;
  36. JObject Jo5560= new JObject();
  37. JObject Jodata = new JObject();
  38. Jodata.Add("certno", certno);
  39. Jodata.Add("begntime", begntime);
  40. Jodata.Add("endtime", endtime);
  41. Jodata.Add("page_num", page_num);
  42. Jodata.Add("page_size", page_size);
  43. Jo5560.Add("data",Jodata);
  44. InvokeHelper invoker = new InvokeHelper();
  45. JObject joRtn = invoker.invokeCenterService(TradeEnum.DRGGroupSelect, Jo5560);
  46. JArray feedetail = JArray.Parse(joRtn["output"]["feedetail"].ToString());
  47. DataTable dtData = (DataTable)feedetail.ToObject(typeof(DataTable));
  48. uiDataGridView1.DataSource = dtData;
  49. grdSetter.SetHeaderTextOfDRGGroupSelect(uiDataGridView1);
  50. grdSetter.DatagridviewColumnWidthAdaptation(uiDataGridView1);
  51. }
  52. private void uiLabel4_Click(object sender, EventArgs e)
  53. {
  54. }
  55. private void uiButton2_Click(object sender, EventArgs e)
  56. {
  57. //Thread t = new Thread(DataToExcel);
  58. //t.IsBackground = true;
  59. ////t.ApartmentState = ApartmentState.STA;//缺少这句话,就会出错误。
  60. ////不过以上这句在Vs 2010中显示是被微软否决的,过时的,虽然还能用。但是为了更好的兼容版本,微软推出以下方法:
  61. //t.SetApartmentState(ApartmentState.STA);  //即是在线程启动时设置它的单元状态,这里设置它的状态为单线程单元
  62. //MessageBox.Show("66666");
  63. //t.Start();
  64. //MessageBox.Show("77777");
  65. string filePath = @"D:\"+"DRG分组情况" + uiDatePicker1.Value.ToString("yyyy-MM")+ ".xlsx";
  66. ExportDataGridViewToExcel(uiDataGridView1, filePath);
  67. }
  68. private void ExportDataGridViewToExcel(DataGridView dgv, string filePath)
  69. {
  70. using (var package = new ExcelPackage())
  71. {
  72. // 添加一个sheet
  73. var worksheet = package.Workbook.Worksheets.Add("Sheet1");
  74. // 将DataGridView列标题导出到Excel
  75. for (int i = 0; i < dgv.Columns.Count; i++)
  76. {
  77. worksheet.Cells[1, i + 1].Value = dgv.Columns[i].HeaderText;
  78. worksheet.Cells[1, i + 1].Style.Font.Bold = true;
  79. }
  80. // 将DataGridView数据导出到Excel
  81. for (int row = 0; row < dgv.RowCount; row++)
  82. {
  83. for (int col = 0; col < dgv.ColumnCount; col++)
  84. {
  85. worksheet.Cells[row + 2, col + 1].Value = dgv[col, row].Value;
  86. }
  87. // 添加进度条更新(可选)
  88. UpdateProgress(row, dgv.RowCount);
  89. }
  90. // 调整列宽以适应内容(可选)
  91. for (int col = 0; col < dgv.ColumnCount; col++)
  92. {
  93. worksheet.Column(col + 1).AutoFit();
  94. }
  95. FileInfo fileInfo = new FileInfo(filePath);
  96. package.SaveAs(fileInfo);
  97. MessageBox.Show("导出成功!!!路径为:" + filePath);
  98. }
  99. }
  100. // 更新进度条的函数(可选)
  101. private void UpdateProgress(int current, int total)
  102. {
  103. int percentage = (int)(((double)current / (double)total) * 100);
  104. // 更新UI线程中的进度条
  105. }
  106. }
  107. }