<body> <h3>一、Excel導入</h3> <h5>1.模板下載:<a href="UpFiles/TemplateFiles/學生成績導入模板.xls">學生成績導入模板</a></h5> <h5>2.選擇Excel文件並上傳</h5> <form enctype="multipart/form-data" id="file-form" > <input type="file" name="filed" /><br /> </form> <input type="button" onclick="importExcel()" value="提交" name="submit" /><br /> <textarea readonly="readonly" rows="10" cols="80" id="msg" ></textarea> <h3>二、Excel導出</h3> <input type="button" onclick="exportExcel()" value="導出Excel" /><br /> <h5>模擬數據:</h5> <pre> 姓名 年齡 性別 語文成績 數學成績 劉一 22 男 80 90 陳二 23 男 81 91 張三 24 男 82 92 李四 25 男 83 93 王五 26 男 84 94 </pre> </body>
<script type="text/javascript"> // 導入Excel function importExcel() { $('#file-form').ajaxSubmit({ type: 'POST', // HTTP請求方式 url: 'ashx/ImportExcel', // 請求的URL地址 dataType: 'json', // 服務器返回數據轉換成的類型 success: function (data, responseStatus) { var msg = ''; if (data.success) { msg = '轉換成功!\r\n'; } else { msg = '轉換失敗!\r\n'; } msg += data.msg + '\r\n'; // 獲取異常信息 for (var i = 0, len = data.data.length; i < len; i++) { // 獲取轉換后的實體對象 msg += '數據:' + JSON.stringify(data.data[i]) + '\r\n'; } $('#msg').val(msg); } }); } // 導出Excel function exportExcel() { $('#file-form').ajaxSubmit({ type: 'POST', // HTTP請求方式 url: 'ashx/ExportExcel', // 請求的URL地址 dataType: 'json', // 服務器返回數據轉換成的類型 success: function (data, responseStatus) { location.href = location.origin + '/' + data; } }); } </script>
public void ProcessRequest(HttpContext context) { StringBuilder errorMsg = new StringBuilder(); // 錯誤信息 try { #region 1.獲取Excel文件並轉換為一個List集合 // 1.1存放Excel文件到本地服務器 HttpPostedFile filePost = context.Request.Files["filed"]; // 獲取上傳的文件 string filePath = ExcelHelper.SaveExcelFile(filePost); // 保存文件並獲取文件路徑 // 單元格抬頭 // key:實體對象屬性名稱,可通過反射獲取值 // value:屬性對應的中文注解 Dictionary<string, string> cellheader = new Dictionary<string, string> { { "Name", "姓名" }, { "Age", "年齡" }, { "GenderName", "性別" }, { "TranscriptsEn.ChineseScores", "語文成績" }, { "TranscriptsEn.MathScores", "數學成績" }, }; // 1.2解析文件,存放到一個List集合里 List<UserEntity> enlist = ExcelHelper.ExcelToEntityList<UserEntity>(cellheader, filePath, out errorMsg); #endregion #region 2.對List集合進行有效性校驗 #region 2.1檢測必填項是否必填 for (int i = 0; i < enlist.Count; i++) { UserEntity en = enlist[i]; string errorMsgStr = "第" + (i + 1) + "行數據檢測異常:"; bool isHaveNoInputValue = false; // 是否含有未輸入項 if (string.IsNullOrEmpty(en.Name)) { errorMsgStr += "姓名列不能為空;"; isHaveNoInputValue = true; } if (isHaveNoInputValue) // 若必填項有值未填 { en.IsExcelVaildateOK = false; errorMsg.AppendLine(errorMsgStr); } } #endregion #region 2.2檢測Excel中是否有重復對象 for (int i = 0; i < enlist.Count; i++) { UserEntity enA = enlist[i]; if (enA.IsExcelVaildateOK == false) // 上面驗證不通過,不進行此步驗證 { continue; } for (int j = i + 1; j < enlist.Count; j++) { UserEntity enB = enlist[j]; // 判斷必填列是否全部重復 if (enA.Name == enB.Name) { enA.IsExcelVaildateOK = false; enB.IsExcelVaildateOK = false; errorMsg.AppendLine("第" + (i + 1) + "行與第" + (j + 1) + "行的必填列重復了"); } } } #endregion // TODO:其他檢測 #endregion // 3.TODO:對List集合進行持久化存儲操作。如:存儲到數據庫 // 4.返回操作結果 bool isSuccess = false; if (errorMsg.Length == 0) { isSuccess = true; // 若錯誤信息成都為空,表示無錯誤信息 } 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格式的內容 } catch (Exception ex) { throw ex; } }
/// <summary> /// 保存Excel文件 /// <para>Excel的導入導出都會在服務器生成一個文件</para> /// <para>路徑:UpFiles/ExcelFiles</para> /// </summary> /// <param name="file">傳入的文件對象</param> /// <returns>如果保存成功則返回文件的位置;如果保存失敗則返回空</returns> public static string SaveExcelFile(HttpPostedFile file) { try { var fileName = file.FileName.Insert(file.FileName.LastIndexOf('.'), "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff")); var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UpFiles/ExcelFiles"), fileName); string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } file.SaveAs(filePath); return filePath; } catch { return string.Empty; } }
/// <summary> /// 從Excel取數據並記錄到List集合里 /// </summary> /// <param name="cellHeard">單元頭的值和名稱:{ { "UserName", "姓名" }, { "Age", "年齡" } };</param> /// <param name="filePath">保存文件絕對路徑</param> /// <param name="errorMsg">錯誤信息</param> /// <returns>轉換后的List對象集合</returns> public static List<T> ExcelToEntityList<T>(Dictionary<string, string> cellHeard, string filePath, out StringBuilder errorMsg) where T : new() { List<T> enlist = new List<T>(); errorMsg = new StringBuilder(); try { if (Regex.IsMatch(filePath, ".xls$")) // 2003 { enlist = Excel2003ToEntityList<T>(cellHeard, filePath, out errorMsg); } else if (Regex.IsMatch(filePath, ".xlsx$")) // 2007 { //return FailureResultMsg("請選擇Excel文件"); // 未設計 } return enlist; } catch (Exception ex) { throw ex; } }
/// <summary> /// 從Excel2003取數據並記錄到List集合里 /// </summary> /// <param name="cellHeard">單元頭的Key和Value:{ { "UserName", "姓名" }, { "Age", "年齡" } };</param> /// <param name="filePath">保存文件絕對路徑</param> /// <param name="errorMsg">錯誤信息</param> /// <returns>轉換好的List對象集合</returns> private static List<T> Excel2003ToEntityList<T>(Dictionary<string, string> cellHeard, string filePath, out StringBuilder errorMsg) where T : new() { errorMsg = new StringBuilder(); // 錯誤信息,Excel轉換到實體對象時,會有格式的錯誤信息 List<T> enlist = new List<T>(); // 轉換后的集合 List<string> keys = cellHeard.Keys.ToList(); // 要賦值的實體對象屬性名稱 try { using (FileStream fs = File.OpenRead(filePath)) { HSSFWorkbook workbook = new HSSFWorkbook(fs); HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); // 獲取此文件第一個Sheet頁 for (int i = 1; i <= sheet.LastRowNum; i++) // 從1開始,第0行為單元頭 { // 1.判斷當前行是否空行,若空行就不在進行讀取下一行操作,結束Excel讀取操作 if (sheet.GetRow(i) == null) { break; } T en = new T(); string errStr = ""; // 當前行轉換時,是否有錯誤信息,格式為:第1行數據轉換異常: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 獲取子類的實例 var subClassEn = en.GetType().GetProperty(subClassName).GetValue(en, null); // 2.1.2 根據屬性名稱獲取子類里的屬性信息 System.Reflection.PropertyInfo properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName); if (properotyInfo != null) { try { // Excel單元格的值轉換為對象屬性的值,若類型不對,記錄出錯信息 properotyInfo.SetValue(subClassEn, GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j)), null); } catch (Exception e) { if (errStr.Length == 0) { errStr = "第" + i + "行數據轉換異常:"; } errStr += cellHeard[keys[j]] + "列;"; } } } } else { // 3.給指定的屬性賦值 System.Reflection.PropertyInfo properotyInfo = en.GetType().GetProperty(keys[j]); if (properotyInfo != null) { try { // Excel單元格的值轉換為對象屬性的值,若類型不對,記錄出錯信息 properotyInfo.SetValue(en, GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j)), null); } catch (Exception e) { if (errStr.Length == 0) { errStr = "第" + i + "行數據轉換異常:"; } errStr += cellHeard[keys[j]] + "列;"; } } } } // 若有錯誤信息,就添加到錯誤信息里 if (errStr.Length > 0) { errorMsg.AppendLine(errStr); } enlist.Add(en); } } return enlist; } catch (Exception ex) { throw ex; } }
導出
public void ProcessRequest(HttpContext context) { try { // 1.獲取數據集合 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.設置單元格抬頭 // key:實體對象屬性名稱,可通過反射獲取值 // value:Excel列的名稱 Dictionary<string, string> cellheader = new Dictionary<string, string> { { "Name", "姓名" }, { "Age", "年齡" }, { "GenderName", "性別" }, { "TranscriptsEn.ChineseScores", "語文成績" }, { "TranscriptsEn.MathScores", "數學成績" }, }; // 3.進行Excel轉換操作,並返回轉換的文件下載鏈接 string urlPath = ExcelHelper.EntityListToExcel2003(cellheader, enlist, "學生成績"); System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer(); context.Response.ContentType = "text/plain"; context.Response.Write(js.Serialize(urlPath)); // 返回Json格式的內容 } catch (Exception ex) { throw ex; } }
/// <summary> /// 實體類集合導出到EXCLE2003 /// </summary> /// <param name="cellHeard">單元頭的Key和Value:{ { "UserName", "姓名" }, { "Age", "年齡" } };</param> /// <param name="enList">數據源</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地址,供給前台下載 string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath); // 文件路徑 // 1.檢測是否存在文件夾,若不存在就建立個文件夾 string directoryName = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } // 2.解析單元格頭部,設置單元頭的中文名稱 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; // 從第二行開始賦值(第一行已設置為單元頭) foreach (var en in enList) { IRow rowTmp = sheet.CreateRow(rowIndex); for (int i = 0; i < keys.Count; i++) // 根據指定的屬性名稱,獲取對象指定屬性的值 { 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 獲取子類的實例 var subClassEn = en.GetType().GetProperty(subClassName).GetValue(en, null); // 3.1.3 根據屬性名稱獲取子類里的屬性類型 properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName); if (properotyInfo != null) { properotyValue = properotyInfo.GetValue(subClassEn, null); // 獲取子類屬性的值 } } } else { // 3.2 若不是子類的屬性,直接根據屬性名稱獲取對象對應的屬性 properotyInfo = en.GetType().GetProperty(keys[i]); if (properotyInfo != null) { properotyValue = properotyInfo.GetValue(en, null); } } // 3.3 屬性值經過轉換賦值給單元格值 if (properotyValue != null) { cellValue = properotyValue.ToString(); // 3.3.1 對時間初始值賦值為空 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; } }
/// <summary> /// 從Excel獲取值傳遞到對象的屬性里 /// </summary> /// <param name="distanceType">目標對象類型</param> /// <param name="sourceCell">對象屬性的值</param> private static Object GetExcelCellToProperty(Type distanceType, ICell sourceCell) { object rs = distanceType.IsValueType ? Activator.CreateInstance(distanceType) : null; // 1.判斷傳遞的單元格是否為空 if (sourceCell == null || string.IsNullOrEmpty(sourceCell.ToString())) { return rs; } // 2.Excel文本和數字單元格轉換,在Excel里文本和數字是不能進行轉換,所以這里預先存值 object sourceValue = null; switch (sourceCell.CellType) { case CellType.Blank: break; case CellType.Boolean: break; case CellType.Error: break; case CellType.Formula: break; case CellType.Numeric: sourceValue = sourceCell.NumericCellValue; break; case CellType.String: sourceValue = sourceCell.StringCellValue; break; case CellType.Unknown: break; default: break; } string valueDataType = distanceType.Name; // 在這里進行特定類型的處理 switch (valueDataType.ToLower()) // 以防出錯,全部小寫 { case "string": rs = sourceValue.ToString(); break; case "int": case "int16": case "int32": rs = (int)Convert.ChangeType(sourceCell.NumericCellValue.ToString(), distanceType); break; case "float": case "single": rs = (float)Convert.ChangeType(sourceCell.NumericCellValue.ToString(), distanceType); break; case "datetime": rs = sourceCell.DateCellValue; break; case "guid": rs = (Guid)Convert.ChangeType(sourceCell.NumericCellValue.ToString(), distanceType); return rs; } return rs; }