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.IO; using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.POIFS; using NPOI.Util; namespace DSWeb.WMS { public partial class VwWmsStocktakeList : System.Web.UI.Page { T_ALL_DA T_ALL_DA = new T_ALL_DA(); //grid private string strColumnVal; public string strUserID;//用户GID private string strType = "51";//Grid模块号 public string strCompanyID;//公司GID public string strShowName;//用户显示名 public string strDeptName;//部门名称 //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; } if (Session["SHOWNAME"] != null) { strShowName = Session["SHOWNAME"].ToString(); } if (Session["COMPANYID"] != null) { strCompanyID = Session["COMPANYID"].ToString(); } if (Session["DEPTNAME"] != null) { strDeptName = Session["DEPTNAME"].ToString(); } // if (Request.QueryString["id"] != null) { this.hid_associatedno.Value = Request.QueryString["id"].ToString(); } else { this.hid_associatedno.Value = ""; } //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 //加载下拉框 Page.ClientScript.RegisterStartupScript(this.GetType(), "key0", ""); } #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 = "VwWmsStocktakeListGridSetting"; 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 = "CLIENTNAME"; break; case "仓库": gridColumnEntity.Name = "STORAGENAME"; break; case "品名": gridColumnEntity.Name = "GOODSNAME"; break; case "入库数量": gridColumnEntity.Name = "hj_GOODSRKSL"; break; case "出库数量": gridColumnEntity.Name = "hj_GOODSPFSL_OUT"; break; case "库存数量": gridColumnEntity.Name = "hj_GOODSSTOCK"; break; case "入库件数": gridColumnEntity.Name = "hj_GOODSPACK"; break; case "出库件数": gridColumnEntity.Name = "hj_GOODSPACKPFSL"; break; case "库存件数": gridColumnEntity.Name = "hj_GOODSPACKSTOCK"; 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 ibExcel_Click(object sender, ImageClickEventArgs e) { string ExcelHeader = "货主\t仓库\t入库日期\t提单号\t合同号\t备案清单号\t等级\t品名\t牌号\t规格\t【片数/件】\t入库件数\t实际入库件数\t完税件数\t出库件数\t库存件数\t入库数量\t实际入库量\t出库数量\t库存数量\t计费单位"; string SQLExcel = this.hid_SQLExcel.Value.Trim(); if (SQLExcel != "") { if (SQLExcel.IndexOf(",。,") > -1) { string[] itemSQL = SQLExcel.Trim().Split(new string[] { ",。," }, StringSplitOptions.RemoveEmptyEntries); DataSet ds = T_ALL_DA.GetAllSQL(itemSQL[0].ToString().Trim()); if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { ExcelDA ExcelDA = new EntityDA.ExcelDA(); MemoryStream ms = new MemoryStream(); IWorkbook workbook = new HSSFWorkbook(); for (int m = 0; m < ds.Tables[0].Rows.Count; m++) { string sSQL = itemSQL[1].ToString().Trim() + " and CUSTOMERNAME='" + ds.Tables[0].Rows[m]["CUSTOMERNAME"].ToString().Trim() + "'" + itemSQL[2].ToString().Trim() + " and CUSTOMERNAME='" + ds.Tables[0].Rows[m]["CUSTOMERNAME"].ToString().Trim() + "'" + itemSQL[3].ToString().Trim(); DataSet ds2 = T_ALL_DA.GetAllSQL(sSQL); if (ds2 != null) { if (ds2.Tables[0].Rows.Count > 0) { using (ds2.Tables[0]) { //Size = “((NPOI.HSSF.UserModel.HSSFWorkbook)(workbook)).Workbook.Size”引发了“System.NullReferenceException”类型的异常 //IWorkbook workbook = new HSSFWorkbook(); string sheetname = ds.Tables[0].Rows[m]["CUSTOMERNAME"].ToString().Trim(); ISheet sheet = workbook.CreateSheet(sheetname); IRow headerRow = sheet.CreateRow(0); IFont font12 = workbook.CreateFont(); font12.FontHeightInPoints = 12; font12.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 IFont font10 = workbook.CreateFont(); font10.FontHeightInPoints = 10; font10.Boldweight = (short)FontBoldWeight.BOLD;//- 加粗 // for (int j = 0; j < ds2.Tables[0].Columns.Count; j++) { headerRow.Height = 25 * 20; ICellStyle cell = headerRow.CreateCell(j).CellStyle;//- 标题行 cell.SetFont(font12); cell.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index; cell.FillForegroundColor = HSSFColor.OLIVE_GREEN.WHITE.index; cell.FillPattern = FillPatternType.SOLID_FOREGROUND; //- 居中 cell.VerticalAlignment = VerticalAlignment.CENTER; cell.Alignment = HorizontalAlignment.CENTER; //- 细边缘 cell.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN; cell.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN; cell.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN; cell.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN; cell.BottomBorderColor = HSSFColor.BLACK.index; cell.LeftBorderColor = HSSFColor.BLACK.index; cell.RightBorderColor = HSSFColor.BLACK.index; cell.TopBorderColor = HSSFColor.BLACK.index; } // string[] txtval = ExcelHeader.Trim().Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < txtval.Length; i++) { headerRow.CreateCell(i).SetCellValue(txtval[i].ToString().Trim()); } // for (int i = 1; i < ds2.Tables[0].Rows.Count; i++) { for (int k = 0; k < ds2.Tables[0].Columns.Count; k++) { IRow headerRow1 = sheet.CreateRow(i); sheet.SetColumnWidth(k, 30 * 256); ICellStyle cell = headerRow1.CreateCell(k).CellStyle;// cell.SetFont(font10); cell.FillPattern = FillPatternType.NO_FILL; //- 居中 cell.VerticalAlignment = VerticalAlignment.CENTER; cell.Alignment = HorizontalAlignment.LEFT; //- 细边缘 cell.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN; cell.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN; cell.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN; cell.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN; cell.BottomBorderColor = HSSFColor.BLACK.index; cell.LeftBorderColor = HSSFColor.BLACK.index; cell.RightBorderColor = HSSFColor.BLACK.index; cell.TopBorderColor = HSSFColor.BLACK.index; } } // int rowIndex = 1; foreach (DataRow row in ds2.Tables[0].Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in ds2.Tables[0].Columns) { //导出内容 dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); } rowIndex++; } } } } } // workbook.Write(ms); ms.Flush(); ms.Position = 0; ExcelDA.RenderToBrowser(ms, Context, "report.xls"); } } } else { DataSet ds = T_ALL_DA.GetAllSQL(SQLExcel); if (ds != null) { ExcelDA ExcelDA = new EntityDA.ExcelDA(); MemoryStream ms = ExcelDA.RenderToExcel(ds.Tables[0], ExcelHeader.Trim()); ExcelDA.RenderToBrowser(ms, Context, "report.xls"); } } } } } }