[点晴永久免费OA]【C#】使用NPOI封装能用于绝大部分场景的导出Execl文件的辅助类
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; namespace C.Customization.Framework { /// <summary> /// Npoi辅助类 /// </summary> public class NpoiHepler { /// <summary> /// 实体类集合导出指定字段到EXCLE /// </summary> /// <param name="cellHeard">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param> /// <param name="enList">数据源</param> /// <param name="sheetName">工作表名称</param> /// <param name="filePath">路径.xls</param> /// <returns> /// 文件的下载地址 /// </returns> public static MessageInfo EntitysToExcel(Dictionary<string, string> cellHeard, IList enList, string sheetName, string filePath) { try { // 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的值 sheet.SetColumnWidth(i, 30 * 256); } // 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.Trim() == "1970-01-01 00:00:00") { 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 new MessageInfo() { IsSucceed = true, Message = filePath }; } catch (Exception ex) { return new MessageInfo() { IsSucceed = false, Message = ex.Message }; } } /// <summary> /// 实体类集合导出指定字段到EXCLE /// </summary> /// <param name="cellHeard">单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } };</param> /// <param name="enList">数据源</param> /// <param name="sheetName">工作表名称</param> /// <param name="filePath">路径.xls</param> /// <returns> /// 文件的下载地址 /// </returns> public static MessageInfo DataTableToExcel(Dictionary<string, string> cellHeard, DataTable enList, string sheetName, string filePath) { try { // 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的值 sheet.SetColumnWidth(i, 30 * 256); } // 3.List对象的值赋值到Excel的单元格里 int rowIndex = 1; // 从第二行开始赋值(第一行已设置为单元头) for (int en=0;en<enList.Rows.Count;en++) { IRow rowTmp = sheet.createRow(rowIndex); for (int i = 0; i < keys.Count; i++) // 根据指定的属性名称,获取对象指定属性的值 { string cellValue = ""; // 单元格的值 object properotyValue = enList.Rows[en][keys[i]]; // 属性的值 // 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.Trim() == "1970-01-01 00:00:00") { 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 new MessageInfo() { IsSucceed = true, Message = filePath }; } catch (Exception ex) { return new MessageInfo() { IsSucceed = false, Message = ex.Message }; } } } } /// <summary> /// 导出 /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void Tb1_Export_Click(object sender, EventArgs e) { //导出 List<Mem_MemberInfo> mems = Mem_MemberService.GetInstance().GetListAll(); Dictionary<string, string> cellHead = new Dictionary<string, string>(); cellHead[nameof(Mem_MemberInfo.UserName)] = "用户昵称"; cellHead[nameof(Mem_MemberInfo.Mobile)] = "手机号"; cellHead[nameof(Mem_MemberInfo.Balance)] = "余额"; cellHead[nameof(Mem_MemberInfo.RealName)] = "真实姓名"; cellHead[nameof(Mem_MemberInfo.IdCardNum)] = "身份证号码"; cellHead[nameof(Mem_MemberInfo.createTime)] = "注册时间"; cellHead[nameof(Mem_MemberInfo.Freeze)] = "冻结金额"; cellHead[nameof(Mem_MemberInfo.IdentityName)] = "等级"; string filename = $"用户数据{DateTime.Now:yyyyMMddHHmmss}.xls"; string filepath = Server.MapPath($"{PageParam.DocumentPath}{filename}"); MessageInfo msg = NpoiHepler.EntitysToExcel(cellHead, mems, "用户列表", filepath); if (msg.IsSucceed == false) { Alert.ShowInTop("导出失败" + msg.Message, MessageBoxIcon.Error); return; } FileInfo file = new FileInfo(msg.Message); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); Response.AddHeader("Content-Length", file.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "gb2312"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(filepath); Response.Flush(); Response.End(); } /// <summary> /// 导出 /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void Tb1_Export_Click(object sender, EventArgs e) { //导出 PageDataBaseInfo pagedata = FUHelper.GridPageData(Grid1, ttbSearch.Text); pagedata.PageSize = 0; DataTable dt = Record_WithdrawalService.GetInstance().FindDt(pagedata, "2", RblStatus.selectedValue); Dictionary<string, string> cellHead = new Dictionary<string, string>(); cellHead["SysNo"] = "系统编号"; cellHead["Mobile"] = "提现账户"; cellHead["StateName"] = "状态"; cellHead["createTime"] = "申请时间"; cellHead["Money"] = "提现金额"; cellHead["Balance"] = "账户余额"; cellHead["Freeze"] = "冻结金额"; cellHead["Remark"] = "备注"; string filename = $"提现记录{DateTime.Now:yyyyMMddHHmmss}.xls"; string filepath = Server.MapPath($"{PageParam.DocumentPath}{filename}"); MessageInfo msg = NpoiHepler.DataTableToExcel(cellHead, dt, "提现记录", filepath); if (msg.IsSucceed == false) { Alert.ShowInTop("导出失败" + msg.Message, MessageBoxIcon.Error); return; } FileInfo file = new FileInfo(msg.Message); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); Response.AddHeader("Content-Length", file.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "gb2312"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(filepath); Response.Flush(); Response.End(); } 该文章在 2022/11/25 15:40:49 编辑过 |
关键字查询
相关文章
正在查询... |