jqentitydetail

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using HraWeb.Common;
using Trirand.Web.UI.WebControls;
using Aspose.Cells;
using System.Collections;
using System.Text.RegularExpressions;
using Elmah;

namespace WebApp.Common
{
public class JQEntityManage<T> : BasePage where T : Framework.Domain.Entity
{
protected Contract.IService.IEntityService<T> svc
{
get;
set;
}
protected virtual void SaveLog()
{
Contract.Domain.SysOplog op = new Contract.Domain.SysOplog();
op.CreateDate = DateTime.Now;
op.CreateOid = CurrentUser.OfficeId;
op.CreatePid = CurrentUser.PositionId;
op.CreateUid = CurrentUser.UserId;
op.CreateUname = CurrentUser.UserName;
op.Moudleid = Request["_menuId"];
if (!string.IsNullOrEmpty(op.Moudleid))
{
op.Moudleid = op.Moudleid.Split(new char[] {',' })[0];
}
op.Opcommand = "查询";
op.Entity = typeof(T).ToString();
op.State.MarkNew();
Dao.SaveOrUpdate(op);
}

protected virtual void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
switch (HttpContext.Current.Request["_method"])
{
case "search":
SaveLog();
GetList();
break;
default:
InitPage();
break;
case "export":
SaveExpertLog();
ArrayList list = new ArrayList();
list.Add("Id");
ExportGrid(jq.Columns, string.Empty, list, true);
break;
}
}
}
private void SaveExpertLog()
{
Contract.Domain.SysOplog op = new Contract.Domain.SysOplog();
op.CreateDate = DateTime.Now;
op.CreateOid = CurrentUser.OfficeId;
op.CreatePid = CurrentUser.PositionId;
op.CreateUid = CurrentUser.UserId;
op.CreateUname = CurrentUser.UserName;
op.Moudleid = Request["_menuId"];
op.Opcommand = "导出数据";
op.OpType = "导出数据";
op.Entity = typeof(T).ToString();
op.State.MarkNew();
Dao.SaveOrUpdate(op);
}
private JQGrid _jq = null;
protected virtual JQGrid jq
{
set
{
_jq = value;
}
get
{
if (_jq == null)
{
_jq = FindControl("jq") as JQGrid;
}
return _jq;
}
}

protected virtual void ExportGrid(JQGridColumnCollection Columns,string title,ArrayList ignorColList, bool showHeader)
{
info = SetInfo();
info.TotalCount = 0;
info.StartRecord = 0;
setDataSource();
ChangeList(info);
System.Data.DataTable table = null;
if (info.Transformer != null)
{
table = (info.Transformer as System.Data.DataSet).Tables[0];
}
else
{
table = ToJson.ToDataTable(info.List, Columns);
}
Workbook workbook = new Workbook(); //工作簿
Worksheet sheet = workbook.Worksheets[0]; //工作表
sheet.AutoFitColumns();
Cells cells = sheet.Cells;//单元格
Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
styleTitle.Font.Name = "宋体";//文字字体
styleTitle.Font.Size = 14;//文字大小
styleTitle.Font.IsBold = false;//粗体
//样式2
Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style2.Font.Name = "宋体";//文字字体
style2.Font.Size = 10;//文字大小
style2.Font.IsBold = false;//粗体
style2.IsTextWrapped = true;//单元格内容自动换行
style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style3.Font.Name = "宋体";//文字字体
style3.Font.Size = 10;//文字大小
style3.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
if (!string.IsNullOrEmpty(title))
{
int i = Columns.Count;
if (ignorColList != null)
{
i = i - ignorColList.Count;
}
cells.Merge(0, 0, 1,i);//合并单元格
cells[0, 0].PutValue(title);//填写内容
cells[0, 0].SetStyle(styleTitle);
}
int start = string.IsNullOrEmpty(title) ? 0 : 1;
int j = 0;
if (showHeader)
{
for (var i = 0; i < Columns.Count; i++)
{
var c = Columns[i];
if (ignorColList.Contains(c.DataField))
{
continue;
}
cells[start, j].PutValue(c.HeaderText);
cells[start, j].SetStyle(style2);
j++;
}
}
for (var q = 0; q < table.Rows.Count; q++)
{
j = 0;
var row = table.Rows[q];
for (var i = 0; i < Columns.Count; i++)
{
var c = Columns[i];
if (ignorColList.Contains(c.DataField))
{
continue;
}
cells[start + 1 + q, j].PutValue(row[c.DataField]);
cells[start + 1 + q, j].SetStyle(style3);
cells.SetRowHeight(start + 1 + q, 25);
j++;
}
}
workbook.Save(string.Format("report.xls"), Aspose.Cells.SaveType.OpenInExcel, Aspose.Cells.FileFormatType.Excel2003, Response);
Response.Flush();
Response.End();
}
//初始控件
protected virtual void InitPage()
{
}
private string _splitchar=string.Empty;
public string SplitChar
{
set
{
_splitchar = "_";
}
get
{
if (string.IsNullOrEmpty(_splitchar))
{
_splitchar = "_";
}
return _splitchar;
}
}
private static string regexCtlValObj = @"w{3,5}?_(?<PROP>w*)_(?<TAG>(wd{1,2})*)$";
public static Regex regEx = new Regex(regexCtlValObj, RegexOptions.IgnoreCase);
/// <summary>
/// 设置查询条件
/// </summary>
/// <returns></returns>

protected virtual Framework.QueryInfo SetInfo()
{
info = new Framework.QueryInfo();
info.Parameters.Clear();
System.Collections.Hashtable aa = new System.Collections.Hashtable();

foreach (string key in Request.QueryString.Keys)
{
if (regEx.IsMatch(key))
{
Match m = regEx.Match(key);
string g = m.Groups["TAG"].Value;
if (string.IsNullOrEmpty(Request.QueryString[key]))
{
continue;
}
if (string.IsNullOrEmpty(g))
{
if (!aa.Contains(m.Groups["PROP"].Value))
{
aa.Add(m.Groups["PROP"].Value, Request.QueryString[key]);
}
}
else
{
if (!aa.Contains(m.Groups["PROP"].Value + "_" + g))
{
aa.Add(m.Groups["PROP"].Value + "_" + g, Request.QueryString[key]);
}

}
}
}
info.AddParam(aa);
// info.AddParam(WebHelper.Web.UI.BindingPanel.SaveData<System.Collections.Hashtable>(Request.QueryString, 0));
info = ExceptRecord(info);
return info;
}
/// <summary>
/// 排除默认值记录
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
protected virtual Framework.QueryInfo ExceptRecord(Framework.QueryInfo info)
{
if (!string.IsNullOrEmpty(Request["defaultId"]))
{
info.AddParam("defaultId", Request["defaultId"], " and Id!=:defaultId");
}
return info;
}
public virtual void ChangeList(Framework.QueryInfo infoList)
{

}
protected virtual void setDataSource()
{
if ((string.IsNullOrEmpty(info.QueryObject) && string.IsNullOrEmpty(info.CustomSQL)))
{

info.QueryObject = typeof(T).Name;

}
if (svc == null)
{
info = Dao.FindByQueryInfo(info);
}
else
{
info = svc.FindByQueryInfo(info);
}
}
public virtual void SetPositionCondition(Framework.QueryInfo tinfo)
{
if (!CurrentUser.IsSuperUser)
{
if (CurrentUser.PositionUserIds != null && CurrentUser.PositionUserIds.Length > 0)
{
if (CurrentUser.PositionUserIds.Length == 1)
{
tinfo.AddParam("CreateUid", CurrentUser.PositionUserIds[0]);
}
else
{
tinfo.AddParam("CreateUid", CurrentUser.PositionUserIds, " and CreateUid in(:CreateUid) ");
}
}
else
{
tinfo.CancelList = true;
}
}
}
/// <summary>
/// 获取列表信息
/// </summary>
protected virtual void GetList()
{
try
{
info = SetInfo();
if (info.CheckPosition)
{
//设置岗位过滤
SetPositionCondition(info);
}
info.TotalCount = 1;
#region 分页信息

int pageIndex = 0;
try
{
info.PageSize = int.Parse(HttpContext.Current.Request["rows"]);
pageIndex = int.Parse(HttpContext.Current.Request["page"]);
info.StartRecord = (pageIndex - 1) * info.PageSize;
if (!string.IsNullOrEmpty(Request["sidx"]))
{
info.OrderBy.Add(Request["sidx"] + " " + Request["sord"]);
}
}
catch (Exception ex)
{


}
#endregion

if (!info.CancelList)
{
setDataSource();
ChangeList(info);
}
else
{
info.List = new ArrayList();
}
if (info.Transformer != null)
{
System.Data.DataSet ds = info.Transformer as System.Data.DataSet;
string s = ToJson.JQDataset2Json(ds, info.PageSize, info.TotalCount, pageIndex);
HttpContext.Current.Response.Write(s);
}
else
{
int totalPage = info.TotalCount / info.PageSize;
if (info.TotalCount % info.PageSize != 0)
{
totalPage++;
}

JGridJson d = new JGridJson(totalPage, info.List, pageIndex,info.TotalCount);

HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(d));
}
// HttpContext.Current.ApplicationInstance.CompleteRequest();

}
catch (Exception ex)
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add(new
{
ErrorCode = -999,
Message = ex.Message
});
Utility.JSUtil.log(ex);
ErrorSignal.FromCurrentContext().Raise(ex);
DataGridJson d = new DataGridJson(0, list);
HttpContext.Current.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(d));
}
finally
{
HttpContext.Current.Response.End();
}
}
}
}

原文地址:https://www.cnblogs.com/kexb/p/4893590.html