5131941288
在委托明细和导出模板中展示炉号与批号,并调整导出文件的筛选及打印范围。
294 lines
13 KiB
C#
294 lines
13 KiB
C#
using BLL;
|
|
using MiniExcelLibs;
|
|
using Model;
|
|
using NPOI.SS.Util;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Xml.Linq;
|
|
|
|
namespace FineUIPro.Web.CLGL
|
|
{
|
|
public partial class AntiCorrosionTrust : PageBase
|
|
{
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
if (!IsPostBack)
|
|
{
|
|
GetButtonPower();
|
|
InitTreeMenu();
|
|
BindGrid();
|
|
}
|
|
}
|
|
|
|
private void BindGrid()
|
|
{
|
|
var filter = new AntiCorrosionTrustOutput
|
|
{
|
|
ProjectId = this.CurrUser.LoginProjectId,
|
|
UnitWorkId = tvControlItem.SelectedNodeID,
|
|
TrustCode = txtTrustCode.Text.Trim(),
|
|
WeldTaskCode = txtWeldTaskCode.Text.Trim()
|
|
};
|
|
Grid1.DataSource = TwAntiCorrosionTrustService.GetListData(filter, Grid1);
|
|
Grid1.RecordCount = TwAntiCorrosionTrustService.Count;
|
|
Grid1.DataBind();
|
|
}
|
|
|
|
private void InitTreeMenu()
|
|
{
|
|
this.tvControlItem.Nodes.Clear();
|
|
|
|
TreeNode rootNode1 = new TreeNode();
|
|
rootNode1.NodeID = "1";
|
|
rootNode1.Text = "建筑工程";
|
|
rootNode1.CommandName = "建筑工程";
|
|
rootNode1.Selectable = false;
|
|
this.tvControlItem.Nodes.Add(rootNode1);
|
|
|
|
TreeNode rootNode2 = new TreeNode();
|
|
rootNode2.NodeID = "2";
|
|
rootNode2.Text = "安装工程";
|
|
rootNode2.CommandName = "安装工程";
|
|
rootNode2.Expanded = true;
|
|
rootNode2.Selectable = false;
|
|
this.tvControlItem.Nodes.Add(rootNode2);
|
|
|
|
var keyword = txtTreeSearch.Text.Trim();
|
|
var unitWorkList = (from x in Funs.DB.WBS_UnitWork
|
|
where x.ProjectId == this.CurrUser.LoginProjectId
|
|
&& x.SuperUnitWork == null && x.UnitId != null && x.ProjectType != null
|
|
&& (string.IsNullOrEmpty(keyword) || x.UnitWorkName.Contains(keyword) || x.UnitWorkCode.Contains(keyword))
|
|
orderby x.SerialNumber, x.UnitWorkCode
|
|
select x).ToList();
|
|
|
|
foreach (var q in unitWorkList.Where(x => x.ProjectType == "1"))
|
|
{
|
|
TreeNode tn = BuildUnitWorkNode(q);
|
|
rootNode1.Nodes.Add(tn);
|
|
}
|
|
foreach (var q in unitWorkList.Where(x => x.ProjectType == "2"))
|
|
{
|
|
TreeNode tn = BuildUnitWorkNode(q);
|
|
rootNode2.Nodes.Add(tn);
|
|
}
|
|
}
|
|
|
|
private TreeNode BuildUnitWorkNode(Model.WBS_UnitWork unitWork)
|
|
{
|
|
var unitNamesUnitIds = BLL.UnitService.getUnitNamesUnitIds(unitWork.UnitId);
|
|
TreeNode node = new TreeNode();
|
|
node.NodeID = unitWork.UnitWorkId;
|
|
node.Text = unitWork.UnitWorkName;
|
|
node.ToolTip = BLL.UiText.Message("施工单位:" + unitNamesUnitIds);
|
|
node.EnableClickEvent = true;
|
|
return node;
|
|
}
|
|
|
|
protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
|
|
{
|
|
BindGrid();
|
|
}
|
|
|
|
protected void btnTreeFind_Click(object sender, EventArgs e)
|
|
{
|
|
InitTreeMenu();
|
|
}
|
|
|
|
protected void btnSearch_Click(object sender, EventArgs e)
|
|
{
|
|
BindGrid();
|
|
}
|
|
|
|
protected void btnNew_Click(object sender, EventArgs e)
|
|
{
|
|
if (!CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, Const.Tw_AntiCorrosionTrustMenuId, Const.BtnAdd))
|
|
{
|
|
ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(tvControlItem.SelectedNodeID) || tvControlItem.SelectedNodeID == "1" || tvControlItem.SelectedNodeID == "2")
|
|
{
|
|
Alert.ShowInTop(BLL.UiText.Message("请选择单位工程!"), MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
PageContext.RegisterStartupScript(Window1.GetShowReference(string.Format("AntiCorrosionTrustEdit.aspx?UnitWorkId={0}", tvControlItem.SelectedNodeID)));
|
|
}
|
|
|
|
protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
|
|
{
|
|
EditSelected();
|
|
}
|
|
|
|
protected void btnMenuEdit_Click(object sender, EventArgs e)
|
|
{
|
|
EditSelected();
|
|
}
|
|
|
|
private void EditSelected()
|
|
{
|
|
if (!CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, Const.Tw_AntiCorrosionTrustMenuId, Const.BtnModify))
|
|
{
|
|
ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(Grid1.SelectedRowID))
|
|
{
|
|
Alert.ShowInTop(BLL.UiText.Message("请选择要编辑的项"), MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
PageContext.RegisterStartupScript(Window1.GetShowReference(string.Format("AntiCorrosionTrustEdit.aspx?Id={0}", Grid1.SelectedRowID)));
|
|
}
|
|
|
|
protected void btnMenuDelete_Click(object sender, EventArgs e)
|
|
{
|
|
if (!CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, Const.Tw_AntiCorrosionTrustMenuId, Const.BtnDelete))
|
|
{
|
|
ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
if (Grid1.SelectedRowIndexArray.Length == 0)
|
|
{
|
|
Alert.ShowInTop(BLL.UiText.Message("请至少选择一条记录"), MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
foreach (int rowIndex in Grid1.SelectedRowIndexArray)
|
|
{
|
|
string rowID = Grid1.DataKeys[rowIndex][0].ToString();
|
|
TwAntiCorrosionTrustService.DeleteById(rowID);
|
|
}
|
|
BindGrid();
|
|
ShowNotify("删除成功!", MessageBoxIcon.Success);
|
|
}
|
|
|
|
protected void btnPrint_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(Grid1.SelectedRowID))
|
|
{
|
|
ShowNotify("请选择要打印的项", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
BLL.FastReportService.ResetData();
|
|
DataTable master = TwAntiCorrosionTrustService.GetPrintMasterTable(Grid1.SelectedRowID);
|
|
DataTable detail = TwAntiCorrosionTrustService.GetPrintDetailTable(Grid1.SelectedRowID);
|
|
BLL.FastReportService.AddFastreportTable(master);
|
|
BLL.FastReportService.AddFastreportTable(detail);
|
|
|
|
string rootPath = Server.MapPath("~/");
|
|
string initTemplatePath = "File\\Fastreport\\防腐委托单.frx";
|
|
if (File.Exists(rootPath + initTemplatePath))
|
|
{
|
|
PageContext.RegisterStartupScript(Window3.GetShowReference(string.Format("~/Controls/Fastreport.aspx?ReportPath={0}", rootPath + initTemplatePath)));
|
|
}
|
|
else
|
|
{
|
|
ShowNotify("打印模板不存在!", MessageBoxIcon.Warning);
|
|
}
|
|
}
|
|
|
|
protected void btnExportTrust_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(Grid1.SelectedRowID))
|
|
{
|
|
ShowNotify("请选择要导出的项", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
string templatePath = Funs.RootPath + @"File\Excel\DataOut\防腐委托单.xlsx";
|
|
if (!File.Exists(templatePath))
|
|
{
|
|
ShowNotify("导出模板不存在!", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
DataTable master = TwAntiCorrosionTrustService.GetPrintMasterTable(Grid1.SelectedRowID);
|
|
DataTable detail = TwAntiCorrosionTrustService.GetPrintDetailTable(Grid1.SelectedRowID);
|
|
if (master.Rows.Count == 0)
|
|
{
|
|
ShowNotify("未找到防腐委托单数据!", MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
string tempPath = Path.Combine(Funs.RootPath, @"File\Excel\Temp", Guid.NewGuid().ToString("N") + ".xlsx");
|
|
Directory.CreateDirectory(Path.GetDirectoryName(tempPath));
|
|
SaveExportWorkbook(tempPath, templatePath, master.Rows[0], detail);
|
|
|
|
string trustCode = Convert.ToString(master.Rows[0]["TrustCode"]);
|
|
string fileName = string.IsNullOrEmpty(trustCode) ? "防腐委托单.xlsx" : trustCode + ".xlsx";
|
|
FileInfo info = new FileInfo(tempPath);
|
|
long fileSize = info.Length;
|
|
System.Web.HttpContext.Current.Response.Clear();
|
|
System.Web.HttpContext.Current.Response.ContentType = "application/x-zip-compressed";
|
|
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
|
|
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
|
|
System.Web.HttpContext.Current.Response.TransmitFile(tempPath, 0, fileSize);
|
|
System.Web.HttpContext.Current.Response.Flush();
|
|
File.Delete(tempPath);
|
|
System.Web.HttpContext.Current.Response.End();
|
|
}
|
|
|
|
private static void SaveExportWorkbook(string path, string templatePath, DataRow master, DataTable detail)
|
|
{
|
|
var value = new Dictionary<string, object>();
|
|
foreach (DataColumn column in master.Table.Columns)
|
|
{
|
|
value[column.ColumnName] = master[column] == DBNull.Value ? string.Empty : master[column];
|
|
}
|
|
value["Data"] = detail;
|
|
MiniExcel.SaveAsByTemplate(path, templatePath, value);
|
|
|
|
using (var archive = ZipFile.Open(path, ZipArchiveMode.Update))
|
|
using (var sheetStream = archive.GetEntry("xl/worksheets/sheet1.xml").Open())
|
|
using (var bookStream = archive.GetEntry("xl/workbook.xml").Open())
|
|
{
|
|
var sheet = XElement.Load(sheetStream);
|
|
XNamespace ns = sheet.Name.Namespace;
|
|
var filter = sheet.Element(ns + "autoFilter");
|
|
var merges = sheet.Element(ns + "mergeCells");
|
|
if (filter == null || merges == null) return;
|
|
|
|
// MiniExcel 展开明细时会把合并区域写在筛选区域之前,桌面 Excel 要求筛选区域在前。
|
|
filter.Remove();
|
|
merges.AddBeforeSelf(filter);
|
|
var filterRange = CellRangeAddress.ValueOf((string)filter.Attribute("ref"));
|
|
filterRange.LastRow = filterRange.FirstRow + detail.Rows.Count;
|
|
filter.SetAttributeValue("ref", filterRange.FormatAsString());
|
|
|
|
// 工作簿的筛选和打印范围也要随明细行数扩展,包含末行的签字和日期。
|
|
var book = XElement.Load(bookStream);
|
|
string sheetName = (string)book.Descendants(ns + "sheet").First().Attribute("name");
|
|
var names = book.Descendants(ns + "definedName");
|
|
var usedRange = CellRangeAddress.ValueOf((string)sheet.Element(ns + "dimension").Attribute("ref"));
|
|
var printRange = new CellRangeAddress(0, usedRange.LastRow, 0, filterRange.LastColumn);
|
|
names.FirstOrDefault(n => (string)n.Attribute("name") == "_xlnm._FilterDatabase")?.SetValue(filterRange.FormatAsString(sheetName, true));
|
|
names.FirstOrDefault(n => (string)n.Attribute("name") == "_xlnm.Print_Area")?.SetValue(printRange.FormatAsString(sheetName, true));
|
|
|
|
sheetStream.SetLength(0);
|
|
sheet.Save(sheetStream, SaveOptions.DisableFormatting);
|
|
bookStream.SetLength(0);
|
|
book.Save(bookStream, SaveOptions.DisableFormatting);
|
|
}
|
|
}
|
|
|
|
protected void Window1_Close(object sender, WindowCloseEventArgs e)
|
|
{
|
|
BindGrid();
|
|
}
|
|
|
|
private void GetButtonPower()
|
|
{
|
|
var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.PersonId, BLL.Const.Tw_AntiCorrosionTrustMenuId);
|
|
if (buttonList.Count > 0)
|
|
{
|
|
btnNew.Hidden = !buttonList.Contains(BLL.Const.BtnAdd);
|
|
btnMenuEdit.Hidden = !buttonList.Contains(BLL.Const.BtnModify);
|
|
btnMenuDelete.Hidden = !buttonList.Contains(BLL.Const.BtnDelete);
|
|
btnExportTrust.Hidden = !buttonList.Contains(BLL.Const.BtnPrint);
|
|
}
|
|
}
|
|
}
|
|
}
|