using BLL; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Data; using System.IO; using System.Linq; using System.Web.UI.WebControls; namespace FineUIPro.Web.KPI { public partial class KPI : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower(); StartTime.Text = DateTime.Now.AddMonths(-11).ToString(StartTime.DateFormatString); EndTime.Text = DateTime.Now.ToString(EndTime.DateFormatString); } } protected void btnExport_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = rootPath + "KPIData.xlsx"; //导出文件 string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string ReportFileName = filePath + "out.xlsx"; FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read); XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); IDataFormat dataformat = hssfworkbook.CreateDataFormat(); ICellStyle styleQfw = hssfworkbook.CreateCellStyle(); styleQfw.DataFormat = dataformat.GetFormat("0.00"); ICellStyle styleDate = hssfworkbook.CreateCellStyle(); styleDate.DataFormat = dataformat.GetFormat("yyyy/m/d"); DateTime minData = DateTime.Parse(StartTime.Text + "-01"); DateTime maxData = DateTime.Parse(EndTime.Text + "-01").AddMonths(1); XSSFSheet wsyear = (XSSFSheet)hssfworkbook.GetSheet("Sheet1"); var kpiData = Funs.DB.ContractorKpi0View.Where(x => x.Submit_Date >= minData && x.Submit_Date < maxData).ToList(); if (kpiData.Count > 0) { var rowIndex = 1; foreach (var item in kpiData) { if (wsyear.GetRow(rowIndex) == null) wsyear.CreateRow(rowIndex); if (wsyear.GetRow(rowIndex).GetCell(0) == null) wsyear.GetRow(rowIndex).CreateCell(0); wsyear.GetRow(rowIndex).GetCell(0).SetCellValue(item.SES); if (wsyear.GetRow(rowIndex).GetCell(1) == null) wsyear.GetRow(rowIndex).CreateCell(1); wsyear.GetRow(rowIndex).GetCell(1).SetCellValue(item.Contract_No); if (wsyear.GetRow(rowIndex).GetCell(2) == null) wsyear.GetRow(rowIndex).CreateCell(2); wsyear.GetRow(rowIndex).GetCell(2).SetCellValue(item.Contractor); if (wsyear.GetRow(rowIndex).GetCell(3) == null) wsyear.GetRow(rowIndex).CreateCell(3); wsyear.GetRow(rowIndex).GetCell(3).SetCellValue(item.CPT_No); if (wsyear.GetRow(rowIndex).GetCell(4) == null) wsyear.GetRow(rowIndex).CreateCell(4); wsyear.GetRow(rowIndex).GetCell(4).SetCellValue(float.Parse(item.Net_Value.ToString())); wsyear.GetRow(rowIndex).GetCell(4).CellStyle = styleQfw; if (wsyear.GetRow(rowIndex).GetCell(5) == null) wsyear.GetRow(rowIndex).CreateCell(5); wsyear.GetRow(rowIndex).GetCell(5).SetCellValue(float.Parse(item.Tax_Value.Value.ToString())); wsyear.GetRow(rowIndex).GetCell(5).CellStyle = styleQfw; if (wsyear.GetRow(rowIndex).GetCell(6) == null) wsyear.GetRow(rowIndex).CreateCell(6); wsyear.GetRow(rowIndex).GetCell(6).SetCellValue(float.Parse(item.Deduction.Value.ToString())); wsyear.GetRow(rowIndex).GetCell(6).CellStyle = styleQfw; if (wsyear.GetRow(rowIndex).GetCell(7) == null) wsyear.GetRow(rowIndex).CreateCell(7); wsyear.GetRow(rowIndex).GetCell(7).SetCellValue(float.Parse(item.Quotation.Value.ToString())); wsyear.GetRow(rowIndex).GetCell(7).CellStyle = styleQfw; if (wsyear.GetRow(rowIndex).GetCell(8) == null) wsyear.GetRow(rowIndex).CreateCell(8); wsyear.GetRow(rowIndex).GetCell(8).SetCellValue(float.Parse(item.Rate.Value.ToString())); wsyear.GetRow(rowIndex).GetCell(8).CellStyle = styleQfw; if (wsyear.GetRow(rowIndex).GetCell(9) == null) wsyear.GetRow(rowIndex).CreateCell(9); wsyear.GetRow(rowIndex).GetCell(9).SetCellValue(item.DateMonth); if (wsyear.GetRow(rowIndex).GetCell(10) == null) wsyear.GetRow(rowIndex).CreateCell(10); if (item.Submit_Date != null) { wsyear.GetRow(rowIndex).GetCell(10).SetCellValue((DateTime)item.Submit_Date); wsyear.GetRow(rowIndex).GetCell(10).CellStyle = styleDate; } if (wsyear.GetRow(rowIndex).GetCell(11) == null) wsyear.GetRow(rowIndex).CreateCell(11); wsyear.GetRow(rowIndex).GetCell(11).SetCellValue(item.Section); if (wsyear.GetRow(rowIndex).GetCell(12) == null) wsyear.GetRow(rowIndex).CreateCell(12); wsyear.GetRow(rowIndex).GetCell(12).SetCellValue(item.Dis); if (wsyear.GetRow(rowIndex).GetCell(13) == null) wsyear.GetRow(rowIndex).CreateCell(13); if (item.SES_CONF_Format != null) { wsyear.GetRow(rowIndex).GetCell(13).SetCellValue((DateTime)item.SES_CONF_Format.Value); wsyear.GetRow(rowIndex).GetCell(13).CellStyle = styleDate; } rowIndex++; } wsyear.ForceFormulaRecalculation = true; using (FileStream filess = File.OpenWrite(ReportFileName)) { hssfworkbook.Write(filess); } FileInfo filet = new FileInfo(ReportFileName); Response.Clear(); Response.Charset = "UTF-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(StartTime.Text + "~" + EndTime.Text + string.Format("kpiData.xlsx"))); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } } #region 权限设置 /// /// 菜单按钮权限 /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.KPIMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnOut)) { this.btnExport.Hidden = false; } else { this.btnExport.Hidden = true; } } } #endregion } }