体检系统架构
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

149 lines
5.7 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using PEIS.Entity;
namespace PEIS.Utils
{
public class ExcelHelper
{
private int StartRow = 4;
public List<BasePatient> SuccessList = new List<BasePatient>();
public List<ExcelRowModel> ErrorList = new List<ExcelRowModel>();
public void ReadOrgPatientList()
{
try
{
var dialog = new OpenFileDialog();
dialog.Multiselect = false;//是否可以选择多个文件
dialog.Filter = @"Excel文件 (*.xls;*.xlsx)|*.xls;*.xlsx";
if (dialog.ShowDialog() != DialogResult.OK) return;
var excelFilePath = dialog.FileName;
ReadExcelData(excelFilePath);
}
catch (Exception e)
{
Console.WriteLine(e);
Global.Msg("err", e.Message);
}
}
/// <summary>
/// 读取Excel文件
/// </summary>
/// <param name="excelFilePath">指定 .xlsx/.xls 文件的路径</param>
public void ReadExcelData(string excelFilePath)
{
var fileExtension = Path.GetExtension(excelFilePath);
if (fileExtension != ".xls" && fileExtension != ".xlsx") return;
using (var fileStream = new FileStream(excelFilePath, FileMode.Open, FileAccess.Read))
{
var sheet = GetSheet(fileStream, fileExtension); // 第一个工作表
for (int startIndex = 0; startIndex < StartRow; startIndex++)
{
ErrorList.Add(new ExcelRowModel(startIndex, sheet.GetRow(startIndex)));
}
// 获取工作表的行数和列数
var rowCount = sheet.PhysicalNumberOfRows;
int continuousEmpty = 0;
// 遍历工作表并读取数据
for (var row = StartRow; row < rowCount; row++)
{
if (continuousEmpty == 5)
{
break; // 如果连续5行错误,停止循环
}
var dataRow = sheet.GetRow(row);
var readPatient = ReadRowToBasePatient(dataRow);
var isBasePatient = readPatient.Validate();
if (!isBasePatient || SuccessList.Any(p => p.CardNo == readPatient.CardNo))
{
continuousEmpty++;
ErrorList.Add(new ExcelRowModel(row, dataRow));
continue;
}
SuccessList.Add(readPatient);
continuousEmpty = 0;
}
};
}
private static ISheet GetSheet(FileStream fileStream, string fileExtension)
{
if (fileExtension == ".xls")
return new HSSFWorkbook(fileStream).GetSheetAt(0);
if (fileExtension == ".xlsx")
return new XSSFWorkbook(fileStream).GetSheetAt(0); // 第一个工作表
return null;
}
private static BasePatient ReadRowToBasePatient(IRow dataRow)
{
var sex = dataRow.GetCell(2)?.ToString()??"";
return new BasePatient()
{
// A-0-序号,B-1-姓名,C-2-性别,D-3-民族,E-4-婚姻,F-5-身份证号,G-6-住址,H-7-电话,I-8-部门,J-9备注
Name = dataRow.GetCell(1)?.ToString().Replace(" ", ""),
Contactor1 = dataRow.GetCell(1)?.ToString().Replace(" ", ""),
Sex = sex.Contains("女")?"2":"1",
Nation = dataRow.GetCell(3)?.ToString(),
Marriage = dataRow.GetCell(4)?.ToString(),
CardNo = dataRow.GetCell(5)?.ToString(),
CardType = "居民身份证",
Address1 = dataRow.GetCell(6)?.ToString(),
Tel1 = dataRow.GetCell(7)?.ToString(),
DeptName = dataRow.GetCell(8)?.ToString(),
};
}
}
public class ExcelRowModel
{
public int RowCount { get; set; }
public string ColumnA { get; set; }
public string ColumnB { get; set; }
public string ColumnC { get; set; }
public string ColumnD { get; set; }
public string ColumnE { get; set; }
public string ColumnF { get; set; }
public string ColumnG { get; set; }
public string ColumnH { get; set; }
public string ColumnI { get; set; }
public string ColumnJ { get; set; }
public ExcelRowModel()
{
}
public ExcelRowModel(int rowCount, IRow row)
{
RowCount = rowCount + 1;
ColumnA = row.GetCell(0)?.ToString()??"";
ColumnB = row.GetCell(1)?.ToString()??"";
ColumnC = row.GetCell(2)?.ToString()??"";
ColumnD = row.GetCell(3)?.ToString()??"";
ColumnE = row.GetCell(4)?.ToString()??"";
ColumnF = row.GetCell(5)?.ToString()??"";
ColumnG = row.GetCell(6)?.ToString()??"";
ColumnH = row.GetCell(7)?.ToString()??"";
ColumnI = row.GetCell(8)?.ToString()??"";
ColumnJ = row.GetCell(9)?.ToString()??"";
}
public ExcelRowModel(BasePatient p)
{
ColumnA = p.Name;
ColumnB = p.Sex;
ColumnC = p.Nation;
ColumnD = p.Marriage;
ColumnE = p.CardNo;
ColumnF = p.CardType;
ColumnG = p.Address1;
ColumnH = p.Tel1;
ColumnI = p.DeptName;
}
}
}