vs2015 JS+EasyUI+C# Excel导出

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 }不喜勿喷!!!纯属个人经验
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

vs2015 JS+EasyUI+C# Excel导出 - HelloWorld