fix:一人一档导出

This commit is contained in:
geh
2025-04-14 19:47:49 +08:00
parent 24390f1ea0
commit 4d7e4d1806
13 changed files with 1483 additions and 299 deletions
@@ -149,6 +149,9 @@
<HintPath>..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Net" />
<Reference Include="System.Net.Http">
<HintPath>..\BLL\bin\Debug\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
@@ -194,6 +194,9 @@
<f:MenuButton ID="btnMenuEdit" OnClick="btnMenuEdit_Click" EnablePostBack="true"
Hidden="true" runat="server" Text="修改" Icon="Pencil">
</f:MenuButton>
<f:MenuButton ID="btnPrinter" EnablePostBack="true" runat="server"
Text="导出" Icon="Printer" OnClick="butExport_Click" EnableAjax="false" DisableControlBeforePostBack="false">
</f:MenuButton>
<f:MenuButton ID="btnMenuDelete" OnClick="btnMenuDelete_Click" EnablePostBack="true"
Hidden="true" ConfirmText="删除选中行?" ConfirmTarget="Parent" runat="server" Icon="Delete" Text="删除">
</f:MenuButton>
@@ -7,6 +7,10 @@ using System.Data.SqlClient;
using System.Linq;
using System.Text;
using AspNet = System.Web.UI.WebControls;
using Aspose.Words;
using System.IO;
using System.Threading;
using Model;
namespace FineUIPro.Web.HSSE.SitePerson
{
@@ -763,5 +767,519 @@ namespace FineUIPro.Web.HSSE.SitePerson
}
return age;
}
protected void butExport_Click(object sender, EventArgs e)
{
if (Grid1.SelectedRowIndexArray.Length == 0)
{
Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
return;
}
var person = Funs.DB.View_SitePerson_Person.FirstOrDefault(x => x.PersonId == Grid1.SelectedRowID);
if (person != null)
{
string rootPath = Server.MapPath("~/");
string initTemplatePath = string.Empty;
string uploadfilepath = string.Empty;
string newUrl = string.Empty;
if (person.WorkPostName == "电工")
{
initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(电工).doc";
}
// else if (person.WorkPostName == "电焊工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(电焊工).doc";
// }
// else if (person.WorkPostName == "防水普工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(防水普工).doc";
// }
// else if (person.WorkPostName == "钢筋工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(钢筋工).doc";
// }
// else if (person.WorkPostName == "管理人员")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(管理人员).doc";
// }
// else if (person.WorkPostName == "架子工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(架子工).doc";
// }
// else if (person.WorkPostName == "木工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(木工).doc";
// }
// else if (person.WorkPostName == "普工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(普工).doc";
// }
// else if (person.WorkPostName == "施工电梯司机")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(施工电梯司机).doc";
// }
// else if (person.WorkPostName == "水电工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(水电工).doc";
// }
// else if (person.WorkPostName == "司索信号工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(司索信号工).doc";
// }
// else if (person.WorkPostName == "塔吊司机")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(塔吊司机).doc";
// }
// else if (person.WorkPostName == "挖机司机")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(挖机司机).doc";
// }
// else if (person.WorkPostName == "瓦工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(瓦工).doc";
// }
// else if (person.WorkPostName == "渣土车司机")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(渣土车司机).doc";
// }
// else if (person.WorkPostName == "桩基")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(桩基).doc";
// }
// else if (person.WorkPostName == "装配、灌浆工")
// {
// initTemplatePath = "File\\Word\\Person\\安全教育培训打印整套(装配、灌浆工).doc";
// }
else
{
Alert.ShowInTop("当前岗位类型不可导出!", MessageBoxIcon.Warning);
return;
}
uploadfilepath = rootPath + initTemplatePath;
newUrl = uploadfilepath.Replace(".doc", person.PersonName + ".doc");
if (File.Exists(newUrl))
{
File.Delete(newUrl);
}
File.Copy(uploadfilepath, newUrl);
AsposeWordHelper helper = new AsposeWordHelper();
helper.OpenTempelte(newUrl); //打开模板文件
string projectName = Funs.DB.Base_Project.First(z => z.ProjectId == person.ProjectId).ProjectName;
string name = person.PersonName;
string time = DateTime.Now.ToString("yyyy-MM-dd");
string sex = string.IsNullOrEmpty(person.Sex) ? "" : person.Sex == "1" ? "男" : "女";
string InTime = person.InTime.HasValue ? string.Format("{0:yyyy-MM-dd}", person.InTime) : "";
string IdCardFront = AttachFileService.getFileUrl(person.PersonId + "#1");
string IdCardBack = AttachFileService.getFileUrl(person.PersonId + "#5");
if (!string.IsNullOrEmpty(IdCardFront))
{
helper.AddImage(rootPath + IdCardFront, "IdCardFront", 100, 100, 10, 20);
}
else
{
helper.AddImage(string.Empty, "IdCardFront", 100, 100, 10, 20);
}
if (!string.IsNullOrEmpty(IdCardBack))
{
helper.AddImage(rootPath + IdCardBack, "IdCardBack", 100, 100, 120, 20);
}
else
{
helper.AddImage(string.Empty, "IdCardBack", 100, 100, 10, 20);
}
var gTrainingTestRecord =
(from x in Funs.DB.Training_TestRecord
join TestPlan in Funs.DB.Training_TestPlan on x.TestPlanId equals TestPlan.TestPlanId into
gTestPlan
from TestPlan in gTestPlan.DefaultIfEmpty()
join user in Funs.DB.Sys_User on TestPlan.PlanManId equals user.UserId into gUser
from user in gUser.DefaultIfEmpty()
where x.TestManId == person.PersonId
// where x.TestManId == "d9f795a2-b8eb-4872-9e4c-4a3dba028b5c"
orderby x.TestEndTime descending
select new
{
TestRecordId = x.TestRecordId,
TestStartTime = x.TestStartTime,
TestEndTime = x.TestEndTime,
Signature = x.Signature,
TestScores = x.TestScores,
PlanManName = user == null ? "" : user.UserName
}).FirstOrDefault();
string date1 = "";
string sign = "";
string sign1 = "";
string TestRecordId = "";
decimal? TestScores = null;
DateTime? TestStartTime = null;
DateTime? dateTime1 = null;
string Signature = "";
string unitName = "";
string workPostName = "";
string testName = "";
string IdCard = "";
int SValue = 0;
int SValueCount = 0;
int MValue = 0;
int MValueCount = 0;
int JValue = 0;
int JValueCount = 0;
Sys_TestRule sysTestRule = null;
IQueryable<Training_TestRecordItem> getTestItems = null;
List<Training_TestRecordItem> getSingleItem = new List<Training_TestRecordItem>();
List<Training_TestRecordItem> getMultipleItem = new List<Training_TestRecordItem>();
List<Training_TestRecordItem> getIsTrueItem = new List<Training_TestRecordItem>();
if (gTrainingTestRecord != null)
{
testName = person.PersonName;
IdCard = person.IdentityCard;
Signature = gTrainingTestRecord.Signature;
TestScores = gTrainingTestRecord.TestScores;
TestStartTime = gTrainingTestRecord.TestStartTime;
TestRecordId = gTrainingTestRecord.TestRecordId;
sign = gTrainingTestRecord.PlanManName;
sign1 = AttachFileService.getFileUrl(person.PersonId + "_1");
dateTime1 = gTrainingTestRecord.TestStartTime.Value;
unitName = BLL.UnitService.GetUnitNameByUnitId(person.UnitId);
workPostName = WorkPostService.getWorkPostNamesWorkPostIds(person.WorkPostId);
sysTestRule = Funs.DB.Sys_TestRule.FirstOrDefault();
getTestItems = from x in Funs.DB.Training_TestRecordItem
where x.TestRecordId == TestRecordId
select x;
getSingleItem = getTestItems.Where(x => x.TestType == "1").ToList();
SValue = sysTestRule.SValue; //每题分数
SValueCount = sysTestRule.SValue * getSingleItem.Count; //总分
getMultipleItem = getTestItems.Where(x => x.TestType == "2").ToList();
MValue = sysTestRule.MValue; //每题分数
MValueCount = sysTestRule.MValue * getMultipleItem.Count; //总分
getIsTrueItem = getTestItems.Where(x => x.TestType == "3").ToList();
JValue = sysTestRule.JValue; //每题分数
JValueCount = sysTestRule.JValue * getIsTrueItem.Count; //总分
}
if (!string.IsNullOrEmpty(sign1))
{
helper.AddImage(rootPath + sign1, "sign1", 50, 25, 15, 30);
helper.AddImage(rootPath + sign1, "sign1_1", 50, 25, 15, 30);
helper.AddImage(rootPath + sign1, "sign1_2", 50, 25, 15, 30);
}
else
{
helper.AddImage(string.Empty, "sign1", 0, 0, 0, 0);
helper.AddImage(string.Empty, "sign1_1", 0, 0, 0, 0);
helper.AddImage(string.Empty, "sign1_2", 0, 0, 0, 0);
}
// 构建格式化的字符串
date1 = dateTime1.HasValue
? $" {dateTime1.Value.Year} 年 {dateTime1.Value.Month:D2} 月 {dateTime1.Value.Day:D2} 日"
: "";
string sign2 = AttachFileService.getFileUrl(person.PersonId + "_2");
if (!string.IsNullOrEmpty(sign2))
{
helper.AddImage(rootPath + sign2, "sign2", 50, 25, 130, 0);
}
else
{
helper.AddImage(string.Empty, "sign2", 0, 0, 0, 0);
}
DateTime dateTime2 = new DateTime(2024, 6, 25);
// 构建格式化的字符串
string date2 = $" {dateTime2.Year} 年 {dateTime2.Month:D2} 月 {dateTime2.Day:D2} 日";
string IdentityCard = person.IdentityCard;
string Telephone = person.Telephone;
// 假设我们有一个DateTime对象
DateTime dateTime = new DateTime(2024, 6, 25);
// 提取年、月、日
int year = dateTime.Year;
int month = dateTime.Month;
int day = dateTime.Day;
string sign3 = AttachFileService.getFileUrl(person.PersonId + "_3");
if (!string.IsNullOrEmpty(sign3))
{
helper.AddImage(rootPath + sign3, "sign3", 50, 25, 350, 5);
}
else
{
helper.AddImage(string.Empty, "sign3", 0, 0, 0, 0);
}
string sign4 = AttachFileService.getFileUrl(person.PersonId + "_4");
if (!string.IsNullOrEmpty(sign4))
{
helper.AddImage(rootPath + sign4, "sign4", 50, 25, 100, 5);
}
else
{
helper.AddImage(string.Empty, "sign4", 0, 0, 0, 0);
}
string sign5 = AttachFileService.getFileUrl(person.PersonId + "_5");
if (!string.IsNullOrEmpty(sign5))
{
helper.AddImage(rootPath + sign5, "sign5", 50, 25, 320, 5);
}
else
{
helper.AddImage(string.Empty, "sign5", 0, 0, 0, 0);
}
#region
#region
DataTable tableS = new DataTable("TableS");
tableS.Columns.Add("SProblem");
tableS.Columns.Add("SOption");
if (getSingleItem.Count > 0)
{
int num = 1;
foreach (var item in getSingleItem)
{
string Avstracts = item.Abstracts.Replace(" ", "").Replace(" ", "").Replace("", "(")
.Replace("", ")").Replace("()", "(" + item.SelectedItem + ")");
string SProblem = num + "、" + Avstracts + " 正确答案:" + item.AnswerItems;
string str = string.Empty;
if (!string.IsNullOrEmpty(item.AItem))
{
str += "A." + item.AItem;
}
if (!string.IsNullOrEmpty(item.BItem))
{
str += " B." + item.BItem;
}
if (!string.IsNullOrEmpty(item.CItem))
{
str += " C." + item.CItem;
}
if (!string.IsNullOrEmpty(item.DItem))
{
str += " D." + item.DItem;
}
if (!string.IsNullOrEmpty(item.EItem))
{
str += " E." + item.EItem;
}
string SOption = str;
tableS.Rows.Add(new object[] { SProblem, SOption });
num++;
}
}
else
{
tableS.Rows.Add(new object[] { "", "" });
}
#endregion
#region
DataTable tableM = new DataTable("TableM");
tableM.Columns.Add("MProblem");
tableM.Columns.Add("MOption");
if (getMultipleItem.Count > 0)
{
int num = 1;
foreach (var item in getMultipleItem)
{
string Avstracts = item.Abstracts.Replace(" ", "").Replace(" ", "").Replace("", "(")
.Replace("", ")").Replace("()", "(" + item.SelectedItem + ")");
string MProblem = num + "、" + Avstracts + " 正确答案:" + item.AnswerItems;
string str = string.Empty;
if (!string.IsNullOrEmpty(item.AItem))
{
str += "A." + item.AItem;
}
if (!string.IsNullOrEmpty(item.BItem))
{
str += " B." + item.BItem;
}
if (!string.IsNullOrEmpty(item.CItem))
{
str += " ;C." + item.CItem;
}
if (!string.IsNullOrEmpty(item.DItem))
{
str += " ;D." + item.DItem;
}
if (!string.IsNullOrEmpty(item.EItem))
{
str += " ;E." + item.EItem;
}
string MOption = str;
tableM.Rows.Add(new object[] { MProblem, MOption });
num++;
}
}
else
{
tableM.Rows.Add(new object[] { "", "" });
}
#endregion
#region
DataTable tableJ = new DataTable("TableJ");
tableJ.Columns.Add("data");
if (getIsTrueItem.Count > 0)
{
int num = 1;
foreach (var item in getIsTrueItem)
{
var Avstracts = item.Abstracts;
if (Avstracts.IndexOf("(") > -1)
{
Avstracts = Avstracts.Replace("(", "" + item.SelectedItem == "A" ? "(√" : "(×");
}
else
{
if (Avstracts.IndexOf("") > -1)
Avstracts = Avstracts.Replace("", "" + item.SelectedItem == "A" ? "(√" : "(×");
}
string data = num + "、" + Avstracts + " 正确答案:" + (item.AnswerItems == "A" ? "√" : "×");
tableJ.Rows.Add(new object[] { data });
num++;
}
}
else
{
tableJ.Rows.Add(new object[] { "" });
}
#endregion
helper.WriteTable(tableS); //集合赋值遍历
helper.WriteTable(tableM); //集合赋值遍历
helper.WriteTable(tableJ); //集合赋值遍历
//考试抓拍照片
var attachFile = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == TestRecordId);
if (attachFile != null && !string.IsNullOrEmpty(attachFile.AttachUrl))
{
List<string> listUrl = Funs.GetStrListByStr(attachFile.AttachUrl, ',');
int count = listUrl.Count();
if (count > 0)
{
helper.AddImage(rootPath + listUrl[0], "imgStr1", 70, 70, 10, 10);
if (count >= 2)
{
int cout2 = count / 2;
helper.AddImage(rootPath + listUrl[cout2], "imgStr2", 70, 70, 10, 10);
}
else
{
helper.AddImage(string.Empty, "imgStr2", 0, 0, 0, 0);
}
helper.AddImage(rootPath + listUrl[count - 1], "imgStr3", 70, 70, 10, 10);
}
}
else
{
helper.AddImage(string.Empty, "imgStr1", 0, 0, 0, 0);
helper.AddImage(string.Empty, "imgStr2", 0, 0, 0, 0);
helper.AddImage(string.Empty, "imgStr3", 0, 0, 0, 0);
}
#endregion
string[] fieldNames = { };
object[] fieldValues = { };
if (!string.IsNullOrEmpty(Signature))
{
helper.AddImage(rootPath + Signature, "testName", 50, 25, 10, 5);
fieldNames = new[]
{
"projectName", "name", "time", "sex", "InTime", "sign",
"date1", "date2", "IdentityCard", "Telephone", "year", "month", "day",
"unitName", "workPostName", "TestStartTime","IdCard", "TestScores", "SValue", "SValueCount", "MValue",
"MValueCount", "JValue", "JValueCount"
};
fieldValues = new object[]
{
projectName, name, time, sex, InTime, sign, date1, date2,
IdentityCard, Telephone, year, month, day,
unitName, workPostName, TestStartTime,IdCard, TestScores, SValue, SValueCount, MValue, MValueCount,
JValue, JValueCount
};
}
else
{
fieldNames = new[]
{
"projectName", "name", "time", "sex", "InTime", "sign",
"date1", "date2", "IdentityCard", "Telephone", "year", "month", "day",
"unitName", "workPostName", "TestStartTime", "testName", "IdCard", "TestScores", "SValue", "SValueCount",
"MValue", "MValueCount", "JValue", "JValueCount"
};
fieldValues = new object[]
{
projectName, name, time, sex, InTime, sign, date1, date2,
IdentityCard, Telephone, year, month, day,
unitName, workPostName, TestStartTime, testName, IdCard, TestScores, SValue, SValueCount,
MValue, MValueCount, JValue, JValueCount
};
}
helper.Executefield(fieldNames, fieldValues); //域赋值
helper.SaveDoc(newUrl); //文件保存,保存为doc
// 验证文件是否存在
if (!File.Exists(newUrl))
{
throw new Exception("文件不存在: " + newUrl);
}
string fileName = Path.GetFileName(newUrl);
FileInfo info = new FileInfo(newUrl);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition",
"attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(newUrl, 0, fileSize);
Response.Flush();
Response.Close();
File.Delete(newUrl);
}
}
}
}
@@ -374,6 +374,15 @@ namespace FineUIPro.Web.HSSE.SitePerson
/// </remarks>
protected global::FineUIPro.MenuButton btnMenuEdit;
/// <summary>
/// btnPrinter 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::FineUIPro.MenuButton btnPrinter;
/// <summary>
/// btnMenuDelete 控件。
/// </summary>