C#的Excel導(dǎo)入、導(dǎo)出
本篇主要介紹C#的Excel導(dǎo)入、導(dǎo)出,供大家參考,具體內(nèi)容如下
一. 介紹
1.1 第三方類庫:NPOI
說明:NPOI是POI項(xiàng)目的.NET 版本,可用于Excel、Word的讀寫操作。
優(yōu)點(diǎn):不用裝Office環(huán)境。
下載地址:http://npoi.codeplex.com/releases
1.2 Excel結(jié)構(gòu)介紹
工作簿(Workbook):每個(gè)Excel文件可理解為一個(gè)工作簿。
工作表(Sheet):一個(gè)工作簿(Workbook)可以包含多個(gè)工作表。
行(row):一個(gè)工作表(Sheet)可以包含多個(gè)行。
二. Excel導(dǎo)入
2.1 操作流程
2.2 NPOI操作代碼
說明:把Excel文件轉(zhuǎn)換為List<T>
步驟:
①讀取Excel文件并以此初始化一個(gè)工作簿(Workbook);
②從工作簿上獲取一個(gè)工作表(Sheet);默認(rèn)為工作薄的第一個(gè)工作表;
③遍歷工作表所有的行(row);默認(rèn)從第二行開始遍歷,第一行(序號0)為單元格頭部;
④遍歷行的每一個(gè)單元格(cell),根據(jù)一定的規(guī)律賦值給對象的屬性。
代碼:
/// <summary> /// 從Excel2003取數(shù)據(jù)并記錄到List集合里 /// </summary> /// <param name="cellHeard">單元頭的Key和Value:{ { "UserName", "姓名" }, { "Age", "年齡" } };</param> /// <param name="filePath">保存文件絕對路徑</param> /// <param name="errorMsg">錯(cuò)誤信息</param> /// <returns>轉(zhuǎn)換好的List對象集合</returns> private static List<T> Excel2003ToEntityList<T>(Dictionary<string, string> cellHeard, string filePath, out StringBuilder errorMsg) where T : new() { errorMsg = new StringBuilder(); // 錯(cuò)誤信息,Excel轉(zhuǎn)換到實(shí)體對象時(shí),會(huì)有格式的錯(cuò)誤信息 List<T> enlist = new List<T>(); // 轉(zhuǎn)換后的集合 List<string> keys = cellHeard.Keys.ToList(); // 要賦值的實(shí)體對象屬性名稱 try { using (FileStream fs = File.OpenRead(filePath)) { HSSFWorkbook workbook = new HSSFWorkbook(fs); HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); // 獲取此文件第一個(gè)Sheet頁 for (int i = 1; i <= sheet.LastRowNum; i++) // 從1開始,第0行為單元頭 { // 1.判斷當(dāng)前行是否空行,若空行就不在進(jìn)行讀取下一行操作,結(jié)束Excel讀取操作 if (sheet.GetRow(i) == null) { break; } T en = new T(); string errStr = ""; // 當(dāng)前行轉(zhuǎn)換時(shí),是否有錯(cuò)誤信息,格式為:第1行數(shù)據(jù)轉(zhuǎn)換異常:XXX列; for (int j = 0; j < keys.Count; j++) { // 2.若屬性頭的名稱包含'.',就表示是子類里的屬性,那么就要遍歷子類,eg:UserEn.TrueName if (keys[j].IndexOf(".") >= 0) { // 2.1解析子類屬性 string[] properotyArray = keys[j].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); string subClassName = properotyArray[0]; // '.'前面的為子類的名稱 string subClassProperotyName = properotyArray[1]; // '.'后面的為子類的屬性名稱 System.Reflection.PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 獲取子類的類型 if (subClassInfo != null) { // 2.1.1 獲取子類的實(shí)例 var subClassEn = en.GetType().GetProperty(subClassName).GetValue(en, null); // 2.1.2 根據(jù)屬性名稱獲取子類里的屬性信息 System.Reflection.PropertyInfo properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName); if (properotyInfo != null) { try { // Excel單元格的值轉(zhuǎn)換為對象屬性的值,若類型不對,記錄出錯(cuò)信息 properotyInfo.SetValue(subClassEn, GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j)), null); } catch (Exception e) { if (errStr.Length == 0) { errStr = "第" + i + "行數(shù)據(jù)轉(zhuǎn)換異常:"; } errStr += cellHeard[keys[j]] + "列;"; } } } } else { // 3.給指定的屬性賦值 System.Reflection.PropertyInfo properotyInfo = en.GetType().GetProperty(keys[j]); if (properotyInfo != null) { try { // Excel單元格的值轉(zhuǎn)換為對象屬性的值,若類型不對,記錄出錯(cuò)信息 properotyInfo.SetValue(en, GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j)), null); } catch (Exception e) { if (errStr.Length == 0) { errStr = "第" + i + "行數(shù)據(jù)轉(zhuǎn)換異常:"; } errStr += cellHeard[keys[j]] + "列;"; } } } } // 若有錯(cuò)誤信息,就添加到錯(cuò)誤信息里 if (errStr.Length > 0) { errorMsg.AppendLine(errStr); } enlist.Add(en); } } return enlist; } catch (Exception ex) { throw ex; } }
2.3 C#邏輯操作代碼
說明:對Excel轉(zhuǎn)換后的List<T>進(jìn)行后續(xù)操作;如:檢測有效性、持久化存儲(chǔ)等等
步驟:
①調(diào)用2.2代碼,把Excel文件轉(zhuǎn)換為List<T>。
②對List<T>進(jìn)行有效性檢測:必填項(xiàng)是否為空、是否有重復(fù)記錄等等。
③對List<T>進(jìn)行持久化存儲(chǔ)操作。如:存儲(chǔ)到數(shù)據(jù)庫。
④返回操作結(jié)果。
代碼:
public void ImportExcel(HttpContext context) { StringBuilder errorMsg = new StringBuilder(); // 錯(cuò)誤信息 try { #region 1.獲取Excel文件并轉(zhuǎn)換為一個(gè)List集合 // 1.1存放Excel文件到本地服務(wù)器 HttpPostedFile filePost = context.Request.Files["filed"]; // 獲取上傳的文件 string filePath = ExcelHelper.SaveExcelFile(filePost); // 保存文件并獲取文件路徑 // 單元格抬頭 // key:實(shí)體對象屬性名稱,可通過反射獲取值 // value:屬性對應(yīng)的中文注解 Dictionary<string, string> cellheader = new Dictionary<string, string> { { "Name", "姓名" }, { "Age", "年齡" }, { "GenderName", "性別" }, { "TranscriptsEn.ChineseScores", "語文成績" }, { "TranscriptsEn.MathScores", "數(shù)學(xué)成績" }, }; // 1.2解析文件,存放到一個(gè)List集合里 List<UserEntity> enlist = ExcelHelper.ExcelToEntityList<UserEntity>(cellheader, filePath, out errorMsg); #endregion #region 2.對List集合進(jìn)行有效性校驗(yàn) #region 2.1檢測必填項(xiàng)是否必填 for (int i = 0; i < enlist.Count; i++) { UserEntity en = enlist[i]; string errorMsgStr = "第" + (i + 1) + "行數(shù)據(jù)檢測異常:"; bool isHaveNoInputValue = false; // 是否含有未輸入項(xiàng) if (string.IsNullOrEmpty(en.Name)) { errorMsgStr += "姓名列不能為空;"; isHaveNoInputValue = true; } if (isHaveNoInputValue) // 若必填項(xiàng)有值未填 { en.IsExcelVaildateOK = false; errorMsg.AppendLine(errorMsgStr); } } #endregion #region 2.2檢測Excel中是否有重復(fù)對象 for (int i = 0; i < enlist.Count; i++) { UserEntity enA = enlist[i]; if (enA.IsExcelVaildateOK == false) // 上面驗(yàn)證不通過,不進(jìn)行此步驗(yàn)證 { continue; } for (int j = i + 1; j < enlist.Count; j++) { UserEntity enB = enlist[j]; // 判斷必填列是否全部重復(fù) if (enA.Name == enB.Name) { enA.IsExcelVaildateOK = false; enB.IsExcelVaildateOK = false; errorMsg.AppendLine("第" + (i + 1) + "行與第" + (j + 1) + "行的必填列重復(fù)了"); } } } #endregion // TODO:其他檢測 #endregion // 3.TODO:對List集合持久化存儲(chǔ)操作。如:存儲(chǔ)到數(shù)據(jù)庫 // 4.返回操作結(jié)果 bool isSuccess = false; if (errorMsg.Length == 0) { isSuccess = true; // 若錯(cuò)誤信息成都為空,表示無錯(cuò)誤信息 } var rs = new { success = isSuccess, msg = errorMsg.ToString(), data = enlist }; System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); context.Response.ContentType = "text/plain"; context.Response.Write(js.Serialize(rs)); // 返回Json格式的內(nèi)容 } catch (Exception ex) { throw ex; } }
3. Excel導(dǎo)出
3.1 導(dǎo)出流程
3.2 NPOI操作代碼
說明:把List<T>轉(zhuǎn)換為Excel
步驟:
①創(chuàng)建一個(gè)工作簿(Workbook);
②在工作簿上創(chuàng)建一個(gè)工作表(Sheet);
③在工作表上創(chuàng)建第一行(row),第一行為列頭,依次寫入cellHeard的值(做為列名)。
④循環(huán)遍歷List<T>集合,每循環(huán)一遍創(chuàng)建一個(gè)行(row),然后根據(jù)cellHeard的鍵(屬性名稱)依次從List<T>中的實(shí)體對象取值存放到單元格內(nèi)。
代碼:
/// <summary> /// 實(shí)體類集合導(dǎo)出到Excle2003 /// </summary> /// <param name="cellHeard">單元頭的Key和Value:{ { "UserName", "姓名" }, { "Age", "年齡" } };</param> /// <param name="enList">數(shù)據(jù)源</param> /// <param name="sheetName">工作表名稱</param> /// <returns>文件的下載地址</returns> public static string EntityListToExcel2003(Dictionary<string, string> cellHeard, IList enList, string sheetName) { try { string fileName = sheetName + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名稱 string urlPath = "UpFiles/ExcelFiles/" + fileName; // 文件下載的URL地址,供給前臺(tái)下載 string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath); // 文件路徑 // 1.檢測是否存在文件夾,若不存在就建立個(gè)文件夾 string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } // 2.解析單元格頭部,設(shè)置單元頭的中文名稱 HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿 ISheet sheet = workbook.CreateSheet(sheetName); // 工作表 IRow row = sheet.CreateRow(0); List<string> keys = cellHeard.Keys.ToList(); for (int i = 0; i < keys.Count; i++) { row.CreateCell(i).SetCellValue(cellHeard[keys[i]]); // 列名為Key的值 } // 3.List對象的值賦值到Excel的單元格里 int rowIndex = 1; // 從第二行開始賦值(第一行已設(shè)置為單元頭) foreach (var en in enList) { IRow rowTmp = sheet.CreateRow(rowIndex); for (int i = 0; i < keys.Count; i++) // 根據(jù)指定的屬性名稱,獲取對象指定屬性的值 { string cellValue = ""; // 單元格的值 object properotyValue = null; // 屬性的值 System.Reflection.PropertyInfo properotyInfo = null; // 屬性的信息 // 3.1 若屬性頭的名稱包含'.',就表示是子類里的屬性,那么就要遍歷子類,eg:UserEn.UserName if (keys[i].IndexOf(".") >= 0) { // 3.1.1 解析子類屬性(這里只解析1層子類,多層子類未處理) string[] properotyArray = keys[i].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); string subClassName = properotyArray[0]; // '.'前面的為子類的名稱 string subClassProperotyName = properotyArray[1]; // '.'后面的為子類的屬性名稱 System.Reflection.PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 獲取子類的類型 if (subClassInfo != null) { // 3.1.2 獲取子類的實(shí)例 var subClassEn = en.GetType().GetProperty(subClassName).GetValue(en, null); // 3.1.3 根據(jù)屬性名稱獲取子類里的屬性類型 properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName); if (properotyInfo != null) { properotyValue = properotyInfo.GetValue(subClassEn, null); // 獲取子類屬性的值 } } } else { // 3.2 若不是子類的屬性,直接根據(jù)屬性名稱獲取對象對應(yīng)的屬性 properotyInfo = en.GetType().GetProperty(keys[i]); if (properotyInfo != null) { properotyValue = properotyInfo.GetValue(en, null); } } // 3.3 屬性值經(jīng)過轉(zhuǎn)換賦值給單元格值 if (properotyValue != null) { cellValue = properotyValue.ToString(); // 3.3.1 對時(shí)間初始值賦值為空 if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59") { cellValue = ""; } } // 3.4 填充到Excel的單元格里 rowTmp.CreateCell(i).SetCellValue(cellValue); } rowIndex++; } // 4.生成文件 FileStream file = new FileStream(filePath, FileMode.Create); workbook.Write(file); file.Close(); // 5.返回下載路徑 return urlPath; } catch (Exception ex) { throw ex; } }
3.3 C#邏輯操作代碼
說明:對Excel轉(zhuǎn)換后的List<T>進(jìn)行后續(xù)操作;如:檢測有效性、持久化存儲(chǔ)等等
步驟:
①獲取List<T>集合。
②調(diào)用3.2,將List<T>轉(zhuǎn)換為Excel文件。
③服務(wù)器存儲(chǔ)Excel文件并返回下載鏈接。
代碼:
public void ExportExcel(HttpContext context) { try { // 1.獲取數(shù)據(jù)集合 List<UserEntity> enlist = new List<UserEntity>() { new UserEntity{Name="劉一",Age=22,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=80,MathScores=90}}, new UserEntity{Name="陳二",Age=23,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=81,MathScores=91} }, new UserEntity{Name="張三",Age=24,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=82,MathScores=92} }, new UserEntity{Name="李四",Age=25,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=83,MathScores=93} }, new UserEntity{Name="王五",Age=26,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=84,MathScores=94} }, }; // 2.設(shè)置單元格抬頭 // key:實(shí)體對象屬性名稱,可通過反射獲取值 // value:Excel列的名稱 Dictionary<string, string> cellheader = new Dictionary<string, string> { { "Name", "姓名" }, { "Age", "年齡" }, { "GenderName", "性別" }, { "TranscriptsEn.ChineseScores", "語文成績" }, { "TranscriptsEn.MathScores", "數(shù)學(xué)成績" }, }; // 3.進(jìn)行Excel轉(zhuǎn)換操作,并返回轉(zhuǎn)換的文件下載鏈接 string urlPath = ExcelHelper.EntityListToExcel2003(cellheader, enlist, "學(xué)生成績"); System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); context.Response.ContentType = "text/plain"; context.Response.Write(js.Serialize(urlPath)); // 返回Json格式的內(nèi)容 } catch (Exception ex) { throw ex; } }
3.4 代碼分析
核心代碼主要是cellheader與List<T>之間的映射關(guān)系:
四. 源碼下載
4.1 運(yùn)行圖
源碼下載:http://xiazai.jb51.net/201605/yuanma/C-Excel(jb51.net).rar
以上就是本文的全部內(nèi)容,希望能夠?qū)Υ蠹业膶W(xué)習(xí)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)順序表(線性表)完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)順序表(線性表)的方法,結(jié)合完整實(shí)例形式分析了順序表的原理及C#相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06C# / VB.NET 在PPT中創(chuàng)建、編輯PPT SmartArt圖形的方法詳解
本文介紹通過C#和VB.NET程序代碼來創(chuàng)建和編輯PPT文檔中的SmartArt圖形。文中將分兩個(gè)操作示例來演示創(chuàng)建和編輯結(jié)果,需要的朋友可以參考下2020-10-10C#利用TreeView控件實(shí)現(xiàn)目錄跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了C#潤滑利用TreeView控件實(shí)現(xiàn)目錄跳轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-07-07C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題
這篇文章主要介紹了C# 解決datagridview控件顯示大量數(shù)據(jù)拖拉卡頓問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01c#多線程中Lock()關(guān)鍵字的用法小結(jié)
本篇文章主要是對c#多線程中Lock()關(guān)鍵字的用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01c# HttpWebRequest通過代理服務(wù)器抓取網(wǎng)頁內(nèi)容應(yīng)用介紹
在C#項(xiàng)目開發(fā)過程中可能會(huì)有些特殊的需求比如:用HttpWebRequest通過代理服務(wù)器驗(yàn)證后抓取網(wǎng)頁內(nèi)容,要想實(shí)現(xiàn)此方法并不容易,本文整理了一下,有需求的朋友可以參考下2012-11-11