excel导入校验

This commit is contained in:
Jimmy 2021-11-16 16:30:54 +08:00
parent 4646f273b4
commit bbf23b8be8
3 changed files with 140 additions and 61 deletions

View File

@ -28,10 +28,9 @@ namespace MineTec.ProManager.GalleryFiles.Dto
public string pictureframe { get; set; } //图幅 public string pictureframe { get; set; } //图幅
/// <summary> public string productmodel { get; set; }//产品型号/系列
/// 产品型号/系列
/// </summary> public string patterninfo { get; set; }//图样信息
public string productmodel { get; set; }
public string remark { get; set; } //备注 public string remark { get; set; } //备注
} }

View File

@ -11,6 +11,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web; using System.Web;
@ -354,6 +355,9 @@ namespace MineTec.ProManager.GalleryFiles
//特征码8 //特征码8
var Query_8 = _PartNumberRepository.GetAll().Where(t => t.IsDelete != 1 && t.PartMark == 8).OrderByDescending(t => t.CreateTime); var Query_8 = _PartNumberRepository.GetAll().Where(t => t.IsDelete != 1 && t.PartMark == 8).OrderByDescending(t => t.CreateTime);
var query_8_list = Query_8.ToList(); var query_8_list = Query_8.ToList();
//图号信息
var GalleryFilesInfo = _GalleryFilesRepository.GetAll().Where(a => a.IsDelete == 0);
var GalleryFiles_list = GalleryFilesInfo.ToList();
using (var package = new ExcelPackage(stream)) using (var package = new ExcelPackage(stream))
{ {
@ -369,6 +373,7 @@ namespace MineTec.ProManager.GalleryFiles
string explainTitle = worksheet.Cells[1, 1].Value == null ? "" : worksheet.Cells[1, 1].Value.ToString();//第一行第一列,填写说明的标题 string explainTitle = worksheet.Cells[1, 1].Value == null ? "" : worksheet.Cells[1, 1].Value.ToString();//第一行第一列,填写说明的标题
string explainText = worksheet.Cells[1, 2].Value == null ? "" : worksheet.Cells[1, 2].Value.ToString();//填写说明的内容 string explainText = worksheet.Cells[1, 2].Value == null ? "" : worksheet.Cells[1, 2].Value.ToString();//填写说明的内容
//从第2行开始把每列标题也给赋值进去方便导出生成数据后续在校验和导入时跳过第二行 //从第2行开始把每列标题也给赋值进去方便导出生成数据后续在校验和导入时跳过第二行
int isFalse = 0;
for (int row = 2; row <= RowNum; row++) for (int row = 2; row <= RowNum; row++)
{ {
//类型必须为string类型防止为null时转换出错在具体校验时再进行验证 //类型必须为string类型防止为null时转换出错在具体校验时再进行验证
@ -383,10 +388,20 @@ namespace MineTec.ProManager.GalleryFiles
string pictureframe = worksheet.Cells[row, 9].Value == null ? "" : worksheet.Cells[row, 11].Value.ToString();//图幅 string pictureframe = worksheet.Cells[row, 9].Value == null ? "" : worksheet.Cells[row, 11].Value.ToString();//图幅
string remark = worksheet.Cells[row, 10].Value == null ? "" : worksheet.Cells[row, 12].Value.ToString();//备注 string remark = worksheet.Cells[row, 10].Value == null ? "" : worksheet.Cells[row, 12].Value.ToString();//备注
string signaturecode = "";//特征码 string signaturecode = "";//特征码
string filename = "";//图样名称 string filename = "";//图样名称
string projectnum = patterninfo.Split('_')[0].Remove(0, 1);//项目编号 string projectnum = "";//项目编号
string patterntype = patterninfo.Split('_')[0].Substring(0, 1);//图样类型 string patterntype = "";//图样类型
if (row > 2)
{
if (importmode == "申请")
{
projectnum = patterninfo.Split('_')[0].Remove(0, 1);
patterntype = patterninfo.Split('_')[0].Substring(0, 1);
if (patterninfo.Split('_')[0].Substring(0, 1) == "5" || patterninfo.Split('_')[0].Substring(0, 1) == "6" || patterninfo.Split('_')[0].Substring(0, 1) == "8") if (patterninfo.Split('_')[0].Substring(0, 1) == "5" || patterninfo.Split('_')[0].Substring(0, 1) == "6" || patterninfo.Split('_')[0].Substring(0, 1) == "8")
{ {
signaturecode = patterninfo.Split('_')[1]; signaturecode = patterninfo.Split('_')[1];
@ -396,18 +411,43 @@ namespace MineTec.ProManager.GalleryFiles
{ {
filename = patterninfo.Split('_')[1]; filename = patterninfo.Split('_')[1];
} }
}
else if (importmode == "录入")
{
projectnum = filenum.Split('_')[0].Remove(0, 1);
patterntype = filenum.Split('_')[0].Substring(0, 1);
if (patterninfo.Split('_')[0].Substring(0, 1) == "5" || patterninfo.Split('_')[0].Substring(0, 1) == "6" || patterninfo.Split('_')[0].Substring(0, 1) == "8")
{
signaturecode = filenum.Split('_')[1];
}
if (filenum.Contains("~"))
{
subcode = filenum.Split('~')[1];
}
}
//校验 //校验
//类型是否为02568 //类型是否为02568
if (patterntype != "0" && patterntype != "2" && patterntype != "5" && patterntype != "6" && patterntype != "8") if (patterntype != "0" && patterntype != "2" && patterntype != "5" && patterntype != "6" && patterntype != "8")
{ {
msg += "图样类型有误,"; msg += "图样类型有误,";
isFalse = 1;
}
//产品型号
var model = projectlist.Select(t => t.productmodel == productmodel);
if (model == null)
{
msg += "产品型号/系列号不存在,";
isFalse = 1;
} }
//项目编号 //项目编号
var pcode = projectlist.Select(t => t.projectcode == projectnum); var pcode = projectlist.Select(t => t.projectcode == projectnum);
if (pcode == null) if (pcode == null)
{ {
msg += "项目编号不存在,"; msg += "项目编号不存在,";
isFalse = 1;
} }
//特征码 //特征码
if (patterntype == "5") if (patterntype == "5")
@ -416,6 +456,7 @@ namespace MineTec.ProManager.GalleryFiles
if (patterntype_5 == null) if (patterntype_5 == null)
{ {
msg += "特征码不存在,"; msg += "特征码不存在,";
isFalse = 1;
} }
} }
else if (patterntype == "6") else if (patterntype == "6")
@ -424,6 +465,7 @@ namespace MineTec.ProManager.GalleryFiles
if (patterntype_6 == null) if (patterntype_6 == null)
{ {
msg += "特征码不存在,"; msg += "特征码不存在,";
isFalse = 1;
} }
} }
else if (patterntype == "8") else if (patterntype == "8")
@ -432,15 +474,46 @@ namespace MineTec.ProManager.GalleryFiles
if (patterntype_8 == null) if (patterntype_8 == null)
{ {
msg += "特征码不存在,"; msg += "特征码不存在,";
isFalse = 1;
} }
} }
//生成序列号 //附加尾号
if (subcode != "" && subcode != null)
{
string pattern = @"^[1-9]\d*$";
if (!Regex.IsMatch(subcode, pattern))
{
msg += "附加尾号有误,";
isFalse = 1;
}
else if (Convert.ToInt32(subcode) < 1 || Convert.ToInt32(subcode) > 999)
{
msg += "附加尾号的值超出1-999之间的范围";
isFalse = 1;
}
}
//图幅
if (pictureframe != "" && pictureframe != null)
{
if (pictureframe != "A1" && pictureframe != "A2" && pictureframe != "A3" && pictureframe != "A4")
{
msg += "图幅输入错误,";
isFalse = 1;
}
}
if (importmode == "录入")
{
var GalleryFiles_check = GalleryFiles_list.Select(t => t.FileNum == filenum);
if (GalleryFiles_check != null)
{
msg += "图纸编号重复,";
isFalse = 1;
}
}
}
//校验数据填入对象
ExcelData.Add(new PatternExcelImportDto ExcelData.Add(new PatternExcelImportDto
{ {
// 获取每列数据 // 获取每列数据
@ -450,19 +523,26 @@ namespace MineTec.ProManager.GalleryFiles
importmode = importmode,//导入模式 importmode = importmode,//导入模式
filenum = filenum,//图纸编号 filenum = filenum,//图纸编号
productmodel = productmodel,//产品型号/系列号 productmodel = productmodel,//产品型号/系列号
projectnum = patterninfo.Split('_')[0].Remove(0, 1),//项目编号 patterninfo = patterninfo,//图样信息
patterntype = patterninfo.Split('_')[0].Substring(0, 1),//图样类型
signaturecode = signaturecode,//特征码
subcode = subcode,//附加尾号 subcode = subcode,//附加尾号
filename = filename,//图样名称
pictureframe = pictureframe,//图幅 pictureframe = pictureframe,//图幅
remark = remark,//备注 remark = remark,//备注
projectnum = projectnum,//项目编号
patterntype = patterntype,//图样类型
signaturecode = signaturecode,//特征码
filename = filename,//图样名称
}); });
} }
//数据获取完毕,开始校验 string sFileName = "校验结果_" + DateTime.Now.ToString("yyMMddhhmmss") + ".xlsx";
var path = Path.Combine(dirPath, sFileName);
FileInfo createfile = new FileInfo(path);
using ExcelPackage createpackage = new ExcelPackage(createfile);
//创建sheet
ExcelWorksheet createworksheet = package.Workbook.Worksheets.Add("Sheet1");
createworksheet.Cells.LoadFromCollection(ExcelData, true);
createpackage.Save(); //Save the workbook.
} }
return result; return result;

Binary file not shown.