AIMS/AIMSControls/OperationAfter/frmReportExport.cs
2023-08-16 22:32:16 +08:00

347 lines
14 KiB
C#

using AIMS.OperationAanesthesia;
using AIMS.OperationAfter.UI;
using AIMSBLL;
using AIMSExtension;
using AIMSModel;
using Microsoft.Office.Interop.Excel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace AIMS
{
public partial class frmReportExport : Form
{
public SysConfig exportConfig;
public string DataGridViewPath = "";
public DataGridView selectdgv;
public frmReportExport()
{
InitializeComponent();
}
private void frmOperationDruggCheck_Load(object sender, EventArgs e)
{
try
{
dtpOpeTime.Value = DateTime.Now;
dtpEnd.Value = dtpOpeTime.Value;
dtpOpeTime2.Value = DateTime.Now;
dtpEnd2.Value = dtpOpeTime.Value;
}
catch (Exception exp)
{
PublicMethod.WriteLog(exp);
}
}
private void dgvtReport_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
//隔行换色
this.dgvtReport.RowsDefaultCellStyle.BackColor = System.Drawing.Color.White;//设置背景为白色
this.dgvtReport.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(212)))), ((int)(((byte)(242)))), (((int)(((byte)242)))));//青色
}
private static string GetControlPath(Control control)
{
StringBuilder controlPathStringBuilder = new StringBuilder();
controlPathStringBuilder.Append(control.Name);
while (control.Parent != null)
{
control = control.Parent;
controlPathStringBuilder.Append("." + control.Name);
}
return controlPathStringBuilder.ToString();
}
public ExportConfig dataGridViewSetting;
private void ConfigDataGridView(SysConfig exportConfig, DataGridView dgv)
{
try
{
dataGridViewSetting = JsonConvert.DeserializeObject<ExportConfig>(exportConfig.Value);
foreach (DataGridViewColumn column in dgv.Columns)
{
foreach (var item in dataGridViewSetting.Exports)
{
if (column.Name == item.Name)
{
column.Visible = item.IsVisible;
column.Width = item.Width;
}
}
}
}
catch (Exception ex)
{
PublicMethod.WriteLog(ex);
}
}
private void tspSetting_Click(object sender, EventArgs e)
{
frmExportConfig formDataGridViewConfig = new frmExportConfig(selectdgv, exportConfig, DataGridViewPath, this.Text);
if (formDataGridViewConfig.ShowDialog() == DialogResult.OK)
{
exportConfig = formDataGridViewConfig.exportConfig;
ConfigDataGridView(formDataGridViewConfig.exportConfig, selectdgv);
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
//DataToExcel(dgv);
KillAllExcel();
ExprotExcel2();
}
private Microsoft.Office.Interop.Excel.Application myExcel = null;
private void ExprotExcel2(bool isPrint = false)
{
//自定义表头
string title = "查询管理数据";
if (dataGridViewSetting != null) title = dataGridViewSetting.Title;
//是否横向
bool xlLandscape = true;
if (dataGridViewSetting != null && dataGridViewSetting.Landscape == false) xlLandscape = false;
//主体字体大小
int BodySize = 9;
if (dataGridViewSetting != null && dataGridViewSetting.FontSize > 0) BodySize = dataGridViewSetting.FontSize;
myExcel = new Microsoft.Office.Interop.Excel.Application();
this.Cursor = Cursors.WaitCursor;
//保存文化环境
System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Workbook workbookData = myExcel.Workbooks.Add(Missing.Value);
Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbookData.Worksheets[1];
xlSheet.Name = title;
if (isPrint == false)
myExcel.Visible = true;
myExcel.Cells[1, 1] = title;//默认的就是在sheet1里面的。
WorksheetClass wsClass = new WorksheetClass();
Dictionary<int, string> Columns = new Dictionary<int, string>();
//填充标题
for (int i = 0; i < selectdgv.Columns.Count; i++)
{
if (dataGridViewSetting != null)
{
foreach (var item in dataGridViewSetting.Exports)
{
if (selectdgv.Columns[i].Name == item.Name)
{
if (item.IsPrint == true)
{
Columns.Add(i, selectdgv.Columns[i].HeaderText);
continue;
}
}
}
}
else
{
if (selectdgv.Columns[i].Visible == true)
Columns.Add(i, selectdgv.Columns[i].HeaderText);
}
}
List<KeyValuePair<int, string>> ColumnsStr = Columns.ToList();
for (int i = 0; i < ColumnsStr.Count; i++)
{
myExcel.Cells[2, i + 1] = ColumnsStr[i].Value;
if (dataGridViewSetting != null)
{
foreach (var item in dataGridViewSetting.Exports)
{
if (ColumnsStr[i].Value == item.Text)
{
xlSheet.get_Range(xlSheet.Cells[2, i + 1], xlSheet.Cells[2, i + 1]).ColumnWidth = item.PrintWidth;//列宽
}
}
}
else
{
xlSheet.get_Range(xlSheet.Cells[2, i + 1], xlSheet.Cells[2, i + 1]).ColumnWidth = 5;//列宽
}
}
//获得数据表的值
int RowCount = selectdgv.Rows.Count;
int colCount = ColumnsStr.Count;
//设置填充单元格样式
xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[RowCount + 2, colCount]).Borders.LineStyle = XlLineStyle.xlContinuous;//边框的样式
object[,] objData = new object[RowCount, colCount];
//将DataTable里的值填充到对象数组中
for (int i = 0; i < RowCount; i++)
{
for (int j = 0; j < colCount; j++)
{
if (selectdgv.Columns[ColumnsStr[j].Key].Name == "序号" || selectdgv.Columns[ColumnsStr[j].Key].Name == "ApplyId")
objData[i, j] = i + 1;
else if (selectdgv.Rows[i].Cells[ColumnsStr[j].Key].Value != null)
objData[i, j] = selectdgv.Rows[i].Cells[ColumnsStr[j].Key].Value.ToString();
}
System.Windows.Forms.Application.DoEvents();
}
//将对象数组的值赋值给Excel
Range range = xlSheet.get_Range(xlSheet.Cells[3, 1], xlSheet.Cells[RowCount + 2, colCount]);
range.Value2 = objData;
//xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[RowCount + 2, colCount]).Font.Size = BodySize;//字体大小
//设置主标题单元格的样式
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).MergeCells = true;//合并单元格
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).HorizontalAlignment = XlHAlign.xlHAlignCenter;//水平对齐方式
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).VerticalAlignment = XlVAlign.xlVAlignBottom;//垂直对齐方式
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).Font.Bold = true;//字体加粗
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).Font.ColorIndex = 0;//字体颜色
//xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).Font.Italic = true;//是否斜体
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).Font.Size = 22;//字体大小
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).Borders.LineStyle = XlLineStyle.xlContinuous;//边框样式
xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1, colCount]).RowHeight = 33.75;//行高
//设置标题单元格样式
xlSheet.get_Range(xlSheet.Cells[2, 1], xlSheet.Cells[2, colCount]).RowHeight = 22.75;//行高
if (xlLandscape == true)
xlSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;//页面方向为横向
//复苏文化环境
System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
try
{
if (isPrint == true)
xlSheet.PrintOutEx();
//myExcel.Save(@"D:a.xls");
//myExcel.Quit();
this.Cursor = Cursors.Default;
//MessageBox.Show("导出成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
finally
{
if (isPrint == true)
{
//释放资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbookData);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
GC.Collect();
range = null;
xlSheet = null;
workbookData = null;
myExcel = null;
KillAllExcel();
}
}
}
private void KillAllExcel()
{
List<Process> excelProcess = GetExcelProcesses();
for (int i = 0; i < excelProcess.Count; i++)
{
excelProcess[i].Kill();
}
}
private List<Process> GetExcelProcesses()
{
Process[] processes = Process.GetProcesses();
List<Process> excelProcesses = new List<Process>();
for (int i = 0; i < processes.Length; i++)
{
if (processes[i].ProcessName.ToUpper() == "EXCEL")
excelProcesses.Add(processes[i]);
}
return excelProcesses;
}
private void btn手术例数按月统计_Click(object sender, EventArgs e)
{
selectdgv = dgvtReport;
ControlExtension.SetDgvAttribute(selectdgv);
DataGridViewPath = GetControlPath(selectdgv);
exportConfig = BSysConfig.SelectSingle(" Note='" + DataGridViewPath + "'", null, RecursiveType.None, 0);
string beginTime = Convert.ToDateTime(dtpOpeTime.Value).ToString("yyyy-MM-dd 00:00:00");
string endTime = Convert.ToDateTime(dtpEnd.Value).ToString("yyyy-MM-dd 23:59:59");
System.Data.DataTable MonthDay = BOperationReport.GetMonthDay();
MonthDay.Rows.Clear();
System.Data.DataTable dataResult = BOperationReport.Get手术例数按月统计(beginTime, endTime);
List<string> Months = new List<string>();
foreach (DataRow row in dataResult.Rows)
{
string month = row["OpeMonth"].ToString();
if (!Months.Contains(month))
{
Months.Add(month);
}
}
foreach (string month in Months)
{
DataRow row = MonthDay.NewRow();
row["月份"] = month;
MonthDay.Rows.Add(row);
}
foreach (DataRow month in MonthDay.Rows)
{
foreach (DataRow row in dataResult.Rows)
{
if (month[0].ToString() == row["OpeMonth"].ToString())
{
string day = row["OpeDay"].ToString();
string num = row["num"].ToString();
month[DateTime.Parse(day).Day] = num;
}
}
}
foreach (DataRow month in MonthDay.Rows)
{
int SumValue = 0;
foreach (DataColumn item in MonthDay.Columns)
{
if (item.ColumnName != "月份" && month[item].ToString() != "")
{
SumValue += int.Parse(month[item].ToString());
}
}
month["合计"] = SumValue;
}
selectdgv.DataSource = MonthDay;
if (exportConfig != null)
ConfigDataGridView(exportConfig, selectdgv);
}
private void btn麻醉专业医疗质控指标_Click(object sender, EventArgs e)
{
selectdgv = dgvtReport2;
ControlExtension.SetDgvAttribute(selectdgv);
DataGridViewPath = GetControlPath(selectdgv);
exportConfig = BSysConfig.SelectSingle(" Note='" + DataGridViewPath + "'", null, RecursiveType.None, 0);
string beginTime = Convert.ToDateTime(dtpOpeTime2.Value).ToString("yyyy-MM-dd 00:00:00");
string endTime = Convert.ToDateTime(dtpEnd2.Value).ToString("yyyy-MM-dd 23:59:59");
System.Data.DataTable dataResult = BOperationReport.Get麻醉专业医疗质控指标(beginTime, endTime);
selectdgv.DataSource = dataResult;
if (exportConfig != null)
ConfigDataGridView(exportConfig, selectdgv);
}
}
}