1前台:{ 2 id: 'print', iconCls: 'icon-print', text: '导出', handler: function () { 3 var filterRules = $('#dg').datagrid('options').filterRules; 4 var filters = ""; 5 if (filterRules.length > 0) { 6 filters = "["; 7 for (var i = 0; i < filterRules.length; i++) { 8 if (i > 0) { 9 filters += ","; 10 } 11 filters += "{\"field\":\"" + filterRules[i].field + "\",\"op\":\"" + filterRules[i].op + "\",\"value\":\"" + filterRules[i].value + "\"}"; 12 } 13 filters += "]"; 14 } 15 window.open("/Test/Test/TestDataQuery?export=y&filterRules=" + filters); 16 } 17 } 18 19后台: public ActionResult TestDataQuery() 20 { private Entitie db = new Entitie(); 21 //查询语句 22 string sql = "select t.ID,t.TEST1,t.TEST2,t.TEST3,t.TEST4,t.TEST5 from test t"; 23 //----导出参数 24 string excelname = "测试表"; 25 string[] excelcol = { "测试1", "测试2", "测试3", "测试4", "测试5" }; 26 DataTable dt = DbHelperOrcl.GetDTQuery(db.Database.Connection.ConnectionString, sql);/ if (Request["export"] == "y") { GridExport(dt, excelname, excelcol); } 27 string jsonData = GetJeJsonData(db.Database.Connection.ConnectionString, sql, Request, excelname, excelcol); 28 return Content(jsonData); 29 30 } 31 32导出:public static void GridExport(DataTable dt, string excelname, string[] excelcol) 33 { 34 HSSFWorkbook workbook = new HSSFWorkbook(); 35 ISheet sheet = workbook.CreateSheet("Sheet1"); 36 ICell cells; 37 //-------------------------------------------------------------------- 38 ICellStyle styles = GetStyles(workbook);//普通样式 39 ICellStyle styles_zd = GetzdStyles(workbook);//表头字段样式 40 //----------------------标题----------------------------------------------- 41 //CellRangeAddress四个参数为:起始行,结束行,起始列,结束列 42 sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, dt.Columns.Count-1)); 43 //----------------------表头----------------------------------------------- 44 cells = sheet.CreateRow(0).CreateCell(0); 45 cells.SetCellValue(excelname); 46 cells.CellStyle = styles_zd; 47 int col_head = 0; 48 for (int j = 0; j < dt.Columns.Count; j++) 49 { 50 string name = dt.Columns[j].ColumnName; 51 if (col_head == 0) 52 { 53 cells = sheet.CreateRow(1).CreateCell(col_head); 54 } 55 else 56 { 57 cells = sheet.GetRow(1).CreateCell(col_head); 58 } 59 cells.SetCellValue(name); 60 cells.CellStyle = styles_zd; 61 col_head++; 62 if (name == "ID" || name == "id") 63 { 64 sheet.SetColumnHidden(0, true); 65 } 66 } 67 //----------------------数据----------------------------------------------- 68 int rowno = 1; 69 foreach (DataRow dr in dt.Rows) 70 { 71 int cellno = 0; 72 for (int i = 0; i < dt.Columns.Count; i++) 73 { 74 object value = dr[i]; 75 //如果非空,则赋给对象的属性 76 if (cellno == 0) 77 { 78 cells = sheet.CreateRow(rowno).CreateCell(cellno); 79 } 80 else 81 { 82 cells = sheet.GetRow(rowno).CreateCell(cellno); 83 } 84 if (value != DBNull.Value) 85 { 86 cells.SetCellValue(value.ToString()); 87 } 88 cells.CellStyle = styles; 89 cellno += 1; 90 } 91 rowno += 1; 92 } 93 HttpResponse response= System.Web.HttpContext.Current.Response; 94 string filename = excelname!=null? excelname+".xls" : "数据.xls"; 95 response.ContentType = "application/vnd.ms-excel"; 96 response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename)); 97 response.Clear(); 98 response.BinaryWrite(WriteToStream(workbook).GetBuffer()); 99 response.End(); 100 }不喜勿喷!!!纯属个人经验
vs2015 JS+EasyUI+C# Excel导出
Wesley13
2021-10-11
1116 0 0
点赞
收藏
评论区
加载中...