StableVersion4.3/HL_FristAidPlatform_Public/Helpers/ExcelHelper.cs

305 lines
12 KiB
C#

using HL_FristAidPlatform_DTO;
using Microsoft.Office.Interop.Excel;
using Newtonsoft.Json;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace HL_FristAidPlatform_Public
{
/// <summary>
/// 导出excel帮助类
/// </summary>
public static class ExcelHelper
{
/// <summary>
/// 卒中EXCEL导出
/// </summary>
/// <param name="hospitalGuid"></param>
/// <param name="patientList"></param>
/// <returns></returns>
public static Boolean OutputApoplexyData(string hospitalGuid, string month, string patientList)
{
if (string.IsNullOrEmpty(hospitalGuid) || string.IsNullOrEmpty(month) || string.IsNullOrEmpty(patientList))
{
return false;
}
else
{
string ListData = JsonConvert.SerializeObject(patientList);
// 获取数据 api/service/T_Service_Apoplexy_UploadInfo/GetApoplexyOutputList
string url = string.Format("api/service/T_Service_Apoplexy_UploadInfo/GetApoplexyOutputList?hospitalGUID={0}&patientGuidList={1}", hospitalGuid, ListData);
ApoplexyOutputDTO apoplexyOutput = DBHelpClass.GetDateModel<ApoplexyOutputDTO>(url);
if (apoplexyOutput == null)
{
return false;
}
else
{
if (apoplexyOutput.list.Count > 0)
{
EPPlusEXCEL(month, apoplexyOutput);
return true;
}
else
{
return false;
}
}
}
}
public static void EPPlusEXCEL(string month, ApoplexyOutputDTO apoplexyOutput)
{
//加载非商业化证书
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
//加载这个EXCEl文件
ExcelPackage package = new ExcelPackage();
//添加一个sheet表
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("病历信息");
//起始行
int rowIndex = 1;
//起始列
int colIndex = 1;
worksheet.Cells["A1"].Value = apoplexyOutput.title1[0];
worksheet.Cells["E1"].Value = apoplexyOutput.title1[1];
worksheet.Cells["N1"].Value = apoplexyOutput.title1[2];
worksheet.Cells["U1"].Value = apoplexyOutput.title1[3];
worksheet.Cells["W1"].Value = apoplexyOutput.title1[4];
worksheet.Cells["BA1"].Value = apoplexyOutput.title1[5];
worksheet.Cells["BS1"].Value = apoplexyOutput.title1[6];
worksheet.Cells["BT1"].Value = apoplexyOutput.title1[7];
worksheet.Cells["CU1"].Value = apoplexyOutput.title1[8];
worksheet.Cells["CW1"].Value = apoplexyOutput.title1[9];
worksheet.Cells["CY1"].Value = apoplexyOutput.title1[10];
worksheet.Cells["DQ1"].Value = apoplexyOutput.title1[11];
worksheet.Cells["EJ1"].Value = apoplexyOutput.title1[12];
worksheet.Cells["EN1"].Value = apoplexyOutput.title1[13];
worksheet.Cells["EW1"].Value = apoplexyOutput.title1[14];
worksheet.Cells["EZ1"].Value = apoplexyOutput.title1[15];
worksheet.Cells["KT1"].Value = apoplexyOutput.title1[16];
worksheet.Cells["LA1"].Value = apoplexyOutput.title1[17];
worksheet.Cells["LG1"].Value = apoplexyOutput.title1[18];
worksheet.Cells["LI1"].Value = apoplexyOutput.title1[19];
worksheet.Cells["LM1"].Value = apoplexyOutput.title1[20];
worksheet.Cells["ME1"].Value = apoplexyOutput.title1[21];
rowIndex++;
for (int i = 1; i <= apoplexyOutput.title2.Count; i++)
{
worksheet.Cells[rowIndex, i].Value = apoplexyOutput.title2[i - 1] + "";
var cell = worksheet.Cells[rowIndex, i];
cell.Style.Font.Size = 12;
// 设置单元格边框样式和颜色
cell.Style.Border.Top.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Left.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Right.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Top.Color.SetColor(Color.Black);
cell.Style.Border.Bottom.Color.SetColor(Color.Black);
cell.Style.Border.Left.Color.SetColor(Color.Black);
cell.Style.Border.Right.Color.SetColor(Color.Black);
}
rowIndex++;
for (int i = 0; i < apoplexyOutput.list.Count; i++)
{
for (int j = 0; j < apoplexyOutput.list[i].Count; j++)
{
worksheet.Cells[rowIndex, j + 1].Value = apoplexyOutput.list[i][j] + "";
}
rowIndex++;
}
//执行合并需要合并的单元格
worksheet.Cells["A1:D1"].Merge = true;
worksheet.Cells["E1:M1"].Merge = true;
worksheet.Cells["N1:T1"].Merge = true;
worksheet.Cells["U1:V1"].Merge = true;
worksheet.Cells["W1:AZ1"].Merge = true;
worksheet.Cells["BA1:BR1"].Merge = true;
worksheet.Cells["BT1:CT1"].Merge = true;
worksheet.Cells["CU1:CV1"].Merge = true;
worksheet.Cells["CW1:CX1"].Merge = true;
worksheet.Cells["CY1:DP1"].Merge = true;
worksheet.Cells["DQ1:EI1"].Merge = true;
worksheet.Cells["EJ1:EM1"].Merge = true;
worksheet.Cells["EN1:EV1"].Merge = true;
worksheet.Cells["EW1:EY1"].Merge = true;
worksheet.Cells["EZ1:KS1"].Merge = true;
worksheet.Cells["KT1:KZ1"].Merge = true;
worksheet.Cells["LA1:LF1"].Merge = true;
worksheet.Cells["LG1:LH1"].Merge = true;
worksheet.Cells["LI1:LL1"].Merge = true;
worksheet.Cells["LM1:MD1"].Merge = true;
worksheet.Cells["ME1:MS1"].Merge = true;
//自动调整列宽
worksheet.Row(1).Height = 20;
var range = worksheet.Cells["A1:" + GetColumnLetter(worksheet.Dimension.Columns) + "1"];
// 设置样式
range.Style.Font.Size = 14;
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
worksheet.Columns.AutoFit();
worksheet.Cells.Style.ShrinkToFit = true;
month = month.Replace("-", "");
string outnameExcel = "卒中出院病历数据" + month + ".xlsx";
string outoutpath = PublicClassForDataBase.Config10017 + "//" + PublicClassForDataBase.Config10016 + "//";
string outpathDirectory = outoutpath != "" ? outoutpath : System.Windows.Forms.Application.StartupPath.Split('\\')[0] + "\\OutputExcel\\";
//如果文件夹信息没有就新建
if (!Directory.Exists(outpathDirectory))
{
Directory.CreateDirectory(outpathDirectory);
}
//保存文件信息
Stream fileStream = new FileStream(outpathDirectory + outnameExcel, FileMode.Create);
package.SaveAs(fileStream);
outoutpath = PublicClassForDataBase.Config10017 + "\\" + PublicClassForDataBase.Config10016 + "\\";
Process.Start("explorer.exe", outoutpath);// + outnameExcel
fileStream.Close();
fileStream.Dispose();
worksheet.Dispose();
package.Dispose();
GC.Collect();
}
/// <summary>
/// 新邵县人民医院需求 胸痛患者工作量表统计EXCEL导出
/// </summary>
/// <param name="hospitalGuid"></param>
/// <param name="patientList"></param>
/// <returns></returns>
public static Boolean MedicalStaffWorkloadStatistics(MedicalStaffWorkloadListDTO model, string nameExcel)
{
if (model.list == null || !(model.list.Count > 0))
{
return false;
}
else
{
MedicalStaffWorkloadToExcel(model, nameExcel);
return true;
}
}
public static void MedicalStaffWorkloadToExcel(MedicalStaffWorkloadListDTO model, string nameExcel)
{
//加载非商业化证书
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
//加载这个EXCEl文件
ExcelPackage package = new ExcelPackage();
//添加一个sheet表
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("工作量表");
worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
//起始行
int rowIndex = 1;
//起始列
int colIndex = 1;
for (int i = 1; i <= model.TitleList.Count; i++)
{
worksheet.Cells[rowIndex, i].Value = model.TitleList[i - 1] + "";
var cell = worksheet.Cells[rowIndex, i];
cell.Style.Font.Size = 12;
// 设置单元格边框样式和颜色
cell.Style.Border.Top.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Left.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Right.Style = ExcelBorderStyle.Thin;
cell.Style.Border.Top.Color.SetColor(Color.Black);
cell.Style.Border.Bottom.Color.SetColor(Color.Black);
cell.Style.Border.Left.Color.SetColor(Color.Black);
cell.Style.Border.Right.Color.SetColor(Color.Black);
}
rowIndex++;
for (int i = 0; i < model.list.Count; i++)
{
for (int j = 0; j < model.list[i].Count; j++)
{
worksheet.Cells[rowIndex, j + 1].Value = model.list[i][j] + "";
}
rowIndex++;
}
//自动调整列宽
worksheet.Row(1).Height = 20;
var range = worksheet.Cells["A1:" + GetColumnLetter(worksheet.Dimension.Columns) + "1"];
// 设置样式
range.Style.Font.Size = 14;
range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
worksheet.Columns.AutoFit();
worksheet.Cells.Style.ShrinkToFit = true;
DateTime dt = DateTime.Now;
string outnameExcel = nameExcel + dt.Year+"年"+dt.Month+"月"+dt.Day+"日"+dt.Hour+"时"+dt.Minute+"分" + ".xlsx";
//string outnameExcel = nameExcel + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + ".xlsx";
string outpathDirectory = System.Windows.Forms.Application.StartupPath.Split('\\')[0] + "\\OutputExcel\\";
//如果文件夹信息没有就新建
if (!Directory.Exists(outpathDirectory))
{
Directory.CreateDirectory(outpathDirectory);
}
//保存文件信息
Stream fileStream = new FileStream(outpathDirectory + outnameExcel, FileMode.Create);
package.SaveAs(fileStream);
Process.Start("explorer.exe", outpathDirectory);// + outnameExcel
fileStream.Close();
fileStream.Dispose();
worksheet.Dispose();
package.Dispose();
GC.Collect();
}
// 根据列数获取列字母
private static string GetColumnLetter(int columnNumber)
{
int dividend = columnNumber;
string columnName = String.Empty;
int modulo;
while (dividend > 0)
{
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = (int)((dividend - modulo) / 26);
}
return columnName;
}
}
}