public HttpResponseBase Export() { InspectionReportQuery query= new InspectionReportQuery(); string json = string.Empty; try { if (!string.IsNullOrEmpty(Request.Params["brand"])) { query.brand_id = Convert.ToUInt32(Request.Params["brand"]); } if (!string.IsNullOrEmpty(Request.Params["name_code"])) { query.name_code = Request.Params["name_code"].ToString().Trim(); } if (!string.IsNullOrEmpty(Request.Params["certificate_type1"])) { query.certificate_type1 = (Request.Params["certificate_type1"]); } if (!string.IsNullOrEmpty(Request.Params["certificate_type2"])) { query.certificate_type2 = (Request.Params["certificate_type2"]); } if (!string.IsNullOrEmpty(Request.Params["start_time"])) { query.start_time = Convert.ToDateTime(Convert.ToDateTime(Request.Params["start_time"]).ToString("yyyy-MM-dd 00:00:00")); } if (!string.IsNullOrEmpty(Request.Params["end_time"])) { query.end_time = Convert.ToDateTime(Convert.ToDateTime(Request.Params["end_time"]).ToString("yyyy-MM-dd 23:59:59")); } if (!string.IsNullOrEmpty(Request.Params["last_day"])) { query.last_day = Convert.ToInt32(Request.Params["last_day"]); } if (!string.IsNullOrEmpty(Request.Params["search_date"])) { query.search_date = Convert.ToInt32(Request.Params["search_date"]); } DataTable _newDt = new DataTable(); string newExcelName = string.Empty; _newDt.Columns.Add("商品編號", typeof(string)); _newDt.Columns.Add("證書大類", typeof(string)); _newDt.Columns.Add("證書小類", typeof(string)); _newDt.Columns.Add("有效期限", typeof(string)); _newDt.Columns.Add("說明", typeof(string)); _newDt.Columns.Add("檔案名稱", typeof(string)); _inspectionReport = new BLL.gigade.Mgr.InspectionReportMgr(mySqlConnectionString); DataTable _dt = _inspectionReport.Export(query); foreach (DataRow row in _dt.Rows) { DataRow newRow=_newDt.NewRow(); newRow[0] = row["product_id"].ToString(); newRow[1] = row["code1"].ToString(); newRow[2] = row["code2"].ToString(); newRow[3] = row["certificate_expdate"].ToString(); newRow[4] = row["certificate_desc"]; newRow[5] = row["certificate_filename"].ToString(); _newDt.Rows.Add(newRow); } string[]colname=new string[_newDt.Columns.Count]; string filename = "InspectionReport" +DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; newExcelName = Server.MapPath(excelPath_export) + filename; for (int i = 0; i < _newDt.Columns.Count; i++) { colname[i] = _newDt.Columns[i].ColumnName; } if (System.IO.File.Exists(newExcelName)) { System.IO.File.Delete(newExcelName); } ExcelHelperXhf.ExportDTtoExcel(_newDt, "", newExcelName); json = "{success:true,ExcelName:\'" + filename + "\'}"; } 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); json = "{success:false}"; } this.Response.Clear(); this.Response.Write(json); this.Response.End(); return this.Response; }