標籤:des style blog http color io os 使用 ar
摘要:咳咳~入園快兩年,工作也快將近兩年了,這才打算寫自己在園中的第一篇文章,主要因為之前只是小白一個,肚子裡沒有什麼墨水,現如今,也喝了些許墨水~當然墨水來源也是受益於廣大程式猿們的技術分享,感謝,共同進步!
好了,廢話不多說,直接切入今天要分享的主題。
主題:.net中資料集合匯出為Excel
背景:如今越來越多的公司開始注重了大資料,公司開始通過資料來分析使用者的行為習慣或者某些產品給公司帶來的利害,總之這些資料都可以很好的協助公司做的更好,瞭解自身的優缺點從而更好的為使用者服務擷取更大的利益。既然公司明確了方向要做資料,那麼這些資料必然需要展示給人們來看,來做資料分析。當然,這些資料僅僅是在頁面上顯示出來,並不能滿足每個想看資料的人,他們都想根據自己的理解使用這些資料來做自己的分析。當然現在最流行的他們都想使用Excel來處理這些資料,所以我們就需要將這些資料匯出到Excel中去,來滿足眾多的使用者群體。
內容:因為俺是做.net開發的猿人,所以在這裡只分析.net中如何將資料匯出Excel文檔了。
首先,我們既然做資料,資料來源必然很多,那麼很多地方都需要做資料匯出。為了代碼的通用性,我們就需要定義一個通用的類DataExportHelper.cs 代碼如下:
public class DataExportHelper { /// <summary> /// 匯出EXCEL /// </summary> /// <typeparam name="T">集合類型</typeparam> /// <param name="data">資料集合</param> /// <param name="exportFileName">要顯示的列名(類欄位名)</param> /// <param name="columnNames"></param> public static void ExcelExport<T>(List<T> data, string exportFileName, List<string> columnNames) { StringWriter sw = GetStringWriter(data,columnNames); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); string fileName = exportFileName; //判斷瀏覽器如果是IE則對檔案名稱進行URL編碼處理 string ua = HttpContext.Current.Request.UserAgent; if (!string.IsNullOrEmpty(ua)) { if (ua.ToLower().IndexOf("msie", System.StringComparison.Ordinal)>=0) { fileName = HttpUtility.UrlEncode(exportFileName, Encoding.UTF8); } } string str = "attachment;filename= " + fileName + ".xls "; HttpContext.Current.Response.AppendHeader("Content-Disposition", str); HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Write(sw); HttpContext.Current.Response.End(); } /// <summary> /// 資料字串拼接 /// </summary> /// <typeparam name="T">集合類型</typeparam> /// <param name="data">資料集合</param> /// <param name="columnNames">要顯示的列名(類欄位名)</param> /// <returns></returns> public static StringWriter GetStringWriter<T>(List<T> data,List<string> columnNames ) { StringWriter sw = new StringWriter(); var type = typeof(T); var properties = type.GetProperties(); if (columnNames!=null&&columnNames.Count > 0) { // properties = properties.Where(t => columnNames.Contains(t.Name)).ToArray(); //為了與指定欄位順序一致,才如此迴圈 PropertyInfo[] property=new PropertyInfo[columnNames.Count]; for (int i = 0; i < columnNames.Count; i++) { property[i] = properties.FirstOrDefault(t => t.Name == columnNames[i]); } properties = property; } sw.Write(sw.NewLine); if (data != null) { //添加列名: var colName = ""; foreach (var property in properties) { colName = property.Name; if (property.CustomAttributes.LongCount() >0) { colName = property.CustomAttributes.First().ConstructorArguments.First().Value.ToString(); } sw.Write(colName + "\t"); } sw.Write(sw.NewLine); foreach (var row in data) { foreach (var property in properties) sw.Write(property.GetValue(row, null) + "\t"); sw.Write(sw.NewLine); } } sw.Close(); return sw; } }
必要的注釋代碼中都有了,這裡就不過多敘述了,從上面代碼中我們可以看到這個方法轉換出來的並不是真的Excel檔案,只是採用字串換行分割的方式來讓Excel程式識別成表格的格式。
缺點是:不能設定複雜的樣式,每次下載後開啟檔案可能都會提示“不是有效Excel檔案,是否繼續開啟?”之類的提示,你只要點是就可以了。當然開啟檔案後你另存新檔標準的Excel檔案就成為真正的Excel檔案了,所以這點可以接受的。
優點是:這種匯出方式只是通過拼接字串來合成的內容,所以執行效率上相對匯出到標準的Excel檔案的效率要提高很多,而且沒有設定格式功能也沒有大的影響,因為每個業務人員都想按照自己的想法去編輯表格的樣式。
調用方式:
protected void CreateExecl(int classId) { StudentManager marager = new StudentManager(); List<StudentModel> list = marager.StudentList(classId); string fileName = "XX班學生資料"; var listfiled = new List<string> { "Id", "Name", "Birthday" }; DataExportHelper.ExcelExport(list, fileName, listfiled); }
資料匯出後表格列顯示的順序由listfiled集合中值的順序來決定的,集合中的值都是傳入的資料集合StudentModel類中的欄位。匯出資料支援泛型,請注意,要顯示的欄位集合必須是傳入資料類型中有的欄位。
接下來我們看下如果我們想讓匯出的表格每列的標題顯示出對應的中文描述應該如何做?下面是StudentModel的定義。
public class StudentModel { [Description("學號")] public DateTime Id { get; set; } [Description("姓名")] public double Name { get; set; } [Description("生日")] public double Birthday { get; set; } }
從上面代碼中可以看出匯出的中文標題是根據欄位的Description屬性來擷取的。
以上就是簡單的資料匯出方法。如有此文有錯誤的地方,或者大俠們有更好的解決方案,歡迎指正和指導~
註:如有轉載或引用請註明出處 http://www.cnblogs.com/sev7en/p/4021994.html ~
.net中資料集合匯出為Excel(支援泛型及顯示欄位順序,可自訂顯示列名)