標籤:
//前台 傳入的查詢條件
function DaochuExcel() { var filite = daochuFilter(); var url = "/Sku/DaochuExcel"; url += "?page=1"; url += "&rows=10000"; url += "&sidx=create_date"; url += "&sord=asc"; url += "&filters=" + JSON.stringify(filite); window.location = url; }
後台代碼
public void DaochuExcel(long page, long rows, string sidx, string sord, string filters = null) { ResultObject res = new ResultObject(); SqlFilter filt = null; if (!string.IsNullOrEmpty(filters) && !string.Equals(filters, "null", StringComparison.OrdinalIgnoreCase)) { JavaScriptSerializer j = new JavaScriptSerializer(); filt = j.Deserialize<SqlFilter>(filters); } List<SqlFilter> f = new List<SqlFilter>(); f.Add(filt); var pageParamer = new PageParamerPlus(page, rows, sidx, sord, f); PageList<Trades> list = Trades.GetPageList(userInfo, pageParamer);//查詢出 資料 var dt = ListToDataTable(list.rows);
var data = new List<List<string>>(); // 選擇欄位 data.Add(new List<string>() { "主訂單號", "客戶名稱", "制單日期"}); for (int i = 0; i < dt.Rows.Count; i++) { data.Add(new List<string>() { dt.Rows[i]["Tid"].ToString(), dt.Rows[i]["Customer_Name"].ToString(), dt.Rows[i]["Create_Date"].ToString(), }); } var workbook = new HSSFWorkbook(); var outputStream = Response.OutputStream; var sheet = workbook.CreateSheet("info"); var headRow = sheet.CreateRow(0); for (int i = 0; i < data[0].Count; i++) { headRow.CreateCell(i).SetCellValue(data[0][i]); } for (int i = 1; i < data.Count; i++) { var dataRow = sheet.CreateRow(i); for (int j = 0; j < data[i].Count; j++) { dataRow.CreateCell(j).SetCellValue(data[i][j]); } } Response.Clear(); workbook.Write(outputStream); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment;filename=export.xls"); Response.ContentEncoding = Encoding.UTF8; Response.ContentType = "applicationnd.ms-excel"; Response.Flush(); }
將泛類型集合List類轉換成DataTable
#region 將泛類型集合List類轉換成DataTable /// <summary> /// 將泛類型集合List類轉換成DataTable /// </summary> /// <typeparam name="T"> </typeparam> /// <param name="entitys">泛類型集合</param> /// <returns></returns> public static DataTable ListToDataTable<T>(List<T> entitys) { //檢查實體集合不可為空 if (entitys == null || entitys.Count < 1) { throw new Exception("需轉換的集合為空白"); } //取出第一個實體的所有Propertie Type entityType = entitys[0].GetType(); PropertyInfo[] entityProperties = entityType.GetProperties(); //產生DataTable的structure //生產代碼中,應將產生的DataTable結構Cache起來,此處略 DataTable dt = new DataTable(); for (int i = 0; i < entityProperties.Length; i++) { //dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType); dt.Columns.Add(entityProperties[i].Name); } //將所有entity添加到DataTable中 foreach (object entity in entitys) { //檢查所有的的實體都為同一類型 if (entity.GetType() != entityType) { throw new Exception("要轉換的集合元素類型不一致"); } object[] entityValues = new object[entityProperties.Length]; for (int i = 0; i < entityProperties.Length; i++) { entityValues[i] = entityProperties[i].GetValue(entity, null); } dt.Rows.Add(entityValues); } return dt; } #endregion
把json轉化成list
public static List<T> JSONStringToList<T>(string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); List<T> objs = Serializer.Deserialize<List<T>>(JsonStr); return objs; }
MVC的匯出