using BLL; using NPOI.HSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Net; using System.Web; namespace FineUIPro.Web.Personal { public partial class TestRunMonthSummary : PageBase { /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Funs.DropDownPageSize(this.ddlPageSize); if (this.CurrUser != null && this.CurrUser.PageSize.HasValue) { Grid1.PageSize = this.CurrUser.PageSize.Value; } this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 this.BindGrid(); ////权限按钮方法 this.GetButtonPower(); } } /// /// 绑定数据 /// private void BindGrid() { string strSql = @"SELECT summary.TestRunMonthSummaryId,summary.ProjectId,summary.UserId,summary.RaiseDate,summary.Major,summary.ProcessName,summary.ProblemDescription,summary.HandleMethod,summary.ExperienceOrSuggestion,Users.UserName,case when summary.ProjectId='0' then '本部' else Project.ProjectName end as ProjectName " + @" From dbo.Person_TestRunMonthSummary AS summary" + @" LEFT JOIN Sys_User AS Users ON Users.UserId=summary.UserId" + @" LEFT JOIN Base_Project AS Project ON Project.ProjectId=summary.ProjectId" + @" WHERE 1=1"; List listStr = new List(); if (this.CurrUser.UserId != BLL.Const.sysglyId && this.CurrUser.UserId != BLL.Const.hfnbdId) { strSql += " AND summary.UserId = @UserId"; listStr.Add(new SqlParameter("@UserId", this.CurrUser.UserId)); } if (!string.IsNullOrEmpty(this.txtUserName.Text.Trim())) { strSql += " AND UserName LIKE @UserName"; listStr.Add(new SqlParameter("@UserName", "%" + this.txtUserName.Text.Trim() + "%")); } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.RecordCount = tb.Rows.Count; var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); } #region 分页 /// /// 分页 /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { BindGrid(); } /// /// 分页显示条数下拉框 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } /// /// 排序 /// /// /// protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e) { BindGrid(); } #endregion #region 删除数据 /// /// 批量删除数据 /// /// /// protected void btnMenuDelete_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); var summary = Person_TestRunMonthSummaryService.GetPersonTestRunMonthSummaryById(rowID); if (summary != null) { BLL.Person_TestRunMonthSummaryService.DeletePersonTestRunMonthSummary(rowID); } } BindGrid(); ShowNotify("删除数据成功!", MessageBoxIcon.Success); } else { ShowNotify("请至少选中一行!", MessageBoxIcon.Warning); } } #endregion protected void btnNew_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TestRunMonthSummaryEdit.aspx", "编辑 - "))); } #region 编辑 /// /// 编辑 /// /// /// protected void btnMenuEdit_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInParent("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string Id = Grid1.SelectedRowID; PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TestRunMonthSummaryEdit.aspx?TestRunMonthSummaryId={0}", Id, "编辑 - "))); } /// /// Grid行双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { btnMenuEdit_Click(null, null); } #endregion #region 查询 /// /// 查询 /// /// /// protected void TextBox_TextChanged(object sender, EventArgs e) { this.BindGrid(); } #endregion #region 获取按钮权限 /// /// 获取按钮权限 /// /// /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.TestRunMonthSummaryMenuId); 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.BtnDelete)) { this.btnMenuDelete.Hidden = false; } } } #endregion protected void Window1_Close(object sender, EventArgs e) { BindGrid(); } /// /// 获取整改前图片(放于Img中) /// /// /// protected string ConvertImageUrlByImage(object TestRunMonthSummaryId) { string url = string.Empty; string httpUrl = string.Empty; if (TestRunMonthSummaryId != null) { var file = BLL.AttachFileService.GetAttachFileByToKeyId(TestRunMonthSummaryId.ToString()); if (file != null) { url = BLL.UploadAttachmentService.ShowImage(Funs.SGGLUrl, file.AttachUrl); } } return url; } /// /// 获取整改前图片(放于Img中) /// /// /// protected string ConvertImageUrlByImage2(object TestRunMonthSummaryId) { string url = string.Empty; string httpUrl = string.Empty; if (TestRunMonthSummaryId != null) { var file = BLL.AttachFileService.GetAttachFileByToKeyId(TestRunMonthSummaryId.ToString() + "R"); if (file != null) { url = BLL.UploadAttachmentService.ShowImage(Funs.SGGLUrl, file.AttachUrl); } } return url; } #region 导出按钮 /// 导出按钮 /// /// /// protected void btnOut_Click(object sender, EventArgs e) { string ids = string.Empty; if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); ids += rowID + ","; } } else { ShowNotify("请至少选中一行!", MessageBoxIcon.Warning); return; } string rootPath = Server.MapPath("~/"); string initTemplatePath = string.Empty; string uploadfilepath = string.Empty; string newUrl = string.Empty; string filePath = string.Empty; initTemplatePath = Const.TestRunMonthSummaryTemplateUrl; uploadfilepath = rootPath + initTemplatePath; newUrl = uploadfilepath.Replace(".xlsx", "(" + string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ").xlsx"); File.Copy(uploadfilepath, newUrl); // 第一步:读取文件流 NPOI.SS.UserModel.IWorkbook workbook; using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read)) { workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream); } Model.SGGLDB db = Funs.DB; string[] strs = ids.Split(','); var projects = from x in db.Base_Project select x; var lists = from x in db.Person_TestRunMonthSummary where strs.Contains(x.TestRunMonthSummaryId) orderby x.RaiseDate descending select x; // 创建单元格样式 NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle.WrapText = true; var font = workbook.CreateFont(); font.FontHeightInPoints = 10; //font.FontHeightInPoints = (short)8.5;字号为小数时要转为short cellStyle.SetFont(font); // 第二步:创建新数据行 NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); int i = 1; foreach (var item in lists) { // 第二步:创建新数据行 NPOI.SS.UserModel.IRow row = sheet.GetRow(i); NPOI.SS.UserModel.ICell cell; // 添加数据 cell = row.CreateCell(0); cell.CellStyle = cellStyle; cell.SetCellValue(i.ToString()); cell = row.CreateCell(1); cell.CellStyle = cellStyle; cell.SetCellValue(BLL.UserService.GetUserNameByUserId(item.UserId)); cell = row.CreateCell(2); cell.CellStyle = cellStyle; string months = string.Empty; if (item.RaiseDate != null) { months = string.Format("{0:yyyy-MM-dd}", item.RaiseDate); } cell.SetCellValue(months); cell = row.CreateCell(3); cell.CellStyle = cellStyle; var project = projects.FirstOrDefault(x => x.ProjectId == item.ProjectId); if (project != null) { cell.SetCellValue(project.ProjectName); } else { cell.SetCellValue("本部"); } cell = row.CreateCell(4); cell.CellStyle = cellStyle; cell.SetCellValue(item.Major); cell = row.CreateCell(5); cell.CellStyle = cellStyle; cell.SetCellValue(item.ProcessName); cell = row.CreateCell(6); cell.CellStyle = cellStyle; cell.SetCellValue(item.ProblemDescription); cell = row.CreateCell(7); cell.CellStyle = cellStyle; cell.SetCellValue(item.HandleMethod); cell = row.CreateCell(8); cell.CellStyle = cellStyle; cell.SetCellValue(item.ExperienceOrSuggestion); i++; } // 第三步:写入文件流 using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write)) { workbook.Write(stream); workbook.Close(); } string fileName = Path.GetFileName(newUrl); FileInfo info = new FileInfo(newUrl); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(newUrl, 0, fileSize); Response.Flush(); Response.Close(); File.Delete(newUrl); } #endregion private void AddCellPicture(NPOI.SS.UserModel.ISheet sheet, HSSFWorkbook workbook, HSSFPatriarch patriarch, string fileUrl, int firstRow, int lastRow, int firstCol, int lastCol) { try { //校验地址 if (fileUrl == null || fileUrl == "" || fileUrl.IndexOf("/") == -1 || fileUrl.IndexOf(".") == -1) { return; } string[] arr = fileUrl.Split(new Char[] { '/' }); string fileName = arr[arr.Length - 1]; string fileLocation = HttpContext.Current.Server.MapPath("~/download"); string fullPath = Path.Combine(fileLocation, Path.GetFileName(fileName)); //本地没有,下载文件保存到本地 if (!File.Exists(fullPath)) { WebClient webClient = new WebClient(); webClient.DownloadFile(fileUrl, fullPath); } //从本地取 if (!string.IsNullOrEmpty(fullPath)) { byte[] bytes = System.IO.File.ReadAllBytes(fullPath); int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG); //HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, firstCol, firstRow, lastCol, lastRow); HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx); } return; } catch (Exception e) { throw e; } } } }