游戏技术文章

NPOI导入excel

时间:2017-4-18 13:16:11  作者:棋牌资源网  来源:棋牌资源网  查看:9080  评论:0
内容摘要:1.excel导入public override string CheckAndImportFile(ref DataTable errorInfoDataTable){porgressBar.SetProgress(5, "开始上传文件到服务器.....")...
1.excel导入

public override string CheckAndImportFile(ref DataTable errorInfoDataTable)
{

porgressBar.SetProgress(5, "开始上传文件到服务器.....");
//上传文件到服务器
file.SaveAs(filePath);
porgressBar.SetProgress(10, "文件已上传到服务器.....");

//用于保存检查结果
errorInfoDataTable = new DataTable();
errorInfoDataTable.Columns.Add("数据行号");
errorInfoDataTable.Columns.Add("错误信息");

List<Proctor> excelData;
Dictionary<int, string> dicErrorInfo;
porgressBar.SetProgress(30, "准备读取文件中数据........");

//获取上传excel中的数据
var importProctorHelper = new ImportProctorHelper();

var result = importProctorHelper.GetAndCheckUpLoadExcelData(filePath, new List<string> {testCampusCode},
porgressBar.SetProgress, out excelData, out dicErrorInfo);

porgressBar.SetProgress(90, "读取文件中数据完毕,准备插入数据......");

if (!result._IsNullOrEmpty())
{
foreach (var item in dicErrorInfo)
{
errorInfoDataTable.Rows.Add(item.Key, item.Value);
}
if (errorInfoDataTable.Rows.Count == 0)
{
errorInfoDataTable.Rows.Add("", result);
}
}
else
{
using (var ts = new TransactionScope(TransactionScopeOption.Required))
{
var updateCount = 0;
var insertCount = 0;
var alreadyCount = 0;
//查询该机构下所有的监考教师姓名和SID
var proctorDictionary =
ProctorDAL.Instance.GetProctorNameByTestIdAndTestCampusCode(testID, testCampusCode);

foreach (var item in excelData)
{
item.TestID = testID;
//该监考教师是否存在
if (proctorDictionary._ContainsKey(item.Name))
{
item.SID = proctorDictionary[item.Name];
if (ProctorDAL.Instance.Update(item))
updateCount++;
}
}
}
else
{
item.SID = CommonHelper.CreateNewGuid();
if (ProctorDAL.Instance.Add(item))
{
insertCount++;
}
}
}


if (result._IsNullOrEmpty())
{
ts.Complete();
result = string.Format("Excel总条数:{0},更新条数:{1},插入条数:{2},已被编排不能修改条数:{3}", excelData.Count, updateCount,
insertCount,alreadyCount);
}
else
{
errorInfoDataTable.Rows.Add("", result);
}
}
}


return result;

}

//该类处理了excel信息,并返回正确list数据,为上面插入准备数据

public class ImportProctorHelper
{
private const string PROCTOR_SHEET_NAME = "测试";

private string LoadProctors(List<string> testCampusCodeList, XSSFWorkbook xssfworkbook, Action<int, string> showProgress, ref List<Proctor> proctorList, ref Dictionary<int, string> errorInfoDic)
{
var result = string.Empty;
var sheet = xssfworkbook.GetSheet(PROCTOR_SHEET_NAME);
if (sheet == null)
{
return result;
}
//获取列名(模版中第四行为列名)
var cIndex = 0;
var columnNumber = LoadCell(sheet, 3, cIndex++).ToString().Trim(); //序号
var columntestCampusCode = LoadCell(sheet, 3, cIndex++).ToString().Trim(); //校区代码(必填)
var columnName = LoadCell(sheet, 3, cIndex++).ToString().Trim(); //监考教师(必填)
var columnProctorType = LoadCell(sheet, 3, cIndex++).ToString().Trim(); //监考类型(必填)
var columnCollege = LoadCell(sheet, 3, cIndex++).ToString().Trim(); //所属院系(必填)
var columnSeq = LoadCell(sheet, 3, cIndex++).ToString().Trim(); //使用顺序
var columnIsUsed = LoadCell(sheet, 3, cIndex).ToString().Trim(); //是否启用

if (columnNumber == "序号" && columntestCampusCode == "校区代码(必填)" &&
columnProctorType == "监考类型(必填)" && columnName == "监考教师(必填)" &&
columnCollege == "所属院系(必填)" && columnSeq == "使用顺序(必填)" && columnIsUsed == "是否启用(必填)")
{
for (var i = 4; i <= sheet.LastRowNum; i++)
{
if (i%1000 == 3)
{
showProgress(30 + i*60/sheet.LastRowNum,
string.Format("读取excel文件:{0}/{1}", i - 3, sheet.LastRowNum - 3));
}
if (errorInfoDic.Count > 1000)
{
result = "Excel文件错误过多,请检查";
break;
}
cIndex = 1;
var testCampusCode = LoadCell(sheet, i, cIndex++).ToString().Trim(); //校区代码(必填)
var name = LoadCell(sheet, i, cIndex++).ToString().Trim(); //监考教师(必填)
var proctorType = LoadCell(sheet, i, cIndex++).ToString().Trim(); //监考类型(必填)
var college = LoadCell(sheet, i, cIndex++).ToString().Trim(); //所属院系(必填)
var seq = LoadCell(sheet, i, cIndex++).ToString().Trim(); //使用顺序
var isUsed = LoadCell(sheet, i, cIndex).ToString().Trim(); //是否启用

//空行不处理
if (testCampusCode._IsNullOrEmpty() &&
name._IsNullOrEmpty() && proctorType._IsNullOrEmpty() &&
college._IsNullOrEmpty() &&
seq._IsNullOrEmpty() &&
isUsed._IsNullOrEmpty()
)
{
continue;
}


//必填字段
if (!testCampusCode._IsNullOrEmpty() &&
!name._IsNullOrEmpty() && !proctorType._IsNullOrEmpty() && !college._IsNullOrEmpty() &&
!seq._IsNullOrEmpty() &&
!isUsed._IsNullOrEmpty())
{
//检查校区代码是否一致
if (!testCampusCodeList.Contains(testCampusCode))
{
errorInfoDic.Add(i + 1,
string.Format("校区代码不一致,选择的校区代码为:{0},Excel中的校区代码为{1}", testCampusCodeList.First(),
testCampusCode));
continue;
}

if (!Regex.IsMatch(proctorType, @"^[1-2]$"))
{
errorInfoDic.Add(i + 1, "监考类型填写错误");
continue;
}
if (!Regex.IsMatch(seq, @"^[0-9]+$"))
{
errorInfoDic.Add(i + 1, "使用顺序填写错误");
continue;
}
if (!Regex.IsMatch(isUsed, @"^[0-1]$"))
{
errorInfoDic.Add(i + 1, "是否启用填写错误");
continue;
}
if (logExistDicName.ContainsKey(name))
{
errorInfoDic.Add(i + 1, string.Format("第{0}行存在一样姓名的教师", logExistDicName[name]));
continue;
}
logExistDicName.Add(name, i + 1);

var tmpEntity = new Proctor
{
Name = name,
TestCampusCode = testCampusCode,
ProctorType = int.Parse(proctorType) == 1 ? ProctorType.监考老师一 : ProctorType.监考老师二,
College = college,
Seq = int.Parse(seq),
IsUsed = int.Parse(isUsed) == 0 ? UsedType.未使用 : UsedType.使用
};
proctorList.Add(tmpEntity);
}
else
{
//必填字段没填写
errorInfoDic.Add(i + 1, "必填项未填写");
}
}
}
else
{
result = "Excel不符合规范,请根据模版中规范填写数据!";
}

if (result._IsNullOrEmpty() && errorInfoDic.Count > 0)
{
result = "Excel文件存在错误,请检查";
}


return result;
}

标签:NPOI导入excel 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2022 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved