using Competition.Common.Util;
using CompetitionAPI.api.back;
using CompetitionAPI.Util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Text.RegularExpressions;
namespace CompetitionAPI.Controllers.back.system
{
[Route("api/[controller]")]
[ApiController]
public class ExportUserController : Controller
{
Competition.Mysql.BLL.admin_user user_bll = new Competition.Mysql.BLL.admin_user();
public ExportUserController()
{
}
///
/// 导出用户接口
///
///
[Authorize]
[HttpPost]
[APIFilter]
public ActionResult Index([FromBody] ExportUserRequest req)
{
try
{
if (req != null)
{
if (req.UserIdList.Count <= 0)
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "用户id集合不能为空"));
}
var workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
var sheet = workbook.CreateSheet("sheet1");
var row1 = sheet.CreateRow(0);//第1行
var cell1_1 = row1.CreateCell(0);//第1列
cell1_1.SetCellValue("所属市");
var cell1_2 = row1.CreateCell(1);//第2列
cell1_2.SetCellValue("姓名");
var cell1_3 = row1.CreateCell(2);//第3列
cell1_3.SetCellValue("ERP编码");
var cell1_4 = row1.CreateCell(3);//第4列
cell1_4.SetCellValue("性别");
var cell1_5 = row1.CreateCell(4);//第5列
cell1_5.SetCellValue("员工编码");
var cell1_6 = row1.CreateCell(5);//第6列
cell1_6.SetCellValue("工作单位");
var cell1_7 = row1.CreateCell(6);//第7列
cell1_7.SetCellValue("所在部门");
var cell1_8 = row1.CreateCell(7);//第8列
cell1_8.SetCellValue("工作岗位");
var cell1_9 = row1.CreateCell(8);//第9列
cell1_9.SetCellValue("身份证号");
var cell1_10 = row1.CreateCell(9);//第9列
cell1_10.SetCellValue("参加工作时间");
var cell1_11 = row1.CreateCell(10);//第9列
cell1_11.SetCellValue("文化程度");
var cell1_12 = row1.CreateCell(11);//第9列
cell1_12.SetCellValue("现有专业资格等级");
var cell1_13 = row1.CreateCell(12);//第9列
cell1_13.SetCellValue("现有职业技能等级");
var cell1_14 = row1.CreateCell(13);//第9列
cell1_14.SetCellValue("联系电话");
var cell1_15 = row1.CreateCell(14);//第9列
cell1_15.SetCellValue("密码");
var cell1_16 = row1.CreateCell(15);//第9列
cell1_16.SetCellValue("备注");
var data = user_bll.GetModelList(" user_id in ('" + string.Join("','", req.UserIdList.ToArray()) + "') ");
for (var i = 0; i < data.Count; i++)
{
var model = data[i];
var row = sheet.CreateRow(i + 1);
var cell1 = row.CreateCell(0);
var cell2 = row.CreateCell(1);
var cell3 = row.CreateCell(2);
var cell4 = row.CreateCell(3);
var cell5 = row.CreateCell(4);
var cell6 = row.CreateCell(5);
var cell7 = row.CreateCell(6);
var cell8 = row.CreateCell(7);
var cell9 = row.CreateCell(8);
var cell10 = row.CreateCell(9);
var cell11 = row.CreateCell(10);
var cell12 = row.CreateCell(11);
var cell13 = row.CreateCell(12);
var cell14 = row.CreateCell(13);
var cell15 = row.CreateCell(14);
var cell16 = row.CreateCell(15);
cell1.SetCellValue(model.OwnCity);
cell2.SetCellValue(model.real_name);
cell3.SetCellValue(model.ErpCode);
cell4.SetCellValue(model.sex);
cell5.SetCellValue(model.login_name);
cell6.SetCellValue(model.unit_name);
cell7.SetCellValue(model.dep_name);
cell8.SetCellValue(model.GZGW);
cell9.SetCellValue(model.id_card);
cell10.SetCellValue(model.WorkDate.HasValue ? model.WorkDate.Value.ToString("yyyy年MM月") : "");
cell11.SetCellValue(model.Education);
cell12.SetCellValue(model.NowMajorGrade);
cell13.SetCellValue(model.NowJobGrade);
cell14.SetCellValue(model.mobile);
cell15.SetCellValue(EncryptionAndDecryption.DecryptByLgzn(EncryptionAndDecryption.DecryptByLgzn(model.password)));
cell16.SetCellValue(model.r1);
}
//保存Workbook方式二: 保存到内存流中
var stream = new MemoryStream();
workbook.Write(stream);
stream.Flush();
stream.Position = 0;
sheet = null;
workbook = null;
string ExcelName = string.Format("{0}.xls", "用户管理_" + DateTime.Now.ToString("yyyyMMddHHmmss"));
return File(stream, "application/vnd.ms-excel", ExcelName);
}
else
{
return Json(Tool.GetJsonWithCode(APICode.Fail, "请求参数不能为空"));
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace);
return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。"));
}
}
}
}