public void IlocExcelList() { string json = string.Empty; IlocQuery ilocQuery = new IlocQuery(); DataTable dtExcel = new DataTable(); try { ilocQuery.lcat_id = Request.Params["Ilocid_type"]; if (!string.IsNullOrEmpty(Request.Params["search_type"]) && Request.Params["search_type"] != "null")//model中默認為F { ilocQuery.lsta_id = Request.Params["search_type"]; } else { ilocQuery.lsta_id = string.Empty; } if (!string.IsNullOrEmpty(Request.Params["searchcontent"].Trim())) { ilocQuery.loc_id = Request.Params["searchcontent"].ToString().ToUpper(); } DateTime time; if (DateTime.TryParse(Request.Params["starttime"].ToString(), out time)) { ilocQuery.starttime = time; } if (DateTime.TryParse(Request.Params["endtime"].ToString(), out time)) { ilocQuery.endtime = time; } List<IlocQuery> store = new List<IlocQuery>(); _IlocMgr = new IlocMgr(mySqlConnectionString); store = _IlocMgr.GetIlocExportList(ilocQuery); dtExcel.Columns.Add("料位編號", typeof(String)); dtExcel.Columns.Add("Hash料位", typeof(String)); dtExcel.Columns.Add("修改人員", typeof(String)); dtExcel.Columns.Add("修改時間", typeof(String)); dtExcel.Columns.Add("料位類型", typeof(String)); dtExcel.Columns.Add("料位狀態", typeof(String)); for (int i = 0; i < store.Count; i++) { DataRow newRow = dtExcel.NewRow(); newRow[0] = store[i].loc_id.ToString(); newRow[1] = store[i].hash_loc_id.ToString(); newRow[2] = store[i].change_users.ToString(); newRow[3] = store[i].change_dtim.ToString(); newRow[4] = store[i].lcat_id.ToString(); newRow[5] = store[i].lsta_id.ToString(); dtExcel.Rows.Add(newRow); } if (dtExcel.Rows.Count > 0) { string fileName = "料位維護_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; MemoryStream ms = ExcelHelperXhf.ExportDT(dtExcel, "料位維護_" + DateTime.Now.ToString("yyyyMMddHHmmss")); Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); Response.BinaryWrite(ms.ToArray()); } else { Response.Write("匯出數據不存在"); } } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); } }