/// <summary> /// 获取指定单元格数据 /// </summary> /// <param name="assistCellDataList">指定查询单元格数据列表</param> /// <param name="excelPath">Excel 路径</param> /// <param name="sheetName">表单名称</param> /// <returns></returns> public static Dictionary <string, string> ToDict(List <ExcelAssistCellData> assistCellDataList, string excelPath, string sheetName) { Dictionary <string, string> resultDict = new Dictionary <string, string>(); ExcelHelper.ExecuteIWorkbookRead(excelPath, (IWorkbook workbook) => { ISheet sheet = workbook.GetSheet(sheetName); IRow row = null; ICell cell = null; foreach (ExcelAssistCellData assistCellData in assistCellDataList) { row = sheet.GetRow(assistCellData.RowIndex); if (row != null) { cell = row.GetCell(assistCellData.ColumnIndex); if (cell != null) { resultDict.Add(assistCellData.Key, ExcelHelper.GetCellText(cell, assistCellData.KeyType)); } } } }); return(resultDict); }
/// <summary> /// 返回多 Sheet 表单数据 /// </summary> /// <param name="excelPath">Excel 路径</param> /// <param name="sheetDataList">单元格数据,类型:ReadMultiSheet</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static Dictionary <string, object> ToEntityList(string excelPath, List <object> sheetDataList, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) { Dictionary <string, object> dataDict = new Dictionary <string, object>(); ExcelHelper.ExecuteIWorkbookRead(excelPath, (IWorkbook workbook) => { MethodInfo method = typeof(ExcelHelper).GetMethod("SheetEntityList", BindingFlags.Static | BindingFlags.NonPublic); foreach (object sheetData in sheetDataList) { IReadMultiSheet excelMultiSheet = sheetData as IReadMultiSheet; if (excelMultiSheet != null) { Type sheetType = ReflectionHelper.GetGenericType(sheetData.GetType()); MethodInfo generic = method.MakeGenericMethod(sheetType); object objectData = generic.Invoke(ExcelHelper.Instance, new object[] { workbook, excelMultiSheet.SheetName, excelMultiSheet.PropertyMatchList, excelMultiSheet.HeaderIndex, excelMultiSheet.DataIndex, excelMultiSheet.PrimaryKey, reflectionType }); dataDict.Add(excelMultiSheet.SheetName, objectData); } } }); return(dataDict); }
/// <summary> /// 返回实体数据列表 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="excelPath">Excel 路径</param> /// <param name="sheetName">Sheet 表单名称</param> /// <param name="propertyMatchExpression">属性匹配,例:p=> new { ID = p.UserID }</param> /// <param name="headerIndex">表头起始索引,默认值:0,表示第一行是表头数据,与 dataIndex 相同时,表示 Excel 无表头</param> /// <param name="dataIndex">数据行起始索引,默认值:1,表示数据从第二行开始</param> /// <param name="primaryKey">主键标识,如果未指定,则表示第一列是主键</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static List <T> ToEntityList <T>(string excelPath, string sheetName, Expression <Func <T, object> > propertyMatchExpression, int headerIndex = 0, int dataIndex = 1, string primaryKey = "", ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { List <T> dataList = new List <T>(); ExcelHelper.ExecuteIWorkbookRead(excelPath, (IWorkbook workbook) => { Dictionary <string, object> propertyDict = CommonHelper.GetExpressionDict <T>(propertyMatchExpression); dataList = ExcelHelper.SheetEntityList <T>(workbook, sheetName, propertyDict, headerIndex, dataIndex, primaryKey, reflectionType); }); return(dataList); }