using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using DSWeb.EntityDA; using DSWeb.Models; using JsonHelper; using System.Xml; using System.Data.OleDb; using WebSqlHelper; namespace DSWeb.box { public partial class boxlist : System.Web.UI.Page { BoxDA boxDA = new BoxDA(); //grid private string strColumnVal; public string strUserID;//用户GID private string strType = "1";//Grid模块号 //gridEnd protected void Page_Load(object sender, EventArgs e) { if (Session["USERID"] != null) { strUserID = Session["USERID"].ToString().Trim(); } else { Server.Transfer("~/Error/FriendError.aspx"); return; //strUserID = ""; } // //grid if (!IsPostBack) { } if (hid_setting.Value.Trim() != "") { strColumnVal = hid_setting.Value.Trim(); SaveColumnSetting(strColumnVal); hid_setting.Value = ""; } if (hid_header.Value.Trim() != "") { strColumnVal = hid_header.Value.Trim(); SaveColumnSetting(strColumnVal); hid_header.Value = ""; } //gridEnd } protected void btDel_Click(object sender, EventArgs e) { if (this.hid_id.Value.Trim() != "") { string alt = boxDA.GetBoxDel(this.hid_id.Value.Trim(), strUserID.Trim()); if (alt == "") { // Response.Write(""); return; } else { Response.Write(""); return; } } } #region Grid参数 private void SaveColumnSetting(string tempColumnVal) { JsonColumnGroupEntity jsonColumnGroupEntity = GetPostEntity(tempColumnVal); List jsonColumnEntities = new List(); UserSettingDA userSettingDA = new UserSettingDA(); UserSettingEntity userSettingEntity = userSettingDA.GetUserSettingByUserIDType(strUserID, strType); if (userSettingEntity.GID != null) { userSettingEntity.ModifiedUser = strUserID; GridEntity gridEntity = null; List gridColumnEntities = new List(); gridEntity = GetSettingXml(userSettingEntity.Xml); gridEntity.PagePreCount = jsonColumnGroupEntity.ShowCount; foreach (JsonColumnEntity jColumn in jsonColumnGroupEntity.JsonColumnEntities) { foreach (GridColumnEntity gColumn in gridEntity.Columns) { if (gColumn.Description.Equals(jColumn.col3)) { gColumn.Description = jColumn.col3; gColumn.Width = jColumn.col5; gColumn.Index = jColumn.col4; if (jColumn.col6 != -1) { gColumn.VisibleState = jColumn.col6; } //gridColumnEntities.Add(gColumn); break; } } } //Columns //gridEntity.Columns = gridColumnEntities; userSettingEntity.Xml = SaveUserSetting(gridEntity).OuterXml; int iResult = userSettingDA.UpdateUserSetting(userSettingEntity); } else { if (jsonColumnGroupEntity != null) { userSettingEntity = new UserSettingEntity(); userSettingEntity.GID = Guid.NewGuid().ToString(); userSettingEntity.Name = "BoxGridSetting"; userSettingEntity.Description = "集装箱管理Grid设置"; userSettingEntity.UserID = strUserID; userSettingEntity.Type = 1;//type值为1表示Grid string strRequestUrl = Request.Url.ToString(); string strUrl = strRequestUrl.Substring(0, strRequestUrl.LastIndexOf("/")); string strPage = strRequestUrl.Substring(strRequestUrl.LastIndexOf("/") + 1, strRequestUrl.Length - strRequestUrl.LastIndexOf("/") - 1); userSettingEntity.Url = strUrl; userSettingEntity.Page = strPage; userSettingEntity.CreateUser = strUserID; GridEntity gridEntity = new GridEntity(); gridEntity.GID = userSettingEntity.GID; gridEntity.UserID = strUserID; gridEntity.GroupBy = ""; gridEntity.DefaultPrePageCount = 15; gridEntity.PagePreCount = jsonColumnGroupEntity.ShowCount; List gridColumnEntities = new List(); foreach (JsonColumnEntity column in jsonColumnGroupEntity.JsonColumnEntities) { GridColumnEntity gridColumnEntity = new GridColumnEntity(); if (column.col6 != -1) { gridColumnEntity.Name = column.col2; gridColumnEntity.VisibleState = column.col6; } else { switch (column.col3.Trim()) { case "编码": gridColumnEntity.Name = "CODENAME"; break; case "公司简称": gridColumnEntity.Name = "NAME"; break; case "公司全称": gridColumnEntity.Name = "FULLNAME"; break; case "联系电话": gridColumnEntity.Name = "OFFICEPHONE"; break; case "传真": gridColumnEntity.Name = "FAX"; break; } gridColumnEntity.VisibleState = 0; } gridColumnEntity.Description = column.col3; gridColumnEntity.Index = column.col4; gridColumnEntity.Width = column.col5; gridColumnEntities.Add(gridColumnEntity); } //Columns gridEntity.Columns = gridColumnEntities; userSettingEntity.Xml = SaveUserSetting(gridEntity).OuterXml; int iResult = userSettingDA.InsertUserSetting(userSettingEntity); dvSaveState.InnerHtml = iResult.ToString(); } } } #region 获取列信息 /// /// 获取列信息 /// /// /// private JsonColumnGroupEntity GetPostEntity(string tempValue) { JsonColumnGroupEntity tempColumnGroupEntity = new JsonColumnGroupEntity(); try { tempColumnGroupEntity = (JsonColumnGroupEntity)JSON.Instance.ToObject(tempValue); } catch (Exception error) { throw error; } return tempColumnGroupEntity; } #endregion private XmlDocument SaveUserSetting(GridEntity gridEntity) { XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement("user-settings"); //xmlDoc.AppendChild(root); XmlElement xGid = xmlDoc.CreateElement("gid"); xGid.InnerText = gridEntity.GID; root.AppendChild(xGid); XmlElement xUserID = xmlDoc.CreateElement("userid"); xUserID.InnerText = gridEntity.UserID; root.AppendChild(xUserID); XmlElement xCreateTime = xmlDoc.CreateElement("create-time"); xCreateTime.InnerText = gridEntity.CreateTime.ToString(); root.AppendChild(xCreateTime); XmlElement xColumns = xmlDoc.CreateElement("columns"); foreach (GridColumnEntity column in gridEntity.Columns) { XmlElement xColumn = xmlDoc.CreateElement("column"); XmlElement nName = xmlDoc.CreateElement("name"); nName.InnerText = column.Name; xColumn.AppendChild(nName); XmlElement nDescription = xmlDoc.CreateElement("description"); nDescription.InnerText = column.Description; xColumn.AppendChild(nDescription); XmlElement nIndex = xmlDoc.CreateElement("index"); nIndex.InnerText = column.Index.ToString(); xColumn.AppendChild(nIndex); XmlElement nWidth = xmlDoc.CreateElement("width"); nWidth.InnerText = column.Width.ToString(); xColumn.AppendChild(nWidth); XmlElement nVisible = xmlDoc.CreateElement("visible"); nVisible.InnerText = column.VisibleState.ToString(); xColumn.AppendChild(nVisible); xColumns.AppendChild(xColumn); } root.AppendChild(xColumns); XmlElement xGroupBy = xmlDoc.CreateElement("group-by"); xGroupBy.InnerText = gridEntity.GroupBy; root.AppendChild(xGroupBy); XmlElement xPage = xmlDoc.CreateElement("page"); XmlElement nPageDefault = xmlDoc.CreateElement("page-default"); nPageDefault.InnerText = gridEntity.DefaultPrePageCount.ToString(); xPage.AppendChild(nPageDefault); XmlElement nPagePre = xmlDoc.CreateElement("page-pre"); nPagePre.InnerText = gridEntity.PagePreCount.ToString(); xPage.AppendChild(nPagePre); root.AppendChild(xPage); xmlDoc.AppendChild(root); return xmlDoc; } private GridEntity GetSettingXml(string strXml) { XmlDocument xmlSettingDoc = new XmlDocument(); GridEntity gridEntity = new GridEntity(); try { xmlSettingDoc.LoadXml(strXml); gridEntity.GID = xmlSettingDoc.ChildNodes[0].ChildNodes[0].InnerText.Trim(); gridEntity.UserID = xmlSettingDoc.ChildNodes[0].ChildNodes[1].InnerText.Trim(); gridEntity.CreateTime = DateTime.Parse(xmlSettingDoc.ChildNodes[0].ChildNodes[2].InnerText.Trim()); IList columnsEntities = new List(); int iColCount = xmlSettingDoc.ChildNodes[0].ChildNodes[3].ChildNodes.Count; for (int i = 0; i < iColCount; i++) { GridColumnEntity columnEntity = new GridColumnEntity(); //columnEntity.GID = xmlSettingDoc.ChildNodes[0].ChildNodes[3].ChildNodes[0].ToString(); columnEntity.Name = xmlSettingDoc.ChildNodes[0].ChildNodes[3].ChildNodes[i].ChildNodes[0].InnerText.Trim(); columnEntity.Description = xmlSettingDoc.ChildNodes[0].ChildNodes[3].ChildNodes[i].ChildNodes[1].InnerText.Trim(); columnEntity.Index = int.Parse(xmlSettingDoc.ChildNodes[0].ChildNodes[3].ChildNodes[i].ChildNodes[2].InnerText.Trim()); columnEntity.Width = int.Parse(xmlSettingDoc.ChildNodes[0].ChildNodes[3].ChildNodes[i].ChildNodes[3].InnerText.Trim()); columnsEntities.Add(columnEntity); } gridEntity.Columns = columnsEntities; gridEntity.GroupBy = xmlSettingDoc.ChildNodes[0].ChildNodes[4].InnerText.Trim(); gridEntity.DefaultPrePageCount = int.Parse(xmlSettingDoc.ChildNodes[0].ChildNodes[5].ChildNodes[0].InnerText.Trim()); gridEntity.PagePreCount = int.Parse(xmlSettingDoc.ChildNodes[0].ChildNodes[5].ChildNodes[1].InnerText.Trim()); } catch (Exception error) { throw (error); } return gridEntity; } public enum SqlName { NAME = 0, LOGTYPE = 1, LOGTIME = 2, LOGCONTENT = 3, USERNAME = 4 } #endregion protected void Button1_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(file.FileName)) { string fileurl = Server.MapPath("/Reports/Import/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); file.PostedFile.SaveAs(fileurl); DataSet ds = LoadDataFromExcel(fileurl, ExcelSheetName(fileurl)[0].ToString()); // B/LNO CNTR NO //Response.Write(ds.Tables[0].Rows[0]["B/LNO"].ToString()); //Response.Write(ds.Tables[0].Rows[0]["箱号"].ToString()); //Response.End(); Alert2(business.ImportBoxNO(ds.Tables[0])); } else { Alert("请选择要导入的文件"); return; } } public DataSet LoadDataFromExcel(string filePath, string table) { string strConn; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;IMEX=1;'"; using (OleDbConnection OleConn = new OleDbConnection(strConn)) { OleConn.Open(); String sql = "SELECT * FROM [" + table + "$]";//可是更改Sheet名称,比如sheet2,等等 OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn); DataSet OleDsExcle = new DataSet(); OleDaExcel.Fill(OleDsExcle); //OleConn.Close(); return OleDsExcle; } } public ArrayList ExcelSheetName(string filepath)//获取excel的sheet的名字列表 注意:名字后不带$ { ArrayList al = new ArrayList(); string strConn; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1;'"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); DataTable sheetNames = conn.GetOleDbSchemaTable (System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); conn.Close(); foreach (DataRow dr in sheetNames.Rows) { al.Add(dr[2].ToString().TrimEnd('$')); } return al; } // Methods public void Alert(string msg) { ScriptManager.RegisterClientScriptBlock(this, base.GetType(), WebSqlHelper.Common.RndNum(8), "alert('" + msg + "');", true); } public void Alert2(string msg) { ScriptManager.RegisterClientScriptBlock(this, base.GetType(), WebSqlHelper.Common.RndNum(8), "alert('" + msg + "');window.location.href = window.location.href;", true); } public void Run(string script) { ScriptManager.RegisterClientScriptBlock(this, base.GetType(), WebSqlHelper.Common.RndNum(8), script, true); } public void ShowMessageAndRedirect(string Message, string Redirect) { ScriptManager.RegisterClientScriptBlock(this, base.GetType(), WebSqlHelper.Common.RndNum(8), "alert('" + Message + "');" + (string.IsNullOrEmpty(Redirect) ? "" : "location='" + Redirect + "';"), true); } } }