using Newtonsoft.Json.Linq; using OfficeOpenXml; using OfficeOpenXml; using PTMedicalInsurance.Business; using PTMedicalInsurance.FormSetter; using PTMedicalInsurance.Helper; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; namespace PTMedicalInsurance.Forms { public partial class DRGGroupSelect : Form { private GridViewSetter grdSetter = new GridViewSetter(); HisMainBusiness hBus = new HisMainBusiness(); public DRGGroupSelect() { InitializeComponent(); } private void uiButton1_Click(object sender, EventArgs e) { string certno = uiTextBox1.Text; string begntime = uiDatePicker1.Value.ToString("yyyy-MM-dd"); string endtime = uiDatePicker2.Value.ToString("yyyy-MM-dd"); string page_num = uiTextBox2.Text; string page_size = uiTextBox3.Text; JObject Jo5560= new JObject(); JObject Jodata = new JObject(); Jodata.Add("certno", certno); Jodata.Add("begntime", begntime); Jodata.Add("endtime", endtime); Jodata.Add("page_num", page_num); Jodata.Add("page_size", page_size); Jo5560.Add("data",Jodata); InvokeHelper invoker = new InvokeHelper(); JObject joRtn = invoker.invokeCenterService(TradeEnum.DRGGroupSelect, Jo5560); JArray feedetail = JArray.Parse(joRtn["output"]["feedetail"].ToString()); DataTable dtData = (DataTable)feedetail.ToObject(typeof(DataTable)); uiDataGridView1.DataSource = dtData; grdSetter.SetHeaderTextOfDRGGroupSelect(uiDataGridView1); grdSetter.DatagridviewColumnWidthAdaptation(uiDataGridView1); } private void uiLabel4_Click(object sender, EventArgs e) { } private void uiButton2_Click(object sender, EventArgs e) { //Thread t = new Thread(DataToExcel); //t.IsBackground = true; ////t.ApartmentState = ApartmentState.STA;//缺少这句话,就会出错误。 ////不过以上这句在Vs 2010中显示是被微软否决的,过时的,虽然还能用。但是为了更好的兼容版本,微软推出以下方法: //t.SetApartmentState(ApartmentState.STA);  //即是在线程启动时设置它的单元状态,这里设置它的状态为单线程单元 //MessageBox.Show("66666"); //t.Start(); //MessageBox.Show("77777"); string filePath = @"D:\"+"DRG分组情况" + uiDatePicker1.Value.ToString("yyyy-MM")+ ".xlsx"; ExportDataGridViewToExcel(uiDataGridView1, filePath); } private void ExportDataGridViewToExcel(DataGridView dgv, string filePath) { using (var package = new ExcelPackage()) { // 添加一个sheet var worksheet = package.Workbook.Worksheets.Add("Sheet1"); // 将DataGridView列标题导出到Excel for (int i = 0; i < dgv.Columns.Count; i++) { worksheet.Cells[1, i + 1].Value = dgv.Columns[i].HeaderText; worksheet.Cells[1, i + 1].Style.Font.Bold = true; } // 将DataGridView数据导出到Excel for (int row = 0; row < dgv.RowCount; row++) { for (int col = 0; col < dgv.ColumnCount; col++) { worksheet.Cells[row + 2, col + 1].Value = dgv[col, row].Value; } // 添加进度条更新(可选) UpdateProgress(row, dgv.RowCount); } // 调整列宽以适应内容(可选) for (int col = 0; col < dgv.ColumnCount; col++) { worksheet.Column(col + 1).AutoFit(); } FileInfo fileInfo = new FileInfo(filePath); package.SaveAs(fileInfo); MessageBox.Show("导出成功!!!路径为:" + filePath); } } // 更新进度条的函数(可选) private void UpdateProgress(int current, int total) { int percentage = (int)(((double)current / (double)total) * 100); // 更新UI线程中的进度条 } } }