365 lines
14 KiB
C#
365 lines
14 KiB
C#
using BLL;
|
||
using BLL.Common;
|
||
using Model;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using NPOI.XSSF.UserModel;
|
||
using NPOI.SS.UserModel;
|
||
|
||
namespace FineUIPro.Web.BaseInfo
|
||
{
|
||
public partial class ContractorList : PageBase
|
||
{
|
||
#region 加载
|
||
/// <summary>
|
||
/// 加载页面
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
if (!IsPostBack)
|
||
{
|
||
GetButtonPower();//按钮权限
|
||
//BLL.DisciplineService.InitDropDownList(this.drpDiscipline, true); //专业
|
||
|
||
ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
|
||
// 绑定表格
|
||
BindGrid();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绑定数据
|
||
/// </summary>
|
||
private void BindGrid()
|
||
{
|
||
string strSql = @"SELECT contractor.ContractorId,
|
||
contractor.VendorNumber,
|
||
(CASE WHEN contractor.Contractor IS NULL THEN ISNULL(contractor.ContractorCN,'')
|
||
ELSE (CASE WHEN contractor.ContractorCN IS NULL THEN ISNULL(contractor.Contractor,'')
|
||
ELSE (ISNULL(contractor.Contractor,'') + ISNULL(contractor.ContractorCN,'')) END) END)AS Contractor
|
||
FROM Base_Contractor AS contractor WHERE 1=1 ";
|
||
|
||
List<SqlParameter> listStr = new List<SqlParameter>();
|
||
if (!string.IsNullOrEmpty(this.txtContractorCode.Text.Trim()))
|
||
{
|
||
strSql += " AND (contractor.ContractorCN LIKE @Contractor OR contractor.Contractor LIKE @Contractor)";
|
||
listStr.Add(new SqlParameter("@Contractor", "%" + this.txtContractorCode.Text.Trim() + "%"));
|
||
}
|
||
if (!string.IsNullOrEmpty(this.txtVendorNum.Text.Trim()))
|
||
{
|
||
strSql += " AND contractor.VendorNumber LIKE @VendorNumber";
|
||
listStr.Add(new SqlParameter("@VendorNumber", "%" + this.txtVendorNum.Text.Trim() + "%"));
|
||
}
|
||
SqlParameter[] parameter = listStr.ToArray();
|
||
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
|
||
// 2.获取当前分页数据
|
||
Grid1.RecordCount = tb.Rows.Count;
|
||
var table = this.GetPagedDataTable(Grid1, tb);
|
||
Grid1.DataSource = table;
|
||
Grid1.DataBind();
|
||
}
|
||
|
||
protected void btnSelect_Click(object sender, EventArgs e)
|
||
{
|
||
BindGrid();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 改变索引事件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
|
||
{
|
||
Grid1.PageIndex = e.NewPageIndex;
|
||
BindGrid();
|
||
}
|
||
#endregion
|
||
|
||
#region 分页下拉选择
|
||
/// <summary>
|
||
/// 分页下拉选择
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
|
||
BindGrid();
|
||
}
|
||
#endregion
|
||
|
||
#region 删除
|
||
/// <summary>
|
||
/// 删除
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void btnDelete_Click(object sender, EventArgs e)
|
||
{
|
||
BLL.ContractorService.DeleteContractorById(hfFormID.Text);
|
||
BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete Contractor");
|
||
// 重新绑定表格,并模拟点击[新增按钮]
|
||
BindGrid();
|
||
//PageContext.RegisterStartupScript("onNewButtonClick();");
|
||
}
|
||
/// <summary>
|
||
/// 右键删除事件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void btnMenuDelete_Click(object sender, EventArgs e)
|
||
{
|
||
this.DeleteData();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除方法
|
||
/// </summary>
|
||
private void DeleteData()
|
||
{
|
||
if (Grid1.SelectedRowIndexArray.Length > 0)
|
||
{
|
||
foreach (int rowIndex in Grid1.SelectedRowIndexArray)
|
||
{
|
||
string rowID = Grid1.DataKeys[rowIndex][0].ToString();
|
||
|
||
BLL.ContractorService.DeleteContractorById(rowID);
|
||
BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete Contractor");
|
||
}
|
||
|
||
BindGrid();
|
||
//PageContext.RegisterStartupScript("onNewButtonClick();");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 编辑
|
||
/// <summary>
|
||
/// 右键编辑事件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void btnMenuEdit_Click(object sender, EventArgs e)
|
||
{
|
||
this.EditData();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 编辑数据方法
|
||
/// </summary>
|
||
private void EditData()
|
||
{
|
||
if (Grid1.SelectedRowIndexArray.Length == 0)
|
||
{
|
||
Alert.ShowInTop("Please select at least one record!", MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
string Id = Grid1.SelectedRowID;
|
||
var contractor = BLL.ContractorService.GetContractorById(Id);
|
||
if (contractor != null)
|
||
{
|
||
this.txtVendorNumber.Text = contractor.VendorNumber;
|
||
this.txtContractor.Text = contractor.Contractor;
|
||
this.txtContractorCN.Text = contractor.ContractorCN;
|
||
hfFormID.Text = Id;
|
||
this.btnDelete.Enabled = true;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 保存
|
||
/// <summary>
|
||
/// 保存按钮
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void btnSave_Click(object sender, EventArgs e)
|
||
{
|
||
string strRowID = hfFormID.Text;
|
||
Model.Base_Contractor contractor = new Model.Base_Contractor();
|
||
contractor.VendorNumber = this.txtVendorNumber.Text.Trim();
|
||
contractor.Contractor = this.txtContractor.Text.Trim();
|
||
contractor.ContractorCN = this.txtContractorCN.Text.Trim();
|
||
|
||
if (BLL.ContractorService.IsExitContractor(this.txtContractor.Text.Trim(), strRowID))
|
||
{
|
||
ShowNotify("The contractor already exists!", MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
if (BLL.ContractorService.IsExitVendor(txtVendorNumber.Text.Trim(), strRowID))
|
||
{
|
||
ShowNotify("The Vendor already exists!", MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(strRowID)) // 增加承包商
|
||
{
|
||
contractor.ContractorId = SQLHelper.GetNewID(typeof(Model.Base_Contractor));
|
||
BLL.ContractorService.AddContractor(contractor);
|
||
BindGrid();
|
||
ShowNotify("Save successfully!", MessageBoxIcon.Success);
|
||
BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add the Contractor");
|
||
|
||
}
|
||
else
|
||
{
|
||
contractor.ContractorId = strRowID;
|
||
BLL.ContractorService.UpdateContractor(contractor);
|
||
BindGrid();
|
||
ShowNotify("Save successfully!", MessageBoxIcon.Success);
|
||
BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify the Contractor");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 权限设置
|
||
/// <summary>
|
||
/// 菜单按钮权限
|
||
/// </summary>
|
||
private void GetButtonPower()
|
||
{
|
||
var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.ContractorMenuId);
|
||
if (buttonList.Count() > 0)
|
||
{
|
||
if (buttonList.Contains(BLL.Const.BtnAdd))
|
||
{
|
||
this.btnNew.Hidden = false;
|
||
}
|
||
if (buttonList.Contains(BLL.Const.BtnModify))
|
||
{
|
||
this.btnMenuEdit.Hidden = false;
|
||
}
|
||
if (buttonList.Contains(BLL.Const.BtnSave))
|
||
{
|
||
this.btnSave.Hidden = false;
|
||
}
|
||
if (buttonList.Contains(BLL.Const.BtnDelete))
|
||
{
|
||
this.btnDelete.Hidden = false;
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
protected void Grid1_RowSelect(object sender, GridRowSelectEventArgs e)
|
||
{
|
||
this.EditData();
|
||
}
|
||
|
||
protected void btnNew_Click(object sender, EventArgs e)
|
||
{
|
||
this.hfFormID.Text = string.Empty;
|
||
this.txtVendorNumber.Text = string.Empty;
|
||
this.txtContractor.Text = string.Empty;
|
||
this.txtContractorCN.Text = string.Empty;
|
||
this.btnDelete.Enabled = false;
|
||
}
|
||
|
||
#region 导出
|
||
/// <summary>
|
||
/// 导出
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void btnExport_Click(object sender, EventArgs e)
|
||
{
|
||
Grid1.PageSize = 10000;
|
||
BindGrid();
|
||
string rootPath = Server.MapPath("~/") + Const.ExcelUrl;
|
||
//模板文件
|
||
string TempletFileName = rootPath + "Contractor.xlsx";
|
||
//导出文件
|
||
string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
|
||
if (!Directory.Exists(filePath))
|
||
{
|
||
Directory.CreateDirectory(filePath);
|
||
}
|
||
string ReportFileName = filePath + "out.xls";
|
||
FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
|
||
XSSFWorkbook hssfworkbook = new XSSFWorkbook(file);
|
||
|
||
XSSFSheet ws = (XSSFSheet)hssfworkbook.GetSheet("Sheet1");
|
||
|
||
#region 样式
|
||
// 字体样式
|
||
IFont font = hssfworkbook.CreateFont();
|
||
font.FontHeightInPoints = 11;
|
||
font.IsBold = false;
|
||
font.FontName = "Arial";
|
||
ICellStyle fontStyle = hssfworkbook.CreateCellStyle();
|
||
fontStyle.SetFont(font);
|
||
|
||
ICellStyle yearStyle = hssfworkbook.CreateCellStyle();
|
||
yearStyle.VerticalAlignment = VerticalAlignment.Center;
|
||
yearStyle.Alignment = HorizontalAlignment.Center;
|
||
yearStyle.SetFont(font);
|
||
|
||
//创建单元格样式
|
||
XSSFCellStyle backgroundstyle = (XSSFCellStyle)hssfworkbook.CreateCellStyle();
|
||
//填充模式
|
||
backgroundstyle.FillPattern = FillPattern.SolidForeground;
|
||
//创建颜色
|
||
XSSFColor xssfcolor = new XSSFColor();
|
||
//rbg值
|
||
byte[] rgb = { (byte)255, (byte)192, (byte)203 };
|
||
//写入rgb 粉色背景颜色定义
|
||
xssfcolor.SetRgb(rgb);
|
||
//设置颜色值
|
||
backgroundstyle.SetFillForegroundColor(xssfcolor);
|
||
backgroundstyle.SetFont(font);
|
||
|
||
#endregion
|
||
|
||
if (Grid1.Rows.Count > 0)
|
||
{
|
||
var rowIndex = 1;
|
||
for (int i = 0; i < Grid1.Rows.Count; i++)
|
||
{
|
||
//int rowID = Convert.ToInt32(Grid1.DataKeys[i][0]);
|
||
if (ws.GetRow(rowIndex) == null) ws.CreateRow(rowIndex);
|
||
#region 列赋值
|
||
if (ws.GetRow(rowIndex).GetCell(0) == null) ws.GetRow(rowIndex).CreateCell(0);
|
||
ws.GetRow(rowIndex).GetCell(0).SetCellValue(rowIndex.ToString());
|
||
ws.GetRow(rowIndex).GetCell(0).CellStyle = fontStyle;
|
||
if (ws.GetRow(rowIndex).GetCell(1) == null) ws.GetRow(rowIndex).CreateCell(1);
|
||
ws.GetRow(rowIndex).GetCell(1).SetCellValue(Grid1.Rows[i].Values[1].ToString());
|
||
ws.GetRow(rowIndex).GetCell(1).CellStyle = fontStyle;
|
||
if (ws.GetRow(rowIndex).GetCell(2) == null) ws.GetRow(rowIndex).CreateCell(2);
|
||
ws.GetRow(rowIndex).GetCell(2).SetCellValue(Grid1.Rows[i].Values[2].ToString());
|
||
rowIndex++;
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
ws.ForceFormulaRecalculation = true;
|
||
|
||
using (FileStream filess = File.OpenWrite(ReportFileName))
|
||
{
|
||
hssfworkbook.Write(filess);
|
||
}
|
||
FileInfo filet = new FileInfo(ReportFileName);
|
||
Response.Clear();
|
||
Response.Charset = "GB2312";
|
||
Response.ContentEncoding = System.Text.Encoding.UTF8;
|
||
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
|
||
Response.AddHeader("Content-Disposition", "attachment; filename=Contractor" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"));
|
||
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
|
||
Response.AddHeader("Content-Length", filet.Length.ToString());
|
||
// 指定返回的是一个不能被客户端读取的流,必须被下载
|
||
Response.ContentType = "application/ms-excel";
|
||
// 把文件流发送到客户端
|
||
Response.WriteFile(filet.FullName);
|
||
// 停止页面的执行
|
||
Response.End();
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
} |