试题导出调整

This commit is contained in:
夏菊 2025-10-15 16:08:06 +08:00
parent 39a8b67c25
commit 902b3ac541
3 changed files with 352 additions and 212 deletions

View File

@ -53,10 +53,10 @@
LabelAlign="Right">
</f:TextBox>
<f:DropDownList runat="server" ID="drptype" AutoPostBack="true" Label="题型" LabelWidth="60px" OnSelectedIndexChanged="TextBox_TextChanged">
<f:ListItem Value="0" Text="全部" Selected="true"/>
<f:ListItem Value="1" Text="单选题"/>
<f:ListItem Value="2" Text="多选题"/>
<f:ListItem Value="3" Text="判断题"/>
<f:ListItem Value="0" Text="全部" Selected="true" />
<f:ListItem Value="1" Text="单选题" />
<f:ListItem Value="2" Text="多选题" />
<f:ListItem Value="3" Text="判断题" />
</f:DropDownList>
<f:CheckBox runat="server" ID="ckIsItem" Text="选项不全" AutoPostBack="true" OnCheckedChanged="ckIsItem_CheckedChanged"></f:CheckBox>
<f:ToolbarFill ID="ToolbarFill1" runat="server">
@ -67,9 +67,12 @@
<f:Button ID="btnImport" ToolTip="导入" Icon="PageExcel" Hidden="true" runat="server"
OnClick="btnImport_Click">
</f:Button>
<f:Button ID="Button1" OnClick="btnMenuOut1_Click" runat="server" ToolTip="导出" Icon="FolderUp"
<f:Button ID="Button1" OnClick="btnOut_Click" runat="server" ToolTip="导出" Icon="FolderUp"
EnableAjax="false" DisableControlBeforePostBack="false">
</f:Button>
<%--<f:Button ID="Button1" OnClick="btnMenuOut1_Click" runat="server" ToolTip="导出" Icon="FolderUp"
EnableAjax="false" DisableControlBeforePostBack="false">
</f:Button>--%>
<%-- <f:Button ID="btnRefresh" ToolTip="按基础数据刷新" Icon="ArrowRefresh" runat="server" OnClick="Refresh_Click"
Hidden="true">
</f:Button>

View File

@ -1,8 +1,10 @@
using BLL;
using NPOI.POIFS.Properties;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.HSSE.EduTrain
@ -160,6 +162,22 @@ namespace FineUIPro.Web.HSSE.EduTrain
private void BindGrid()
{
if (this.tvTestTraining.SelectedNode != null && !string.IsNullOrEmpty(this.tvTestTraining.SelectedNode.NodeID))
{
string trainingId = this.tvTestTraining.SelectedNode.NodeID;
DataTable tb = BindData(trainingId);
Grid1.RecordCount = tb.Rows.Count;
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
}
}
/// <summary>
/// 数据加载
/// </summary>
/// <param name="trainingId"></param>
/// <returns></returns>
public DataTable BindData(string trainingId)
{
string strSql = @"SELECT TrainingItemId,TrainingId,TrainingItemCode,TrainingItemName,Abstracts,AttachUrl,VersionNum,TestType "
+ @" ,(CASE WHEN TestType = '1' THEN '单选题' WHEN TestType = '2' THEN '多选题' ELSE '判断题' END) AS TestTypeName "
@ -168,7 +186,7 @@ namespace FineUIPro.Web.HSSE.EduTrain
+ @" WHERE TrainingId=@TrainingId ";
List<SqlParameter> listStr = new List<SqlParameter>
{
new SqlParameter("@TrainingId", this.tvTestTraining.SelectedNode.NodeID)
new SqlParameter("@TrainingId", trainingId)
};
if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
{
@ -187,11 +205,7 @@ namespace FineUIPro.Web.HSSE.EduTrain
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
Grid1.RecordCount = tb.Rows.Count;
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
}
return tb;
}
#endregion
@ -393,41 +407,162 @@ namespace FineUIPro.Web.HSSE.EduTrain
}
#endregion
#region
#region
/// <summary>
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuOut_Click(object sender, EventArgs e)
protected void btnOut_Click(object sender, EventArgs e)
{
PageContext.RegisterStartupScript(Window3.GetShowReference(String.Format("TestTrainingOut.aspx", "导出 - ")));
}
/// <summary>
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuOut1_Click(object sender, EventArgs e)
string rootPath = Server.MapPath("~/");
string initTemplatePath = Const.TestTrainingTemplateUrl;
string uploadfilepath = string.Empty;
string newUrl = string.Empty;
uploadfilepath = rootPath + initTemplatePath;
string trainingId = string.Empty;
if (this.tvTestTraining.SelectedNode != null && !string.IsNullOrEmpty(this.tvTestTraining.SelectedNode.NodeID))
{
if (this.tvTestTraining.SelectedNode != null && this.tvTestTraining.SelectedNodeID != "0")
{
Response.ClearContent();
string filename = Funs.GetNewFileName();
Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("考试试题" + filename, System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "application/excel";
Response.ContentEncoding = System.Text.Encoding.UTF8;
this.Grid1.PageSize = Grid1.RecordCount;
BindGrid();
Response.Write(GetGridTableHtml(Grid1));
Response.End();
trainingId = this.tvTestTraining.SelectedNode.NodeID;
}
else
{
ShowNotify("请选择树节点", MessageBoxIcon.Warning);
}
ShowNotify("请选择试题类型!", MessageBoxIcon.Warning);
return;
}
var testTraining = Funs.DB.Training_TestTraining.FirstOrDefault(x => x.TrainingId == trainingId);
var lists = (from x in Funs.DB.Training_TestTrainingItem
where x.TrainingId == trainingId
select x);
string tName = this.txtName.Text.Trim();
if (!string.IsNullOrEmpty(tName))
{
lists = lists.Where(x => x.TrainingItemCode.Contains(tName) || x.TrainingItemName.Contains(tName) || x.Abstracts.Contains(tName) || x.WorkPostNames.Contains(tName));
}
if (this.drptype.SelectedValue != "0")
{
lists = lists.Where(x => x.TestType == this.drptype.SelectedValue);
}
if (this.ckIsItem.Checked)
{
lists = lists.Where(x => string.IsNullOrWhiteSpace(x.AItem) == true || string.IsNullOrWhiteSpace(x.BItem) == true);
}
lists = lists.OrderByDescending(x => x.TrainingItemCode);
if (lists.Any())
{
//string projectName = BLL.ProjectService.GetShortNameByProjectId(this.CurrUser.LoginProjectId);
newUrl = uploadfilepath.Replace("考试试题模版", $"{testTraining.TrainingName}考试试题({DateTime.Now.ToString("yyyyMMddHHmmss")})");
if (File.Exists(newUrl))
{
File.Delete(newUrl);
}
File.Copy(uploadfilepath, newUrl);
// 第一步:读取文件流
NPOI.SS.UserModel.IWorkbook workbook;
using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read))
{
//workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream);
workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(stream);
}
// 创建单元格样式
NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle();
cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
cellStyle.WrapText = true;//自动换行
var font = workbook.CreateFont();
font.FontHeightInPoints = 11;
cellStyle.SetFont(font);
// 第二步:创建新数据行
NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
NPOI.SS.UserModel.IRow row = sheet.GetRow(0);
NPOI.SS.UserModel.ICell cell;
int i = 2;
foreach (var item in lists)
{
// 第二步:创建新数据行
row = sheet.CreateRow(i);
// 添加数据
cell = row.CreateCell(0);
cell.CellStyle = cellStyle;
cell.SetCellValue(testTraining.TrainingCode);//试题类型编号
cell = row.CreateCell(1);
cell.CellStyle = cellStyle;
cell.SetCellValue(testTraining.TrainingName);//试题类型
cell = row.CreateCell(2);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.TrainingItemCode);//试题编号
string testTypeName = item.TestType == "1" ? "单选题" : item.TestType == "2" ? "多选题" : "判断题";
cell = row.CreateCell(3);
cell.CellStyle = cellStyle;
cell.SetCellValue(testTypeName);//题型
cell = row.CreateCell(4);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.WorkPostNames);//适合岗位
cell = row.CreateCell(5);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.Abstracts);//试题内容
cell = row.CreateCell(6);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.AItem);//答案项A
cell = row.CreateCell(7);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.BItem);//答案项B
cell = row.CreateCell(8);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.CItem);//答案项C
cell = row.CreateCell(9);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.DItem);//答案项D
cell = row.CreateCell(10);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.EItem);//答案项E
cell = row.CreateCell(11);
cell.CellStyle = cellStyle;
cell.SetCellValue(item.AnswerItems);//正确答案
i++;
}
// 第三步:写入文件流
using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write))
{
workbook.Write(stream);
workbook.Close();
}
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);
}
else
{
Alert.ShowInTop("当前无记录,无法导出!", MessageBoxIcon.Warning);
}
}
#endregion
#region

View File

@ -7,10 +7,12 @@
// </自动生成>
//------------------------------------------------------------------------------
namespace FineUIPro.Web.HSSE.EduTrain {
namespace FineUIPro.Web.HSSE.EduTrain
{
public partial class TestTraining {
public partial class TestTraining
{
/// <summary>
/// form1 控件。