//获取修改记录 public string getChange(DataTable dt, JObject json, string table) { string str = ""; Switch_helper_Base_codename sc = new Switch_helper_Base_codename(); if (dt.Rows[0][sc.getColum(table)[2]] != json.Value <string>(sc.getColum(table)[2])) { str += sc.getColum(table)[0] + ":" + dt.Rows[0][sc.getColum(table)[2]] + "——>" + json.Value <string>(sc.getColum(table)[2]) + "。"; } if (dt.Rows[0][sc.getColum(table)[3]] != json.Value <string>(sc.getColum(table)[3])) { str += sc.getColum(table)[1] + ":" + dt.Rows[0][sc.getColum(table)[3]] + "——>" + json.Value <string>(sc.getColum(table)[3]) + "。"; } if (dt.Rows[0]["StartDate"] != json.Value <string>("STARTDATE")) { str += "开始时间:" + dt.Rows[0]["StartDate"] + "——>" + json.Value <string>("STARTDATE") + "。"; } if (dt.Rows[0]["EndDate"] != json.Value <string>("ENDDATE")) { str += "停用时间:" + dt.Rows[0]["EndDate"] + "——>" + json.Value <string>("ENDDATE") + "。"; } if (dt.Rows[0]["remark"] != json.Value <string>("REMARK")) { str += "备注:" + dt.Rows[0]["remark"] + "——>" + json.Value <string>("REMARK") + "。"; } return(str); }
private void export(string table) { string strWhere = string.Empty; string combo_ENABLED_S = Request["combo_ENABLED_S"]; if (combo_ENABLED_S == "null") { combo_ENABLED_S = String.Empty; } if (!string.IsNullOrEmpty(Request["CODE_S"])) { strWhere = strWhere + " and t1.Code like '%" + Request["CODE_S"] + "%'"; } if (!string.IsNullOrEmpty(Request["CNNAME_S"])) { strWhere = strWhere + " and t1.name like '%" + Request["CNNAME_S"] + "%'"; } if (!string.IsNullOrEmpty(combo_ENABLED_S)) { strWhere = strWhere + " and t1.enabled='" + combo_ENABLED_S + "'"; } strWhere = strWhere + " order by t1.code asc"; Sql.Base_codename bc = new Sql.Base_codename(); Switch_helper_Base_codename sc = new Switch_helper_Base_codename(); List <string> ColumnName = sc.getColum(table); DataTable dt = bc.ExportTable(table, strWhere); //创建Excel文件的对象 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); //添加一个导出成功sheet NPOI.SS.UserModel.ISheet sheet_S = book.CreateSheet(sc.getExcelName(table)); NPOI.SS.UserModel.IRow row1 = sheet_S.CreateRow(0); row1.CreateCell(0).SetCellValue(ColumnName[0]); row1.CreateCell(1).SetCellValue(ColumnName[1]); row1.CreateCell(2).SetCellValue("启用情况"); row1.CreateCell(3).SetCellValue("启用时间"); row1.CreateCell(4).SetCellValue("维护人"); row1.CreateCell(5).SetCellValue("维护时间"); row1.CreateCell(6).SetCellValue("停用人"); row1.CreateCell(7).SetCellValue("停用时间"); row1.CreateCell(8).SetCellValue("备注"); for (int i = 0; i < dt.Rows.Count; i++) { NPOI.SS.UserModel.IRow rowtemp = sheet_S.CreateRow(i + 1); rowtemp.CreateCell(0).SetCellValue(dt.Rows[i][ColumnName[2]].ToString()); rowtemp.CreateCell(1).SetCellValue(dt.Rows[i][ColumnName[3]].ToString()); rowtemp.CreateCell(2).SetCellValue(dt.Rows[i]["ENABLED"].ToString() == "1" ? "是" : "否"); rowtemp.CreateCell(3).SetCellValue(dt.Rows[i]["STARTDATE"].ToString()); rowtemp.CreateCell(4).SetCellValue(dt.Rows[i]["CREATEMANNAME"].ToString()); rowtemp.CreateCell(5).SetCellValue(dt.Rows[i]["CREATEDATE"].ToString()); rowtemp.CreateCell(6).SetCellValue(dt.Rows[i]["STOPMANNAME"].ToString()); rowtemp.CreateCell(7).SetCellValue(dt.Rows[i]["ENDDATE"].ToString()); rowtemp.CreateCell(8).SetCellValue(dt.Rows[i]["REMARK"].ToString()); } try { // 输出Excel string filename = sc.getExcelName(table) + ".xls"; Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", Server.UrlEncode(filename))); Response.Clear(); MemoryStream ms = new MemoryStream(); book.Write(ms); Response.BinaryWrite(ms.GetBuffer()); Response.End(); } catch (Exception e) { Console.WriteLine(e.Message); } }