protected void btnImport_Click(object sender, EventArgs e) { string strPrintId = this.strPrintId.Value; TMisMonitorSubtaskVo objSubTask = new TMisMonitorSubtaskVo(); objSubTask.TASK_ID = strPrintId; DataTable dtSub = new TMisMonitorSubtaskLogic().SelectByTable(objSubTask); string strSampleIDs = ""; for (int i = 0; i < dtSub.Rows.Count; i++) { GetPoint_UnderTask(dtSub.Rows[i]["ID"].ToString(), ref strSampleIDs); } //获取基本信息 DataTable dt = new TMisMonitorSampleInfoLogic().getSamplingAllocationSheetInfoBySampleId(strSampleIDs, "021", "0"); int iPageCount = dt.Rows.Count / 17; if (dt.Rows.Count % 17 != 0) { iPageCount += 1; } FileStream file = new FileStream(HttpContext.Current.Server.MapPath("template/QHDSamplingCode.xls"), FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(file); //sheet复制 for (int k = 1; k < iPageCount; k++) { hssfworkbook.CloneSheet(0); hssfworkbook.SetSheetName(k, "Sheet" + (k + 1).ToString()); } for (int m = 1; m <= iPageCount; m++) { ISheet sheet = hssfworkbook.GetSheet("Sheet" + m.ToString()); sheet.GetRow(23).GetCell(0).SetCellValue("样品交接数量: " + dt.Rows.Count.ToString()); sheet.GetRow(2).GetCell(0).SetCellValue(string.Format(" 采 样 日 期: {0} 年 {1} 月 {2} 日", DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString())); sheet.GetRow(3).GetCell(0).SetCellValue(string.Format(" 样品交接日期: {0} 年 {1} 月 {2} 日 {3} 点 {4} 分", DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString(), DateTime.Now.Hour.ToString(), DateTime.Now.Minute.ToString())); sheet.GetRow(1).GetCell(6).SetCellValue(string.Format("第 {0} 页 共 {1} 页", m.ToString(), iPageCount.ToString())); DataTable dtNew = new DataTable(); dtNew = dt.Copy(); dtNew.Clear(); for (int n = (m - 1) * 17; n < m * 17; n++) { if (n >= dt.Rows.Count) { break; } dtNew.ImportRow(dt.Rows[n]); } for (int i = 0; i < dtNew.Rows.Count; i++) { string strItmeNum = ""; TMisMonitorResultVo objResult = new TMisMonitorResultVo(); objResult.SAMPLE_ID = dtNew.Rows[i]["ID"].ToString(); DataTable dtResult = new TMisMonitorResultLogic().SelectByTable(objResult); for (int j = 0; j < dtResult.Rows.Count; j++) { TBaseItemInfoVo objItem = new TBaseItemInfoVo(); objItem.ID = dtResult.Rows[j]["ITEM_ID"].ToString(); objItem.IS_SAMPLEDEPT = "否"; DataTable dtItem = new TBaseItemInfoLogic().SelectByTable(objItem); if (dtItem.Rows.Count > 0 && dtItem.Rows[0]["ITEM_NUM"].ToString().Length > 0) { strItmeNum += (strItmeNum.Length > 0) ? "," + dtItem.Rows[0]["ITEM_NUM"].ToString() : dtItem.Rows[0]["ITEM_NUM"].ToString(); } } sheet.GetRow(i + 6).GetCell(0).SetCellValue((i + 1).ToString()); sheet.GetRow(i + 6).GetCell(1).SetCellValue(dtNew.Rows[i]["SAMPLE_CODE"].ToString()); sheet.GetRow(i + 6).GetCell(6).SetCellValue(strItmeNum); } } using (MemoryStream stream = new MemoryStream()) { hssfworkbook.Write(stream); HttpContext curContext = HttpContext.Current; // 设置编码和附件格式 curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = Encoding.UTF8; curContext.Response.Charset = ""; curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("水质样品交接记录表.xls", Encoding.UTF8)); curContext.Response.BinaryWrite(stream.GetBuffer()); curContext.Response.End(); } }
protected void btnImport_Click(object sender, EventArgs e) { string strPrintId = this.strPrintId.Value; string strCompanyName = "郑州市环保局"; TMisMonitorTaskVo objTask = new TMisMonitorTaskLogic().Details(strPrintId); if (objTask.CLIENT_COMPANY_ID.Length > 0) { strCompanyName = new i3.BusinessLogic.Channels.Base.Company.TBaseCompanyInfoLogic().Details(objTask.CLIENT_COMPANY_ID).COMPANY_NAME; } TMisMonitorSubtaskVo objSubTask = new TMisMonitorSubtaskVo(); objSubTask.TASK_ID = strPrintId; DataTable dtSub = new TMisMonitorSubtaskLogic().SelectByTable(objSubTask); string strSampleIDs = ""; for (int i = 0; i < dtSub.Rows.Count; i++) { GetPoint_UnderTask(dtSub.Rows[i]["ID"].ToString(), ref strSampleIDs); } //获取基本信息 DataTable dt = new TMisMonitorSampleInfoLogic().getSamplingAllocationSheetInfoBySampleId(strSampleIDs, "021", "0"); int iPageCount = dt.Rows.Count / 7; if (dt.Rows.Count % 7 != 0) { iPageCount += 1; } FileStream file = new FileStream(HttpContext.Current.Server.MapPath("template/QHDSamplingCode.xls"), FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(file); //sheet复制 for (int k = 1; k < iPageCount; k++) { hssfworkbook.CloneSheet(0); hssfworkbook.SetSheetName(k, "Sheet" + (k + 1).ToString()); } for (int m = 1; m <= iPageCount; m++) { ISheet sheet = hssfworkbook.GetSheet("Sheet" + m.ToString()); sheet.GetRow(2).GetCell(0).SetCellValue("委托单位: " + strCompanyName); sheet.GetRow(2).GetCell(4).SetCellValue("任务编号: " + objTask.TICKET_NUM); DataTable dtNew = new DataTable(); dtNew = dt.Copy(); dtNew.Clear(); for (int n = (m - 1) * 7; n < m * 7; n++) { if (n >= dt.Rows.Count) { break; } dtNew.ImportRow(dt.Rows[n]); } for (int i = 0; i < dtNew.Rows.Count; i++) { sheet.GetRow(i + 4).GetCell(0).SetCellValue((i + 1).ToString()); if (objTask.SAMPLE_SOURCE == "抽样") { sheet.GetRow(i + 4).GetCell(1).SetCellValue(dtNew.Rows[i]["SAMPLE_NAME"].ToString()); } else { sheet.GetRow(i + 4).GetCell(1).SetCellValue(dtNew.Rows[i]["SAMPLE_NAME"].ToString()); } sheet.GetRow(i + 4).GetCell(3).SetCellValue(dtNew.Rows[i]["MONITOR_TYPE_NAME"].ToString()); sheet.GetRow(i + 4).GetCell(4).SetCellValue(dtNew.Rows[i]["ITEM_NAME"].ToString()); } } using (MemoryStream stream = new MemoryStream()) { hssfworkbook.Write(stream); HttpContext curContext = HttpContext.Current; // 设置编码和附件格式 curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = Encoding.UTF8; curContext.Response.Charset = ""; curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("水质样品交接记录表.xls", Encoding.UTF8)); curContext.Response.BinaryWrite(stream.GetBuffer()); curContext.Response.End(); } }