MVC的匯出

來源:互聯網
上載者:User

標籤:

MVC  的匯出
//前台 傳入的查詢條件
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的匯出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.