├── CSum.FK ├── CSum.Offices │ ├── packages.config │ ├── Excel │ │ ├── Model │ │ │ ├── TemplateMode.cs │ │ │ ├── ColumnEntity.cs │ │ │ └── ExcelTemplate.cs │ │ ├── ExcelConfig.cs │ │ ├── ExcelHelper.T.cs │ │ └── ExcelHelper.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── CSum.Offices.csproj │ └── Helper │ │ └── DataTableHelper.cs ├── CSum.Util │ ├── packages.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ConfigHelper.cs │ ├── Md5Helper.cs │ ├── Extensions │ │ ├── Ext.Json.cs │ │ ├── Ext.Convert.cs │ │ └── Ext.Type.cs │ ├── CSum.Util.csproj │ ├── Net.cs │ ├── CommonHelper.cs │ └── Time.cs ├── CSum.WebApi │ ├── FilterConfig.cs │ ├── BundleConfig.cs │ ├── RouteConfig.cs │ ├── Properties │ │ ├── PublishProfiles │ │ │ └── FolderProfile.pubxml │ │ └── AssemblyInfo.cs │ ├── packages.config │ ├── Api Handler │ │ └── HandlerSecretAttribute.cs │ ├── Web.config │ └── CSum.WebApi.csproj ├── CSum │ ├── packages.config │ ├── Exceptions │ │ ├── NoAuthorizeException.cs │ │ ├── UserFriendlyException.cs │ │ └── ErrorMessage.cs │ ├── Web │ │ ├── ExcuteMode.cs │ │ ├── AjaxResult.cs │ │ └── Pagination.cs │ ├── app.config │ ├── Model │ │ └── ExportModel.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Logging │ │ ├── ILogger.cs │ │ ├── LogHelper.cs │ │ └── LogMessageFormat.cs │ └── CSum.csproj ├── CSum.TestDemo │ ├── Program.cs │ ├── packages.config │ ├── App.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── CSum.TestDemo.csproj │ └── XmlConfig │ │ └── log4net.config └── CSum.FK.sln ├── .gitignore └── Vux └── CsLoadMore ├── props.js └── loadMore.vue /CSum.FK/CSum.Offices/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/FilterConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | 3 | namespace CSum.WebApi 4 | { 5 | public class FilterConfig 6 | { 7 | public static void RegisterGlobalFilters(GlobalFilterCollection filters) 8 | { 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /CSum.FK/CSum/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 3 | ################################################################################ 4 | *.user 5 | *.suo 6 | *.pdb 7 | *.nupkg 8 | *.nuspec 9 | 10 | bin 11 | obj 12 | packages 13 | .vs -------------------------------------------------------------------------------- /CSum.FK/CSum/Exceptions/NoAuthorizeException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CSum.Exceptions 4 | { 5 | /// 6 | /// 没有被授权的异常 7 | /// 8 | public class NoAuthorizeException : Exception 9 | { 10 | public NoAuthorizeException(string message) 11 | : base(message) 12 | { 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /CSum.FK/CSum/Exceptions/UserFriendlyException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CSum.Exceptions 4 | { 5 | /// 6 | /// 用户友好异常 7 | /// 8 | public class UserFriendlyException : Exception 9 | { 10 | public UserFriendlyException(string message) 11 | : base(message) 12 | { 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /CSum.FK/CSum/Web/ExcuteMode.cs: -------------------------------------------------------------------------------- 1 | namespace CSum.Web 2 | { 3 | /// 4 | /// 执行模式 5 | /// 6 | public enum ExcuteMode 7 | { 8 | /// 9 | /// 执行 10 | /// 11 | Enforce, 12 | 13 | /// 14 | /// 忽略 15 | /// 16 | Ignore 17 | } 18 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/BundleConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Optimization; 2 | 3 | namespace CSum.WebApi 4 | { 5 | public class BundleConfig 6 | { 7 | /// 8 | /// 绑定js,css 9 | /// 10 | /// 11 | public static void RegisterBundles(BundleCollection bundles) 12 | { 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.TestDemo/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using CSum.Logging; 7 | 8 | namespace CSum.TestDemo 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | LogHelper.Trace("log222"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /CSum.FK/CSum.TestDemo/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/Excel/Model/TemplateMode.cs: -------------------------------------------------------------------------------- 1 | namespace CSum.Offices.Excel 2 | { 3 | public class TemplateMode 4 | { 5 | /// 6 | /// 行号 7 | /// 8 | public int row { get; set; } 9 | 10 | /// 11 | /// 列号 12 | /// 13 | public int cell { get; set; } 14 | 15 | /// 16 | /// 数据值 17 | /// 18 | public string value { get; set; } 19 | } 20 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/RouteConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Web.Mvc; 2 | using System.Web.Routing; 3 | 4 | namespace CSum.WebApi 5 | { 6 | public class RouteConfig 7 | { 8 | public static void RegisterRoutes(RouteCollection routes) 9 | { 10 | routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 11 | 12 | routes.MapRoute( 13 | "Default", 14 | "{controller}/{action}/{id}", 15 | new {controller = "Home", action = "Index", id = UrlParameter.Optional}, 16 | new[] {"CSum.WebApi.Controllers"} 17 | ); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /CSum.FK/CSum/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/Properties/PublishProfiles/FolderProfile.pubxml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | FileSystem 9 | FileSystem 10 | Release 11 | Any CPU 12 | 13 | True 14 | False 15 | bin\Release\Publish 16 | False 17 | 18 | -------------------------------------------------------------------------------- /CSum.FK/CSum/Model/ExportModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CSum.Model 8 | { 9 | public class ExportModel 10 | { 11 | /// 12 | /// 保存的文件名称 13 | /// 14 | public string FileName { get; set; } 15 | /// 16 | /// 标题 17 | /// 18 | public string Title { get; set; } 19 | /// 20 | /// 表头集合 21 | /// 22 | public List LstCol { get; set; } 23 | /// 24 | /// 数据 25 | /// 26 | public string Data { get; set; } 27 | } 28 | /// 29 | /// 列的list 30 | /// 31 | public class ExportDataColumn 32 | { 33 | public string prop { get; set; } 34 | public string label { get; set; } 35 | public int width { get; set; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // 有关程序集的常规信息通过下列特性集 5 | // 控制。更改这些特性值可修改 6 | // 与程序集关联的信息。 7 | [assembly: AssemblyTitle("HH.Net")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany(" ")] 11 | [assembly: AssemblyProduct("HH.Net")] 12 | [assembly: AssemblyCopyright("版权所有(C) 2017")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // 将 ComVisible 设置为 false 会使此程序集中的类型 17 | // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的某个类型, 18 | // 请针对该类型将 ComVisible 特性设置为 true。 19 | [assembly: ComVisible(false)] 20 | 21 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 22 | [assembly: Guid("f3d291d0-2418-4e42-89fb-776970048066")] 23 | 24 | // 程序集的版本信息由下列四个值组成: 25 | // 26 | // 主版本 27 | // 次版本 28 | // 内部版本号 29 | // 修订号 30 | // 31 | // 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值, 32 | // 方法是按如下所示使用“*”: 33 | [assembly: AssemblyVersion("1.0.0.0")] 34 | [assembly: AssemblyFileVersion("1.0.0.0")] -------------------------------------------------------------------------------- /CSum.FK/CSum.TestDemo/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /CSum.FK/CSum/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // 有关程序集的常规信息通过以下 5 | // 特性集控制。更改这些特性值可修改 6 | // 与程序集关联的信息。 7 | [assembly: AssemblyTitle("CSum")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("CSum")] 12 | [assembly: AssemblyCopyright("Copyright © 2018")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // 将 ComVisible 设置为 false 使此程序集中的类型 17 | // 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 18 | // 则将该类型上的 ComVisible 特性设置为 true。 19 | [assembly: ComVisible(false)] 20 | 21 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 22 | [assembly: Guid("b0f79f0e-1895-4654-b70a-640a2e56919d")] 23 | 24 | // 程序集的版本信息由下面四个值组成: 25 | // 26 | // 主版本 27 | // 次版本 28 | // 生成号 29 | // 修订号 30 | // 31 | // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 32 | // 方法是按如下所示使用“*”: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("2.0.0")] 35 | [assembly: AssemblyFileVersion("2.0.0")] -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // 有关程序集的常规信息通过以下 5 | // 特性集控制。更改这些特性值可修改 6 | // 与程序集关联的信息。 7 | [assembly: AssemblyTitle("CSum.Util")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("CSum.Util")] 12 | [assembly: AssemblyCopyright("Copyright © 2017")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // 将 ComVisible 设置为 false 使此程序集中的类型 17 | // 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 18 | // 则将该类型上的 ComVisible 特性设置为 true。 19 | [assembly: ComVisible(false)] 20 | 21 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 22 | [assembly: Guid("58ab2a12-043d-4a7b-9d68-d6aa0ee3dd6c")] 23 | 24 | // 程序集的版本信息由下面四个值组成: 25 | // 26 | // 主版本 27 | // 次版本 28 | // 生成号 29 | // 修订号 30 | // 31 | // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 32 | // 方法是按如下所示使用“*”: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("2.0.0")] 35 | [assembly: AssemblyFileVersion("2.0.0")] -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // 有关程序集的常规信息通过以下 5 | // 特性集控制。更改这些特性值可修改 6 | // 与程序集关联的信息。 7 | [assembly: AssemblyTitle("CSum.Offices")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("CSum.Offices")] 12 | [assembly: AssemblyCopyright("Copyright © 2017")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // 将 ComVisible 设置为 false 使此程序集中的类型 17 | // 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, 18 | // 则将该类型上的 ComVisible 特性设置为 true。 19 | [assembly: ComVisible(false)] 20 | 21 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 22 | [assembly: Guid("48caa432-829c-46a5-b67b-f6176b3b1d90")] 23 | 24 | // 程序集的版本信息由下面四个值组成: 25 | // 26 | // 主版本 27 | // 次版本 28 | // 生成号 29 | // 修订号 30 | // 31 | // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, 32 | // 方法是按如下所示使用“*”: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | [assembly: AssemblyVersion("2.0.0.0")] 35 | [assembly: AssemblyFileVersion("2.0.0.0")] -------------------------------------------------------------------------------- /CSum.FK/CSum.TestDemo/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("CSum.TestDemo")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CSum.TestDemo")] 13 | [assembly: AssemblyCopyright("Copyright © 2019")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("44b434e3-c995-4e01-ba2c-1dcffb6bf198")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /CSum.FK/CSum/Logging/ILogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CSum.Logging 4 | { 5 | /// 6 | /// 日志记录接口 7 | /// 8 | public interface ILogger 9 | { 10 | #region 级别日志 11 | 12 | /// 13 | /// 调试期间的日志 14 | /// 15 | /// 16 | void Debug(string message); 17 | 18 | /// 19 | /// 将message记录到日志文件 20 | /// 21 | /// 22 | void Info(string message); 23 | 24 | /// 25 | /// 引起警告的日志 26 | /// 27 | /// 28 | void Warn(string message); 29 | 30 | /// 31 | /// 异常发生的日志 32 | /// 33 | /// 34 | void Error(Exception ex); 35 | 36 | /// 37 | /// 引起程序终止的日志 38 | /// 39 | /// 40 | void Fatal(string message); 41 | 42 | #endregion 43 | } 44 | } -------------------------------------------------------------------------------- /CSum.FK/CSum/Web/AjaxResult.cs: -------------------------------------------------------------------------------- 1 | namespace CSum.Web 2 | { 3 | /// 4 | /// 表示Ajax操作结果 5 | /// 6 | public class AjaxResult 7 | { 8 | /// 9 | /// 获取 Ajax操作结果类型 10 | /// 11 | public ResultType type { get; set; } 12 | 13 | /// 14 | /// 获取 Ajax操作结果编码 15 | /// 16 | public int errorcode { get; set; } 17 | 18 | /// 19 | /// 获取 消息内容 20 | /// 21 | public string message { get; set; } 22 | 23 | /// 24 | /// 获取 返回数据 25 | /// 26 | public object resultdata { get; set; } 27 | } 28 | 29 | /// 30 | /// 表示 ajax 操作结果类型的枚举 31 | /// 32 | public enum ResultType 33 | { 34 | /// 35 | /// 消息结果类型 36 | /// 37 | info = 0, 38 | 39 | /// 40 | /// 成功结果类型 41 | /// 42 | success = 1, 43 | 44 | /// 45 | /// 警告结果类型 46 | /// 47 | warning = 2, 48 | 49 | /// 50 | /// 异常结果类型 51 | /// 52 | error = 3 53 | } 54 | } -------------------------------------------------------------------------------- /Vux/CsLoadMore/props.js: -------------------------------------------------------------------------------- 1 | let props = { 2 | // 远程请求路径 3 | url: { 4 | type: String 5 | }, 6 | // 查询参数对象,无论是单条件查询,高级查询,远程请求数据或本地请求数据,都是改变该参数 7 | params: { 8 | type: [Object, Array] 9 | }, 10 | // 自动加载,组件初始化时就加载 11 | autoLoad: { 12 | type: Boolean, 13 | default: true 14 | }, 15 | // 每页数量 16 | pageSize: { 17 | type: Number, 18 | default: 10 19 | }, 20 | // 排序字段 21 | sortName: { 22 | type: String, 23 | default: 'CreateDate' 24 | }, 25 | // 排序顺序 26 | sordName: { 27 | type: String, 28 | default: 'desc', 29 | validator: value => { 30 | const sordTypes = ['desc', 'asc'] 31 | return sordTypes.indexOf(value.toLowerCase()) !== -1 32 | } 33 | }, 34 | 35 | /** 以下字段用来自定义分页或排序字段 */ 36 | // 远程请求,返回的数据字段名称 37 | listField: { 38 | type: String, 39 | default: 'rows' 40 | }, 41 | // 远程请求,返回的总数字段名称 42 | totalField: { 43 | type: String, 44 | default: 'total' 45 | }, 46 | // 远程请求 请求的参数数据 当前页字段名称 47 | pageIndexField: { 48 | type: String, 49 | default: 'page' 50 | }, 51 | // 远程请求 请求的参数数据 每页行数字段名称 52 | pageSizeField: { 53 | type: String, 54 | default: 'rows' 55 | }, 56 | // 远程请求 请求的参数数据 排序字段名称 57 | sortNameField: { 58 | type: String, 59 | default: 'sidx' 60 | }, 61 | // 远程请求 请求的参数数据 排序顺序字段名称 62 | sordField: { 63 | type: String, 64 | default: 'sord' 65 | } 66 | } 67 | export default props 68 | -------------------------------------------------------------------------------- /CSum.FK/CSum/Web/Pagination.cs: -------------------------------------------------------------------------------- 1 | namespace CSum 2 | { 3 | /// 4 | /// 分页参数 5 | /// 6 | public class Pagination 7 | { 8 | public Pagination() 9 | { 10 | sidx = ""; 11 | sord = ""; 12 | } 13 | 14 | /// 15 | /// 每页行数 16 | /// 17 | public int rows { get; set; } 18 | 19 | /// 20 | /// 当前页(当值为-1时查询所有数据) 21 | /// 22 | public int page { get; set; } = -1; 23 | 24 | /// 25 | /// 排序列 26 | /// 27 | public string sidx { get; set; } 28 | 29 | /// 30 | /// 排序类型 31 | /// 32 | public string sord { get; set; } 33 | 34 | /// 35 | /// 总记录数 36 | /// 37 | public int records { get; set; } 38 | 39 | /// 40 | /// 总页数 41 | /// 42 | public int total 43 | { 44 | get 45 | { 46 | if (records > 0) 47 | return records % rows == 0 ? records / rows : records / rows + 1; 48 | return 0; 49 | } 50 | } 51 | 52 | ///// 53 | ///// 查询条件Json 54 | ///// 55 | //public string conditionJson { get; set; } 56 | } 57 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/Excel/Model/ColumnEntity.cs: -------------------------------------------------------------------------------- 1 | using System.Drawing; 2 | 3 | namespace CSum.Offices.Excel 4 | { 5 | /// 6 | /// 描 述:Excel导入导出列设置 7 | /// 8 | public class ColumnEntity 9 | { 10 | /// 11 | /// 列名 12 | /// 13 | public string Column { get; set; } 14 | 15 | /// 16 | /// Excel列名 17 | /// 18 | public string ExcelColumn { get; set; } 19 | 20 | /// 21 | /// 宽度 22 | /// 23 | public int Width { get; set; } 24 | 25 | /// 26 | /// 前景色 27 | /// 28 | public Color ForeColor { get; set; } 29 | 30 | /// 31 | /// 背景色 32 | /// 33 | public Color Background { get; set; } 34 | 35 | /// 36 | /// 字体 37 | /// 38 | public string Font { get; set; } 39 | 40 | /// 41 | /// 字号 42 | /// 43 | public short Point { get; set; } 44 | 45 | /// 46 | /// 对齐方式 47 | /// left 左 48 | /// center 中间 49 | /// right 右 50 | /// fill 填充 51 | /// justify 两端对齐 52 | /// centerselection 跨行居中 53 | /// distributed 54 | /// 55 | public string Alignment { get; set; } 56 | } 57 | } -------------------------------------------------------------------------------- /CSum.FK/CSum/Logging/LogHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Common.Logging; 3 | 4 | namespace CSum.Logging 5 | { 6 | /// 7 | /// 日志记录帮助类 8 | /// 9 | public static class LogHelper 10 | { 11 | #region 级别日志 12 | 13 | /// 14 | /// 调试期间的日志 15 | /// 16 | /// 17 | public static void Debug(string message) 18 | { 19 | LogManager.GetLogger("DebugLog").Debug(message); 20 | } 21 | 22 | /// 23 | /// 跟踪日志 24 | /// 25 | /// 26 | public static void Trace(string message) 27 | { 28 | LogManager.GetLogger("TraceLog").Info(message); 29 | } 30 | 31 | /// 32 | /// 将message记录到日志文件 33 | /// 34 | /// 35 | public static void Info(string message) 36 | { 37 | LogManager.GetLogger("InfoLog").Info(message); 38 | } 39 | 40 | /// 41 | /// 引起警告的日志 42 | /// 43 | /// 44 | public static void Warn(string message) 45 | { 46 | LogManager.GetLogger("WarnLog").Warn(LogMessageFormat.WarnFormat(message)); 47 | } 48 | 49 | /// 50 | /// 异常发生的日志 51 | /// 52 | /// 53 | public static void Error(Exception ex) 54 | { 55 | LogManager.GetLogger("ErrorLog").Error(LogMessageFormat.ErrorFormat(ex)); 56 | } 57 | 58 | /// 59 | /// 引起程序终止的日志 60 | /// 61 | /// 62 | public static void Fatal(string message) 63 | { 64 | LogManager.GetLogger("FatalLog").Fatal(message); 65 | } 66 | 67 | #endregion 68 | } 69 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/ConfigHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Configuration; 3 | using System.Web; 4 | using System.Xml; 5 | 6 | namespace CSum.Util 7 | { 8 | /// 9 | /// WebConfig AppSetting文件操作 10 | /// 11 | public class ConfigHelper 12 | { 13 | /// 14 | /// 根据Key取Value值 15 | /// 16 | /// 17 | public static string GetValue(string key) 18 | { 19 | var r = ConfigurationManager.AppSettings[key]; 20 | if (r == null) 21 | return ""; 22 | return r.Trim(); 23 | } 24 | 25 | /// 26 | /// 根据Key修改Value 27 | /// 28 | /// 要修改的Key 29 | /// 要修改为的值 30 | public static void SetValue(string key, string value) 31 | { 32 | var xDoc = new XmlDocument(); 33 | xDoc.Load(HttpContext.Current.Server.MapPath("~/XmlConfig/system.config")); 34 | XmlNode xNode; 35 | XmlElement xElem1; 36 | XmlElement xElem2; 37 | xNode = xDoc.SelectSingleNode("//appSettings"); 38 | 39 | xElem1 = (XmlElement) xNode.SelectSingleNode("//add[@key='" + key + "']"); 40 | if (xElem1 != null) 41 | { 42 | xElem1.SetAttribute("value", value); 43 | } 44 | else 45 | { 46 | xElem2 = xDoc.CreateElement("add"); 47 | xElem2.SetAttribute("key", key); 48 | xElem2.SetAttribute("value", value); 49 | xNode.AppendChild(xElem2); 50 | } 51 | 52 | xDoc.Save(HttpContext.Current.Server.MapPath("~/XmlConfig/system.config")); 53 | } 54 | } 55 | 56 | [Serializable] 57 | internal class NameValueModel 58 | { 59 | public string Id { get; set; } 60 | public string Value { get; set; } 61 | } 62 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/Excel/Model/ExcelTemplate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Web; 4 | using NPOI.SS.UserModel; 5 | using NPOI.XSSF.UserModel; 6 | 7 | namespace CSum.Offices.Excel 8 | { 9 | public class ExcelTemplate 10 | { 11 | private readonly string newFileName; 12 | private readonly string templatePath; 13 | private readonly string templdateName; 14 | 15 | public ExcelTemplate(string templdateName, string newFileName) 16 | { 17 | SheetName = "sheet1"; 18 | templatePath = HttpContext.Current.Server.MapPath("/") + "/Resource/ExcelTemplate/"; 19 | this.templdateName = string.Format("{0}{1}", templatePath, templdateName); 20 | this.newFileName = newFileName; 21 | } 22 | 23 | public string SheetName { get; set; } 24 | 25 | public void ExportDataToExcel(Action actionMethod) 26 | { 27 | using (var ms = SetDataToExcel(actionMethod)) 28 | { 29 | var data = ms.ToArray(); 30 | 31 | #region response to the client 32 | 33 | var response = HttpContext.Current.Response; 34 | response.Clear(); 35 | response.Charset = "UTF-8"; 36 | response.ContentType = "application/vnd-excel"; //"application/vnd.ms-excel"; 37 | HttpContext.Current.Response.AddHeader("Content-Disposition", 38 | string.Format("attachment; filename=" + newFileName)); 39 | HttpContext.Current.Response.BinaryWrite(data); 40 | 41 | #endregion 42 | } 43 | } 44 | 45 | private MemoryStream SetDataToExcel(Action actionMethod) 46 | { 47 | //Load template file 48 | var file = new FileStream(templdateName, FileMode.Open, FileAccess.Read); 49 | var workbook = new XSSFWorkbook(file); 50 | var sheet = workbook.GetSheet(SheetName); 51 | 52 | if (actionMethod != null) actionMethod(sheet); 53 | 54 | sheet.ForceFormulaRecalculation = true; 55 | using (var ms = new MemoryStream()) 56 | { 57 | workbook.Write(ms); 58 | ms.Flush(); 59 | //ms.Position = 0; 60 | return ms; 61 | } 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/Md5Helper.cs: -------------------------------------------------------------------------------- 1 | using System.Security.Cryptography; 2 | using System.Text; 3 | 4 | namespace CSum.Util 5 | { 6 | /// 7 | /// MD5加密帮助类 8 | /// 9 | public class Md5Helper 10 | { 11 | /// 12 | /// MD5加密 默认32位大写 13 | /// 14 | /// 加密字符 15 | /// 16 | public static string MD5(string str) 17 | { 18 | return MD5(str, 32); 19 | } 20 | 21 | /// 22 | /// MD5加密 默认大写 23 | /// 24 | /// 加密字符 25 | /// 位数 16或32 26 | /// 27 | public static string MD5(string str, int code) 28 | { 29 | return MD5(str, code, true); 30 | } 31 | 32 | /// 33 | /// MD5加密 34 | /// 35 | /// 加密字符 36 | /// 位数 16或32 37 | /// 大小写 true 大写 false 小写 38 | /// 39 | public static string MD5(string str, int code, bool isUp) 40 | { 41 | if (isUp) 42 | return MD5Up(str, code); 43 | return MD5Lower(str, code); 44 | } 45 | 46 | #region "大写 MD5加密" 47 | 48 | /// 49 | /// 大写 MD5加密 50 | /// 51 | /// 加密字符 52 | /// 位数,默认32位 53 | /// 54 | public static string MD5Up(string str, int code = 32) 55 | { 56 | if (string.IsNullOrEmpty(str)) 57 | return str; 58 | var sb = new StringBuilder(code); 59 | var md5 = System.Security.Cryptography.MD5.Create(); 60 | var output = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); 61 | for (var i = 0; i < output.Length; i++) 62 | sb.Append(output[i].ToString("X").PadLeft(2, '0')); 63 | return sb.ToString(); 64 | } 65 | 66 | #endregion 67 | 68 | #region "小写 MD5加密" 69 | 70 | /// 71 | /// 小写 MD5加密 72 | /// 73 | /// 加密字符 74 | /// 位数,默认32位 75 | /// 76 | public static string MD5Lower(string str, int code = 32) 77 | { 78 | //32位小写MD5加密 79 | MD5 md5 = new MD5CryptoServiceProvider(); 80 | var data = Encoding.UTF8.GetBytes(str); 81 | var md5data = md5.ComputeHash(data); 82 | var sb = new StringBuilder(code); 83 | for (var i = 0; i < md5data.Length; i++) sb.Append(md5data[i].ToString("x").PadLeft(2, '0')); 84 | return sb.ToString(); 85 | } 86 | 87 | #endregion 88 | } 89 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/Excel/ExcelConfig.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Drawing; 3 | 4 | namespace CSum.Offices.Excel 5 | { 6 | /// 7 | /// 描 述:Excel导入导出设置 8 | /// 9 | public class ExcelConfig 10 | { 11 | private string _headfont; 12 | private short _headpoint; 13 | private string _titlefont; 14 | private short _titlepoint; 15 | 16 | /// 17 | /// 文件名 18 | /// 19 | public string FileName { get; set; } 20 | 21 | /// 22 | /// 标题 23 | /// 24 | public string Title { get; set; } 25 | 26 | /// 27 | /// 前景色 28 | /// 29 | public Color ForeColor { get; set; } 30 | 31 | /// 32 | /// 背景色 33 | /// 34 | public Color Background { get; set; } 35 | 36 | /// 37 | /// 标题字号 38 | /// 39 | public short TitlePoint 40 | { 41 | get 42 | { 43 | if (_titlepoint == 0) 44 | return 20; 45 | return _titlepoint; 46 | } 47 | set => _titlepoint = value; 48 | } 49 | 50 | /// 51 | /// 列头字号 52 | /// 53 | public short HeadPoint 54 | { 55 | get 56 | { 57 | if (_headpoint == 0) 58 | return 10; 59 | return _headpoint; 60 | } 61 | set => _headpoint = value; 62 | } 63 | 64 | /// 65 | /// 标题高度 66 | /// 67 | public short TitleHeight { get; set; } 68 | 69 | /// 70 | /// 列标题高度 71 | /// 72 | public short HeadHeight { get; set; } 73 | 74 | /// 75 | /// 标题字体 76 | /// 77 | public string TitleFont 78 | { 79 | get 80 | { 81 | if (_titlefont == null) 82 | return "微软雅黑"; 83 | return _titlefont; 84 | } 85 | set => _titlefont = value; 86 | } 87 | 88 | /// 89 | /// 列头字体 90 | /// 91 | public string HeadFont 92 | { 93 | get 94 | { 95 | if (_headfont == null) 96 | return "微软雅黑"; 97 | return _headfont; 98 | } 99 | set => _headfont = value; 100 | } 101 | 102 | /// 103 | /// 是否按内容长度来适应表格宽度 104 | /// 105 | public bool IsAllSizeColumn { get; set; } 106 | 107 | /// 108 | /// 列设置 109 | /// 110 | public List ColumnEntity { get; set; } 111 | } 112 | } -------------------------------------------------------------------------------- /Vux/CsLoadMore/loadMore.vue: -------------------------------------------------------------------------------- 1 | 17 | 108 | 113 | -------------------------------------------------------------------------------- /CSum.FK/CSum.FK.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27703.2026 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSum", "CSum\CSum.csproj", "{D4238537-E643-4A5E-98AA-1CAF53653AAB}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSum.Offices", "CSum.Offices\CSum.Offices.csproj", "{BDCC2B41-41F9-4FD6-903F-DB7E02A93929}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSum.WebApi", "CSum.WebApi\CSum.WebApi.csproj", "{D521EFDC-F0A2-4E23-98AB-5C0C51FE0A92}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSum.Util", "CSum.Util\CSum.Util.csproj", "{F5EFC00E-7E3F-400F-9777-CCA6C6CA4DBC}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSum.TestDemo", "CSum.TestDemo\CSum.TestDemo.csproj", "{44B434E3-C995-4E01-BA2C-1DCFFB6BF198}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {D4238537-E643-4A5E-98AA-1CAF53653AAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {D4238537-E643-4A5E-98AA-1CAF53653AAB}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {D4238537-E643-4A5E-98AA-1CAF53653AAB}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {D4238537-E643-4A5E-98AA-1CAF53653AAB}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {BDCC2B41-41F9-4FD6-903F-DB7E02A93929}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {BDCC2B41-41F9-4FD6-903F-DB7E02A93929}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {BDCC2B41-41F9-4FD6-903F-DB7E02A93929}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {BDCC2B41-41F9-4FD6-903F-DB7E02A93929}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {D521EFDC-F0A2-4E23-98AB-5C0C51FE0A92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {D521EFDC-F0A2-4E23-98AB-5C0C51FE0A92}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {D521EFDC-F0A2-4E23-98AB-5C0C51FE0A92}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {D521EFDC-F0A2-4E23-98AB-5C0C51FE0A92}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {F5EFC00E-7E3F-400F-9777-CCA6C6CA4DBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {F5EFC00E-7E3F-400F-9777-CCA6C6CA4DBC}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {F5EFC00E-7E3F-400F-9777-CCA6C6CA4DBC}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {F5EFC00E-7E3F-400F-9777-CCA6C6CA4DBC}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {44B434E3-C995-4E01-BA2C-1DCFFB6BF198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {44B434E3-C995-4E01-BA2C-1DCFFB6BF198}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {44B434E3-C995-4E01-BA2C-1DCFFB6BF198}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {44B434E3-C995-4E01-BA2C-1DCFFB6BF198}.Release|Any CPU.Build.0 = Release|Any CPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | GlobalSection(ExtensibilityGlobals) = postSolution 47 | SolutionGuid = {08E8C043-5CC3-40A1-B914-90F0062790E4} 48 | EndGlobalSection 49 | EndGlobal 50 | -------------------------------------------------------------------------------- /CSum.FK/CSum/Exceptions/ErrorMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Web; 3 | using CSum.Util; 4 | 5 | namespace CSum.Exceptions 6 | { 7 | /// 8 | /// 异常错误信息 9 | /// 10 | [Serializable] 11 | public class ExceptionMessage 12 | { 13 | public ExceptionMessage() 14 | { 15 | } 16 | 17 | /// 18 | /// 构造函数 19 | /// 默认显示异常页面 20 | /// 21 | /// 异常对象 22 | public ExceptionMessage(Exception ex) 23 | : this(ex, true) 24 | { 25 | } 26 | 27 | /// 28 | /// 构造函数 29 | /// 30 | /// 异常对象 31 | /// 是否显示异常页面 32 | public ExceptionMessage(Exception ex, bool isShowException) 33 | { 34 | MsgType = ex.GetType().Name; 35 | Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message; 36 | StackTrace = ex.StackTrace.Length > 300 ? ex.StackTrace.Substring(0, 300) : ex.StackTrace; 37 | Source = ex.Source; 38 | Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 39 | Assembly = ex.TargetSite.Module.Assembly.FullName; 40 | Method = ex.TargetSite.Name; 41 | 42 | ShowException = isShowException; 43 | var request = HttpContext.Current.Request; 44 | IP = Net.Ip; 45 | UserAgent = request.UserAgent; 46 | Path = request.Path; 47 | HttpMethod = request.HttpMethod; 48 | } 49 | 50 | /// 51 | /// 消息类型 52 | /// 53 | public string MsgType { get; set; } 54 | 55 | /// 56 | /// 消息内容 57 | /// 58 | public string Message { get; set; } 59 | 60 | /// 61 | /// 请求路径 62 | /// 63 | public string Path { get; set; } 64 | 65 | /// 66 | /// 程序集名称 67 | /// 68 | public string Assembly { get; set; } 69 | 70 | /// 71 | /// 异常参数 72 | /// 73 | public string ActionArguments { get; set; } 74 | 75 | /// 76 | /// 请求类型 77 | /// 78 | public string HttpMethod { get; set; } 79 | 80 | /// 81 | /// 异常堆栈 82 | /// 83 | public string StackTrace { get; set; } 84 | 85 | /// 86 | /// 异常源 87 | /// 88 | public string Source { get; set; } 89 | 90 | /// 91 | /// 服务器IP 端口 92 | /// 93 | public string IP { get; set; } 94 | 95 | /// 96 | /// 客户端浏览器标识 97 | /// 98 | public string UserAgent { get; set; } 99 | 100 | 101 | /// 102 | /// 是否显示异常界面 103 | /// 104 | public bool ShowException { get; set; } 105 | 106 | /// 107 | /// 异常发生时间 108 | /// 109 | public string Time { get; set; } 110 | 111 | /// 112 | /// 异常发生方法 113 | /// 114 | public string Method { get; set; } 115 | } 116 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/Extensions/Ext.Json.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data; 4 | using System.Linq; 5 | using System.Reflection; 6 | using Newtonsoft.Json; 7 | using Newtonsoft.Json.Converters; 8 | using Newtonsoft.Json.Linq; 9 | using Newtonsoft.Json.Serialization; 10 | 11 | namespace CSum.Util 12 | { 13 | /// 14 | /// Json格式字符串辅助扩展方法类 15 | /// 16 | public static partial class Extensions 17 | { 18 | public static object ToJson(this string Json) 19 | { 20 | return Json == null ? null : JsonConvert.DeserializeObject(Json); 21 | } 22 | 23 | public static string ToJson(this object obj) 24 | { 25 | var jsonSerializerSettings = new JsonSerializerSettings 26 | { 27 | DateFormatHandling = DateFormatHandling.MicrosoftDateFormat, 28 | ContractResolver = new NullToEmptyStringResolver() 29 | }; 30 | return JsonConvert.SerializeObject(obj, jsonSerializerSettings); 31 | } 32 | 33 | public static string ToJson(this object obj, string datetimeformats) 34 | { 35 | var timeConverter = new IsoDateTimeConverter {DateTimeFormat = datetimeformats}; 36 | return JsonConvert.SerializeObject(obj, timeConverter); 37 | } 38 | 39 | public static T ToObject(this string Json) 40 | { 41 | return Json == null ? default(T) : JsonConvert.DeserializeObject(Json); 42 | } 43 | 44 | public static List ToList(this string Json) 45 | { 46 | return Json == null ? null : JsonConvert.DeserializeObject>(Json); 47 | } 48 | 49 | public static DataTable ToTable(this string Json) 50 | { 51 | return Json == null ? null : JsonConvert.DeserializeObject(Json); 52 | } 53 | 54 | public static JObject ToJObject(this string Json) 55 | { 56 | return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace(" ", "")); 57 | } 58 | 59 | #region 私有方法 60 | 61 | private class NullToEmptyStringResolver : DefaultContractResolver 62 | { 63 | protected override IList CreateProperties(Type type, MemberSerialization memberSerialization) 64 | { 65 | return type.GetProperties() 66 | .Select(p => 67 | { 68 | var jp = base.CreateProperty(p, memberSerialization); 69 | jp.ValueProvider = new NullToEmptyStringValueProvider(p); 70 | return jp; 71 | }).ToList(); 72 | } 73 | } 74 | 75 | private class NullToEmptyStringValueProvider : IValueProvider 76 | { 77 | private readonly PropertyInfo _MemberInfo; 78 | 79 | public NullToEmptyStringValueProvider(PropertyInfo memberInfo) 80 | { 81 | _MemberInfo = memberInfo; 82 | } 83 | 84 | public object GetValue(object target) 85 | { 86 | var result = _MemberInfo.GetValue(target); 87 | if (_MemberInfo.PropertyType == typeof(string) && result == null) result = ""; 88 | return result; 89 | } 90 | 91 | public void SetValue(object target, object value) 92 | { 93 | _MemberInfo.SetValue(target, value); 94 | } 95 | } 96 | 97 | #endregion 98 | } 99 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/CSum.Util.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F5EFC00E-7E3F-400F-9777-CCA6C6CA4DBC} 8 | Library 9 | Properties 10 | CSum.Util 11 | CSum.Util 12 | v4.5 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | false 25 | bin\Debug\CSum.Util.XML 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | false 35 | 36 | 37 | 38 | 39 | ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 77 | -------------------------------------------------------------------------------- /CSum.FK/CSum/Logging/LogMessageFormat.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using CSum.Util; 4 | 5 | namespace CSum.Logging 6 | { 7 | /// 8 | /// 日志格式器 9 | /// 10 | public static class LogMessageFormat 11 | { 12 | /// 13 | /// 生成警告 14 | /// 15 | /// 警告内容 16 | /// 17 | public static string WarnFormat(string content) 18 | { 19 | // var user = SessionManager.Instance.Current(); 20 | dynamic user = null; 21 | var strInfo = new StringBuilder(); 22 | strInfo.Append("1. 警告: >> 操作时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 操作人: " + 23 | (user == null ? "" : user.UserId) + " \r\n"); 24 | strInfo.Append("2. 主机: " + Net.Host + " Ip : " + Net.Ip + " 浏览器: " + Net.Browser + " \r\n"); 25 | strInfo.Append("3. 内容: " + content + "\r\n"); 26 | strInfo.Append( 27 | "-----------------------------------------------------------------------------------------------------------------------------\r\n"); 28 | return strInfo.ToString(); 29 | } 30 | 31 | /// 32 | /// 生成错误信息,保存至文件的文本 33 | /// 34 | /// 异常对象 35 | /// 36 | public static string ErrorFormat(Exception ex) 37 | { 38 | //var user = SessionManager.Instance.Current(); 39 | dynamic user = null; 40 | var strInfo = new StringBuilder(); 41 | strInfo.Append("1. 错误: >> 操作时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 操作人: " + 42 | (user == null ? "" : user.UserId) + " \r\n"); 43 | strInfo.Append("2. 程序集: " + ex.TargetSite.Module.Assembly.FullName + " \r\n"); 44 | strInfo.Append("3. 异常方法: " + ex.TargetSite.Name + " \r\n"); 45 | strInfo.Append("4. 主机: " + Net.Host + " Ip : " + Net.Ip + " 浏览器: " + Net.Browser + " \r\n"); 46 | strInfo.Append("5. 异常: " + (ex.InnerException != null ? ex.InnerException.Message : ex.Message) + "\r\n"); 47 | strInfo.Append("6. 调用堆栈: " + ex.StackTrace + "\r\n"); 48 | strInfo.Append( 49 | "-----------------------------------------------------------------------------------------------------------------------------\r\n"); 50 | return strInfo.ToString(); 51 | } 52 | 53 | /// 54 | /// 生成异常信息,保存至数据库的文本 55 | /// 56 | /// 异常对象 57 | /// 58 | public static string ExceptionFormat(Exception ex) 59 | { 60 | //var user = SessionManager.Instance.Current(); 61 | dynamic user = null; 62 | var strInfo = new StringBuilder(); 63 | strInfo.Append("1. 错误: >> 操作时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 操作人: " + 64 | (user == null ? "" : user.UserId) + " \r\n"); 65 | strInfo.Append("2. 程序集: " + ex.TargetSite.Module.Assembly.FullName + " \r\n"); 66 | strInfo.Append("3. 异常方法: " + ex.TargetSite.Name + " \r\n"); 67 | strInfo.Append("4. 主机: " + Net.Host + " Ip : " + Net.Ip + " 浏览器: " + Net.Browser + " \r\n"); 68 | strInfo.Append("5. 异常: " + (ex.InnerException != null ? ex.InnerException.Message : ex.Message) + "\r\n"); 69 | strInfo.Append( 70 | "-----------------------------------------------------------------------------------------------------------------------------\r\n"); 71 | return strInfo.ToString(); 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.TestDemo/CSum.TestDemo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {44B434E3-C995-4E01-BA2C-1DCFFB6BF198} 8 | Exe 9 | CSum.TestDemo 10 | CSum.TestDemo 11 | v4.5 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | ..\packages\Common.Logging.3.3.1\lib\net40\Common.Logging.dll 37 | 38 | 39 | ..\packages\Common.Logging.Core.3.3.1\lib\net40\Common.Logging.Core.dll 40 | 41 | 42 | ..\packages\Common.Logging.Log4Net1211.3.3.1\lib\net40\Common.Logging.Log4Net1211.dll 43 | 44 | 45 | ..\packages\log4net.2.0.0\lib\net40-full\log4net.dll 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | Always 65 | 66 | 67 | 68 | 69 | {d4238537-e643-4a5e-98aa-1caf53653aab} 70 | CSum 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/Net.cs: -------------------------------------------------------------------------------- 1 | using System.Net; 2 | using System.Net.Sockets; 3 | using System.Web; 4 | 5 | namespace CSum.Util 6 | { 7 | /// 8 | /// 网络操作 9 | /// 10 | public class Net 11 | { 12 | #region Browser(获取浏览器信息) 13 | 14 | /// 15 | /// 获取浏览器信息 16 | /// 17 | public static string Browser 18 | { 19 | get 20 | { 21 | if (HttpContext.Current == null) 22 | return string.Empty; 23 | var browser = HttpContext.Current.Request.Browser; 24 | return string.Format("{0} {1}", browser.Browser, browser.Version); 25 | } 26 | } 27 | 28 | #endregion 29 | 30 | #region Ip(获取Ip) 31 | 32 | /// 33 | /// 获取Ip 34 | /// 35 | public static string Ip 36 | { 37 | get 38 | { 39 | var result = string.Empty; 40 | if (HttpContext.Current != null) 41 | result = GetWebClientIp(); 42 | if (result.IsEmpty()) 43 | result = GetLanIp(); 44 | return result; 45 | } 46 | } 47 | 48 | /// 49 | /// 获取Web客户端的Ip 50 | /// 51 | private static string GetWebClientIp() 52 | { 53 | var ip = GetWebRemoteIp(); 54 | foreach (var hostAddress in Dns.GetHostAddresses(ip)) 55 | if (hostAddress.AddressFamily == AddressFamily.InterNetwork) 56 | return hostAddress.ToString(); 57 | return string.Empty; 58 | } 59 | 60 | /// 61 | /// 获取Web远程Ip 62 | /// 63 | private static string GetWebRemoteIp() 64 | { 65 | return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? 66 | HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 67 | } 68 | 69 | /// 70 | /// 获取局域网IP 71 | /// 72 | private static string GetLanIp() 73 | { 74 | foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName())) 75 | if (hostAddress.AddressFamily == AddressFamily.InterNetwork) 76 | return hostAddress.ToString(); 77 | return string.Empty; 78 | } 79 | 80 | #endregion 81 | 82 | #region Host(获取主机名) 83 | 84 | /// 85 | /// 获取主机名 86 | /// 87 | public static string Host => HttpContext.Current == null ? Dns.GetHostName() : GetWebClientHostName(); 88 | 89 | /// 90 | /// 获取Web客户端主机名 91 | /// 92 | private static string GetWebClientHostName() 93 | { 94 | if (!HttpContext.Current.Request.IsLocal) 95 | return string.Empty; 96 | var ip = GetWebRemoteIp(); 97 | var result = Dns.GetHostEntry(IPAddress.Parse(ip)).HostName; 98 | if (result == "localhost.localdomain") 99 | result = Dns.GetHostName(); 100 | return result; 101 | } 102 | 103 | /// 104 | /// 获取当前部署站点的协议名 + 域名 + 端口号 105 | /// 106 | public static string Url 107 | { 108 | get 109 | { 110 | var context = HttpContext.Current; 111 | if (context == null) return ""; 112 | 113 | var t = context.Request.Url.AbsoluteUri.IndexOf(context.Request.Url.Authority); 114 | return context.Request.Url.AbsoluteUri.Substring(0, t) + context.Request.Url.Authority; 115 | } 116 | } 117 | 118 | #endregion 119 | } 120 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.TestDemo/XmlConfig/log4net.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/CSum.Offices.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {BDCC2B41-41F9-4FD6-903F-DB7E02A93929} 8 | Library 9 | Properties 10 | CSum.Offices 11 | CSum.Offices 12 | v4.0 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\NPOI.2.1.3.1\lib\net40\ICSharpCode.SharpZipLib.dll 35 | 36 | 37 | ..\packages\NPOI.2.1.3.1\lib\net40\NPOI.dll 38 | 39 | 40 | ..\packages\NPOI.2.1.3.1\lib\net40\NPOI.OOXML.dll 41 | 42 | 43 | ..\packages\NPOI.2.1.3.1\lib\net40\NPOI.OpenXml4Net.dll 44 | 45 | 46 | ..\packages\NPOI.2.1.3.1\lib\net40\NPOI.OpenXmlFormats.dll 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 79 | -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/CommonHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Text; 5 | 6 | namespace CSum.Util 7 | { 8 | /// 9 | /// 常用公共类 10 | /// 11 | public class CommonHelper 12 | { 13 | #region 删除数组中的重复项 14 | 15 | /// 16 | /// 删除数组中的重复项 17 | /// 18 | /// 19 | /// 20 | public static string[] RemoveDup(string[] values) 21 | { 22 | var list = new List(); 23 | for (var i = 0; i < values.Length; i++) //遍历数组成员 24 | { 25 | if (!list.Contains(values[i])) list.Add(values[i]); 26 | ; 27 | } 28 | 29 | return list.ToArray(); 30 | } 31 | 32 | #endregion 33 | 34 | #region 自动生成日期编号 35 | 36 | /// 37 | /// 自动生成编号 201008251145409865 38 | /// 39 | /// 40 | public static string CreateNo() 41 | { 42 | var random = new Random(); 43 | var strRandom = random.Next(1000, 10000).ToString(); //生成编号 44 | var code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom; //形如 45 | return code; 46 | } 47 | 48 | #endregion 49 | 50 | #region 生成0-9随机数 51 | 52 | /// 53 | /// 生成0-9随机数 54 | /// 55 | /// 生成长度 56 | /// 57 | public static string RndNum(int codeNum) 58 | { 59 | var sb = new StringBuilder(codeNum); 60 | var rand = new Random(); 61 | for (var i = 1; i < codeNum + 1; i++) 62 | { 63 | var t = rand.Next(9); 64 | sb.AppendFormat("{0}", t); 65 | } 66 | 67 | return sb.ToString(); 68 | } 69 | 70 | #endregion 71 | 72 | #region 生成指定范围内的不重复随机数 73 | 74 | /// 75 | /// 生成指定范围内的不重复随机数 76 | /// 77 | /// 随机数量 78 | /// 下限值 79 | /// 上限值 80 | /// 81 | public int[] GetRandomArray(int Number, int minNum, int maxNum) 82 | { 83 | int j; 84 | var b = new int[Number]; 85 | var r = new Random(); 86 | for (j = 0; j < Number; j++) 87 | { 88 | var i = r.Next(minNum, maxNum + 1); 89 | var num = 0; 90 | for (var k = 0; k < j; k++) 91 | if (b[k] == i) 92 | num = num + 1; 93 | if (num == 0) 94 | b[j] = i; 95 | else 96 | j = j - 1; 97 | } 98 | 99 | return b; 100 | } 101 | 102 | #endregion 103 | 104 | #region Stopwatch计时器 105 | 106 | /// 107 | /// 计时器开始 108 | /// 109 | /// 110 | public static Stopwatch TimerStart() 111 | { 112 | var watch = new Stopwatch(); 113 | watch.Reset(); 114 | watch.Start(); 115 | return watch; 116 | } 117 | 118 | /// 119 | /// 计时器结束 120 | /// 121 | /// 122 | /// 123 | public static string TimerEnd(Stopwatch watch) 124 | { 125 | watch.Stop(); 126 | double costtime = watch.ElapsedMilliseconds; 127 | return costtime.ToString(); 128 | } 129 | 130 | #endregion 131 | 132 | #region 删除最后一个字符之后的字符 133 | 134 | /// 135 | /// 删除最后结尾的一个逗号 136 | /// 137 | public static string DelLastComma(string str) 138 | { 139 | return str.Substring(0, str.LastIndexOf(",")); 140 | } 141 | 142 | /// 143 | /// 删除最后结尾的指定字符后的字符 144 | /// 145 | public static string DelLastChar(string str, string strchar) 146 | { 147 | return str.Substring(0, str.LastIndexOf(strchar)); 148 | } 149 | 150 | /// 151 | /// 删除最后结尾的长度 152 | /// 153 | /// 154 | /// 155 | /// 156 | public static string DelLastLength(string str, int Length) 157 | { 158 | if (string.IsNullOrEmpty(str)) 159 | return ""; 160 | str = str.Substring(0, str.Length - Length); 161 | return str; 162 | } 163 | 164 | #endregion 165 | } 166 | } -------------------------------------------------------------------------------- /CSum.FK/CSum/CSum.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {D4238537-E643-4A5E-98AA-1CAF53653AAB} 8 | Library 9 | Properties 10 | CSum 11 | CSum 12 | v4.5 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | bin\Debug\CSum.XML 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | ..\packages\AutoMapper.3.3.0\lib\net40\AutoMapper.dll 36 | 37 | 38 | ..\packages\AutoMapper.3.3.0\lib\net40\AutoMapper.Net4.dll 39 | 40 | 41 | ..\packages\Common.Logging.3.3.1\lib\net40\Common.Logging.dll 42 | 43 | 44 | ..\packages\Common.Logging.Core.3.3.1\lib\net40\Common.Logging.Core.dll 45 | 46 | 47 | ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll 48 | 49 | 50 | ..\packages\Spring.Aop.2.0.0\lib\net45\Spring.Aop.dll 51 | 52 | 53 | ..\packages\Spring.Core.2.0.0\lib\net45\Spring.Core.dll 54 | 55 | 56 | ..\packages\Spring.Data.2.0.0\lib\net45\Spring.Data.dll 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | {f5efc00e-7e3f-400f-9777-cca6c6ca4dbc} 86 | CSum.Util 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 102 | -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/Api Handler/HandlerSecretAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.Specialized; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Security.Cryptography; 7 | using System.Text; 8 | using System.Web; 9 | using System.Web.Http.Controllers; 10 | using System.Web.Http.Filters; 11 | using CSum.Web; 12 | using CSum.Exceptions; 13 | using CSum.Util; 14 | 15 | namespace CSum.WebApi 16 | { 17 | /// 18 | /// 安全拦截过滤器 19 | /// 20 | public class HandlerSecretAttribute : ActionFilterAttribute 21 | { 22 | private readonly ExcuteMode _customMode; 23 | 24 | /// 默认构造 25 | /// 认证模式 26 | public HandlerSecretAttribute(ExcuteMode Mode) 27 | { 28 | _customMode = Mode; 29 | } 30 | 31 | /// 32 | /// 安全校验 33 | /// 34 | /// 35 | public override void OnActionExecuting(HttpActionContext filterContext) 36 | { 37 | //是否忽略权限验证 38 | if (_customMode == ExcuteMode.Ignore) return; 39 | 40 | //从http请求的头里面获取AppId 41 | var request = filterContext.Request; 42 | var method = request.Method.Method; 43 | var appId = ""; //客户端应用唯一标识 44 | long timeStamp; //时间戳, 校验10分钟内有效 45 | var signature = ""; //参数签名,去除空参数,按字母倒序排序进行Md5签名 为了提高传参过程中,防止参数被恶意修改,在请求接口的时候加上sign可以有效防止参数被篡改 46 | try 47 | { 48 | appId = request.Headers.GetValues("appId").SingleOrDefault(); 49 | timeStamp = Convert.ToInt64(request.Headers.GetValues("timeStamp").SingleOrDefault()); 50 | signature = request.Headers.GetValues("signature").SingleOrDefault(); 51 | } 52 | catch (Exception ex) 53 | { 54 | throw new UserFriendlyException("签名参数异常:" + ex.Message); 55 | } 56 | 57 | #region 安全校验 58 | 59 | //appId是否为可用的 60 | if (!VerifyAppId(appId)) throw new UserFriendlyException("AppId不被允许访问:" + appId); 61 | 62 | var tonow = Time.StampToDateTime(timeStamp.ToString()); 63 | 64 | //请求是否超时 65 | var expires_minute = tonow.Minute - DateTime.Now.Minute; 66 | if (expires_minute > 10 || expires_minute < -10) throw new UserFriendlyException("接口请求超时:" + expires_minute); 67 | 68 | 69 | //根据请求类型拼接参数 70 | var form = HttpContext.Current.Request.QueryString; 71 | var data = string.Empty; 72 | switch (method) 73 | { 74 | case "POST": 75 | if (form.Count > 0) 76 | { 77 | data = GetFormQueryString(form); 78 | } 79 | else 80 | { 81 | var stream = HttpContext.Current.Request.InputStream; 82 | stream.Position = 0; 83 | var responseJson = string.Empty; 84 | var streamReader = new StreamReader(stream); 85 | data = streamReader.ReadToEnd(); 86 | stream.Position = 0; 87 | } 88 | break; 89 | case "GET": 90 | data = GetFormQueryString(form); 91 | break; 92 | } 93 | var result = Validate(timeStamp.ToString(), data, signature); 94 | if (!result) 95 | throw new UserFriendlyException("无效签名"); 96 | base.OnActionExecuting(filterContext); 97 | 98 | #endregion 99 | } 100 | 101 | #region 私有方法 102 | 103 | /// 104 | /// 验证appId是否被允许 105 | /// 106 | /// 107 | /// 108 | private bool VerifyAppId(string appId) 109 | { 110 | if (appId.IsEmpty()) return false; 111 | return ConfigHelper.GetValue("AllowAppId").IndexOf(appId) > -1; 112 | } 113 | 114 | /// 115 | /// 表单查询参数, url上直接接参数时,通过此方法获取 116 | /// 117 | /// 118 | /// 119 | private string GetFormQueryString(NameValueCollection form) 120 | { 121 | //第一步:取出所有get参数 122 | IDictionary parameters = new Dictionary(); 123 | for (var f = 0; f < form.Count; f++) 124 | { 125 | var key = form.Keys[f]; 126 | parameters.Add(key, form[key]); 127 | } 128 | 129 | // 第二步:把字典按Key的字母顺序排序 130 | IDictionary sortedParams = new SortedDictionary(parameters); 131 | var dem = sortedParams.GetEnumerator(); 132 | 133 | // 第三步:把所有参数名和参数值串在一起 134 | var query = new StringBuilder(); 135 | while (dem.MoveNext()) 136 | { 137 | var key = dem.Current.Key; 138 | var value = dem.Current.Value; 139 | if (!string.IsNullOrEmpty(key)) query.Append(key).Append(value); 140 | } 141 | 142 | return query.ToString(); 143 | } 144 | 145 | /// 146 | /// 签名校验 147 | /// 148 | /// 149 | /// 150 | /// 151 | /// 152 | public bool Validate(string timeStamp, string data, string signature) 153 | { 154 | var hash = MD5.Create(); 155 | //拼接签名数据 156 | var signStr = timeStamp + data; 157 | //将字符串中字符按升序排序 158 | var sortStr = string.Concat(signStr.OrderBy(c => c)); 159 | var bytes = Encoding.UTF8.GetBytes(sortStr); 160 | //使用32位大写 MD5签名 161 | var md5Val = hash.ComputeHash(bytes); 162 | var result = new StringBuilder(); 163 | foreach (var c in md5Val) result.Append(c.ToString("X2")); 164 | var s = result.ToString().ToUpper(); 165 | //与前端传过来的签名参数进行比对 166 | return s == signature; 167 | } 168 | #endregion 169 | } 170 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/Web.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 |
10 | 11 |
12 | 13 | 14 |
15 |
16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/Extensions/Ext.Convert.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Data; 5 | using System.Data.SqlTypes; 6 | 7 | namespace CSum.Util 8 | { 9 | /// 10 | /// 类型转换扩展 11 | /// 12 | public static partial class Extensions 13 | { 14 | #region 字符串转换 15 | 16 | /// 17 | /// 转换为字符串 18 | /// 19 | /// 数据 20 | public static string ToString(this object data) 21 | { 22 | return data == null ? string.Empty : data.ToString().Trim(); 23 | } 24 | 25 | #endregion 26 | 27 | #region 安全返回值 28 | 29 | /// 30 | /// 安全返回值 31 | /// 32 | /// 可空值 33 | public static T SafeValue(this T? value) where T : struct 34 | { 35 | return value ?? default(T); 36 | } 37 | 38 | #endregion 39 | 40 | #region 是否为空 41 | 42 | /// 43 | /// 是否为空 44 | /// 45 | /// 值 46 | public static bool IsEmpty(this object value) 47 | { 48 | if (value != null) 49 | { 50 | if (value is ICollection) 51 | { 52 | return ((ICollection)value).Count == 0; 53 | } 54 | else if (value is DateTime) 55 | { 56 | return (DateTime)value==DateTime.MinValue; 57 | } 58 | else if (value.GetType().IsBaseOn()) 59 | { 60 | return ((INullable)value).IsNull; 61 | } 62 | else if (value is DataTable) 63 | { 64 | var dt = ((DataTable) value); 65 | return (dt.Rows.Count==0); 66 | } 67 | else if (value is DBNull) 68 | { 69 | return true; 70 | } 71 | else 72 | { 73 | return string.IsNullOrWhiteSpace(value.ToString()); 74 | } 75 | 76 | } 77 | return true; 78 | } 79 | 80 | #endregion 81 | 82 | #region 数值转换 83 | 84 | /// 85 | /// 转换为整型 86 | /// 87 | /// 数据 88 | public static int ToInt(this object data) 89 | { 90 | if (data == null) 91 | return 0; 92 | int result; 93 | var success = int.TryParse(data.ToString(), out result); 94 | if (success) 95 | return result; 96 | try 97 | { 98 | return Convert.ToInt32(ToDouble(data, 0)); 99 | } 100 | catch (Exception) 101 | { 102 | return 0; 103 | } 104 | } 105 | 106 | /// 107 | /// 转换为可空整型 108 | /// 109 | /// 数据 110 | public static int? ToIntOrNull(this object data) 111 | { 112 | if (data == null) 113 | return null; 114 | int result; 115 | var isValid = int.TryParse(data.ToString(), out result); 116 | if (isValid) 117 | return result; 118 | return null; 119 | } 120 | 121 | /// 122 | /// 转换为双精度浮点数 123 | /// 124 | /// 数据 125 | public static double ToDouble(this object data) 126 | { 127 | if (data == null) 128 | return 0; 129 | double result; 130 | return double.TryParse(data.ToString(), out result) ? result : 0; 131 | } 132 | 133 | /// 134 | /// 转换为双精度浮点数,并按指定的小数位4舍5入 135 | /// 136 | /// 数据 137 | /// 小数位数 138 | public static double ToDouble(this object data, int digits) 139 | { 140 | return Math.Round(ToDouble(data), digits); 141 | } 142 | 143 | /// 144 | /// 转换为可空双精度浮点数 145 | /// 146 | /// 数据 147 | public static double? ToDoubleOrNull(this object data) 148 | { 149 | if (data == null) 150 | return null; 151 | double result; 152 | var isValid = double.TryParse(data.ToString(), out result); 153 | if (isValid) 154 | return result; 155 | return null; 156 | } 157 | 158 | /// 159 | /// 转换为高精度浮点数 160 | /// 161 | /// 数据 162 | public static decimal ToDecimal(this object data) 163 | { 164 | if (data == null) 165 | return 0; 166 | decimal result; 167 | return decimal.TryParse(data.ToString(), out result) ? result : 0; 168 | } 169 | 170 | /// 171 | /// 转换为高精度浮点数,并按指定的小数位4舍5入 172 | /// 173 | /// 数据 174 | /// 小数位数 175 | public static decimal ToDecimal(this object data, int digits) 176 | { 177 | return Math.Round(ToDecimal(data), digits); 178 | } 179 | 180 | /// 181 | /// 转换为可空高精度浮点数 182 | /// 183 | /// 数据 184 | public static decimal? ToDecimalOrNull(this object data) 185 | { 186 | if (data == null) 187 | return null; 188 | decimal result; 189 | var isValid = decimal.TryParse(data.ToString(), out result); 190 | if (isValid) 191 | return result; 192 | return null; 193 | } 194 | 195 | /// 196 | /// 转换为可空高精度浮点数,并按指定的小数位4舍5入 197 | /// 198 | /// 数据 199 | /// 小数位数 200 | public static decimal? ToDecimalOrNull(this object data, int digits) 201 | { 202 | var result = ToDecimalOrNull(data); 203 | if (result == null) 204 | return null; 205 | return Math.Round(result.Value, digits); 206 | } 207 | 208 | #endregion 209 | 210 | #region 日期转换 211 | 212 | /// 213 | /// 转换为日期 214 | /// 215 | /// 数据 216 | public static DateTime ToDate(this object data) 217 | { 218 | if (data == null) 219 | return DateTime.MinValue; 220 | DateTime result; 221 | return DateTime.TryParse(data.ToString(), out result) ? result : new DateTime(1900, 1, 1); 222 | } 223 | 224 | /// 225 | /// 转换为可空日期 226 | /// 227 | /// 数据 228 | public static DateTime? ToDateOrNull(this object data) 229 | { 230 | if (data == null) 231 | return null; 232 | DateTime result; 233 | var isValid = DateTime.TryParse(data.ToString(), out result); 234 | if (isValid) 235 | return result; 236 | return null; 237 | } 238 | 239 | #endregion 240 | 241 | #region 布尔转换 242 | 243 | /// 244 | /// 转换为布尔值 245 | /// 246 | /// 数据 247 | public static bool ToBool(this object data) 248 | { 249 | if (data == null) 250 | return false; 251 | var value = GetBool(data); 252 | if (value != null) 253 | return value.Value; 254 | bool result; 255 | return bool.TryParse(data.ToString(), out result) && result; 256 | } 257 | 258 | /// 259 | /// 获取布尔值 260 | /// 261 | private static bool? GetBool(this object data) 262 | { 263 | switch (data.ToString().Trim().ToLower()) 264 | { 265 | case "0": 266 | return false; 267 | case "1": 268 | return true; 269 | case "是": 270 | return true; 271 | case "否": 272 | return false; 273 | case "yes": 274 | return true; 275 | case "no": 276 | return false; 277 | default: 278 | return null; 279 | } 280 | } 281 | 282 | /// 283 | /// 转换为可空布尔值 284 | /// 285 | /// 数据 286 | public static bool? ToBoolOrNull(this object data) 287 | { 288 | if (data == null) 289 | return null; 290 | var value = GetBool(data); 291 | if (value != null) 292 | return value.Value; 293 | bool result; 294 | var isValid = bool.TryParse(data.ToString(), out result); 295 | if (isValid) 296 | return result; 297 | return null; 298 | } 299 | 300 | #endregion 301 | } 302 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/Extensions/Ext.Type.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.ComponentModel.DataAnnotations.Schema; 7 | using System.Data; 8 | using System.Linq; 9 | using System.Reflection; 10 | using System.Threading.Tasks; 11 | 12 | namespace CSum.Util 13 | { 14 | /// 15 | /// 类型辅助扩展方法类 16 | /// 17 | public static partial class Extensions 18 | { 19 | /// 20 | /// 判断类型是否为Nullable类型 21 | /// 22 | /// 要处理的类型 23 | /// 是返回True,不是返回False 24 | public static bool IsNullableType(this Type type) 25 | { 26 | return type != null && type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); 27 | } 28 | 29 | /// 30 | /// 由类型的Nullable类型返回实际类型 31 | /// 32 | /// 要处理的类型对象 33 | /// 34 | public static Type GetNonNummableType(this Type type) 35 | { 36 | if (IsNullableType(type)) return type.GetGenericArguments()[0]; 37 | return type; 38 | } 39 | 40 | /// 41 | /// 通过类型转换器获取Nullable类型的基础类型 42 | /// 43 | /// 要处理的类型对象 44 | /// 45 | public static Type GetUnNullableType(this Type type) 46 | { 47 | if (IsNullableType(type)) 48 | { 49 | var nullableConverter = new NullableConverter(type); 50 | return nullableConverter.UnderlyingType; 51 | } 52 | 53 | return type; 54 | } 55 | 56 | /// 57 | /// 获取类型的Description特性描述信息 58 | /// 59 | /// 类型对象 60 | /// 是否搜索类型的继承链以查找描述特性 61 | /// 返回Description特性描述信息,如不存在则返回类型的全名 62 | public static string ToDescription(this Type type, bool inherit = false) 63 | { 64 | var desc = type.GetAttribute(inherit); 65 | return desc == null ? type.FullName : desc.Description; 66 | } 67 | 68 | /// 69 | /// 获取类型的Table特性信息 70 | /// 71 | /// 类型对象 72 | /// 是否搜索类型的继承链以查找描述特性 73 | /// 返回Table特性描述信息,如不存在则返回类型的全名 74 | public static string ToTableName(this Type type, bool inherit = false) 75 | { 76 | var desc = type.GetAttribute(inherit); 77 | return desc == null ? type.FullName : desc.Name; 78 | } 79 | 80 | /// 81 | /// 获取成员元数据的Description/DisplayName/Display特性描述信息 82 | /// 83 | /// 成员元数据对象 84 | /// 是否搜索成员的继承链以查找描述特性 85 | /// 返回Description/DisplayName/Display特性描述信息,如不存在则返回成员的名称 86 | public static string ToDescription(this MemberInfo member, bool inherit = false) 87 | { 88 | var desc = member.GetAttribute(inherit); 89 | if (desc != null) return desc.Description; 90 | var displayName = member.GetAttribute(inherit); 91 | if (displayName != null) return displayName.DisplayName; 92 | var display = member.GetAttribute(inherit); 93 | if (display != null) return display.Name; 94 | return member.Name; 95 | } 96 | 97 | /// 98 | /// 获取类型的Description/DisplayName特性描述信息 99 | /// 100 | /// 成员元数据对象 101 | /// 是否搜索成员的继承链以查找描述特性 102 | /// 返回Description/DisplayName特性描述信息,如不存在则返回成员的名称 103 | public static string ToDisplayName(this Type type, bool inherit = true) 104 | { 105 | var desc = type.GetAttribute(inherit); 106 | if (desc != null) return desc.Description; 107 | var displayName = type.GetAttribute(inherit); 108 | if (displayName != null) return displayName.DisplayName; 109 | var display = type.GetAttribute(inherit); 110 | if (display != null) return display.Name; 111 | return type.Name; 112 | } 113 | 114 | /// 115 | /// 获取成员元数据的Column特性描述信息 116 | /// 117 | /// 成员元数据对象 118 | /// 是否搜索成员的继承链以查找描述特性 119 | /// 返回Column特性描述信息,如不存在则返回成员的名称 120 | public static string ToColumnName(this MemberInfo member, bool inherit = false) 121 | { 122 | var display = member.GetAttribute(inherit); 123 | if (display != null) return display.Name; 124 | return member.Name; 125 | } 126 | 127 | /// 128 | /// 检查指定指定类型成员中是否存在指定的Attribute特性 129 | /// 130 | /// 要检查的Attribute特性类型 131 | /// 要检查的类型成员 132 | /// 是否从继承中查找 133 | /// 是否存在 134 | public static bool HasAttribute(this MemberInfo memberInfo, bool inherit = false) where T : Attribute 135 | { 136 | return memberInfo.IsDefined(typeof(T), inherit); 137 | //return memberInfo.GetCustomAttributes(typeof(T), inherit).Any(m => (m as T) != null); 138 | } 139 | 140 | /// 141 | /// 从类型成员获取指定Attribute特性 142 | /// 143 | /// Attribute特性类型 144 | /// 类型类型成员 145 | /// 是否从继承中查找 146 | /// 存在返回第一个,不存在返回null 147 | public static T GetAttribute(this MemberInfo memberInfo, bool inherit = false) where T : Attribute 148 | { 149 | var descripts = memberInfo.GetCustomAttributes(typeof(T), inherit); 150 | return descripts.FirstOrDefault() as T; 151 | } 152 | 153 | /// 154 | /// 从类型成员获取指定Attribute特性 155 | /// 156 | /// Attribute特性类型 157 | /// 类型类型成员 158 | /// 是否从继承中查找 159 | /// 返回所有指定Attribute特性的数组 160 | public static T[] GetAttributes(this MemberInfo memberInfo, bool inherit = false) where T : Attribute 161 | { 162 | return memberInfo.GetCustomAttributes(typeof(T), inherit).Cast().ToArray(); 163 | } 164 | 165 | /// 166 | /// 判断类型是否为集合类型 167 | /// 168 | /// 要处理的类型 169 | /// 是返回True,不是返回False 170 | public static bool IsEnumerable(this Type type) 171 | { 172 | if (type == typeof(string)) return false; 173 | return typeof(IEnumerable).IsAssignableFrom(type); 174 | } 175 | 176 | /// 177 | /// 判断当前泛型类型是否可由指定类型的实例填充 178 | /// 179 | /// 泛型类型 180 | /// 指定类型 181 | /// 182 | public static bool IsGenericAssignableFrom(this Type genericType, Type type) 183 | { 184 | if (!genericType.IsGenericType) throw new ArgumentException("该功能只支持泛型类型的调用,非泛型类型可使用 IsAssignableFrom 方法。"); 185 | 186 | var allOthers = new List {type}; 187 | if (genericType.IsInterface) allOthers.AddRange(type.GetInterfaces()); 188 | 189 | foreach (var other in allOthers) 190 | { 191 | var cur = other; 192 | while (cur != null) 193 | { 194 | if (cur.IsGenericType) cur = cur.GetGenericTypeDefinition(); 195 | if (cur.IsSubclassOf(genericType) || cur == genericType) return true; 196 | cur = cur.BaseType; 197 | } 198 | } 199 | 200 | return false; 201 | } 202 | 203 | /// 204 | /// 方法是否是异步 205 | /// 206 | public static bool IsAsync(this MethodInfo method) 207 | { 208 | return method.ReturnType == typeof(Task) 209 | || method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>); 210 | } 211 | 212 | /// 213 | /// 返回当前类型是否是指定基类的派生类 214 | /// 215 | /// 当前类型 216 | /// 要判断的基类型 217 | /// 218 | public static bool IsBaseOn(this Type type, Type baseType) 219 | { 220 | if (type.IsGenericTypeDefinition) return baseType.IsGenericAssignableFrom(type); 221 | return baseType.IsAssignableFrom(type); 222 | } 223 | 224 | /// 225 | /// 返回当前类型是否是指定基类的派生类 226 | /// 227 | /// 要判断的基类型 228 | /// 当前类型 229 | /// 230 | public static bool IsBaseOn(this Type type) 231 | { 232 | var baseType = typeof(TBaseType); 233 | return type.IsBaseOn(baseType); 234 | } 235 | 236 | /// 237 | /// 属性类型转换成数据库类型 238 | /// 239 | /// 240 | /// 241 | public static DbType ToDbType(this Type pType) 242 | { 243 | var map = new Dictionary(); 244 | map[typeof(byte)] = DbType.Byte; 245 | map[typeof(sbyte)] = DbType.SByte; 246 | map[typeof(short)] = DbType.Int16; 247 | map[typeof(ushort)] = DbType.UInt16; 248 | map[typeof(int)] = DbType.Int32; 249 | map[typeof(uint)] = DbType.UInt32; 250 | map[typeof(long)] = DbType.Int64; 251 | map[typeof(ulong)] = DbType.UInt64; 252 | map[typeof(float)] = DbType.Single; 253 | map[typeof(double)] = DbType.Double; 254 | map[typeof(decimal)] = DbType.Decimal; 255 | map[typeof(bool)] = DbType.Boolean; 256 | map[typeof(string)] = DbType.String; 257 | map[typeof(char)] = DbType.StringFixedLength; 258 | map[typeof(Guid)] = DbType.Guid; 259 | map[typeof(DateTime)] = DbType.DateTime; 260 | map[typeof(DateTimeOffset)] = DbType.DateTimeOffset; 261 | map[typeof(byte[])] = DbType.Binary; 262 | map[typeof(byte?)] = DbType.Byte; 263 | map[typeof(sbyte?)] = DbType.SByte; 264 | map[typeof(short?)] = DbType.Int16; 265 | map[typeof(ushort?)] = DbType.UInt16; 266 | map[typeof(int?)] = DbType.Int32; 267 | map[typeof(uint?)] = DbType.UInt32; 268 | map[typeof(long?)] = DbType.Int64; 269 | map[typeof(ulong?)] = DbType.UInt64; 270 | map[typeof(float?)] = DbType.Single; 271 | map[typeof(double?)] = DbType.Double; 272 | map[typeof(decimal?)] = DbType.Decimal; 273 | map[typeof(bool?)] = DbType.Boolean; 274 | map[typeof(char?)] = DbType.StringFixedLength; 275 | map[typeof(Guid?)] = DbType.Guid; 276 | map[typeof(DateTime?)] = DbType.DateTime; 277 | map[typeof(DateTimeOffset?)] = DbType.DateTimeOffset; 278 | //typeMap[typeof(System.Data.Linq.Binary)] = System.Data.DbType.Binary; 279 | var dtype = DbType.String; 280 | try 281 | { 282 | dtype = map[pType]; 283 | } 284 | catch (Exception ex) 285 | { 286 | } 287 | 288 | return dtype; 289 | } 290 | } 291 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.WebApi/CSum.WebApi.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 8 | 9 | 2.0 10 | {D521EFDC-F0A2-4E23-98AB-5C0C51FE0A92} 11 | {349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} 12 | Library 13 | Properties 14 | CSum.WebApi 15 | CSum.WebApi 16 | v4.5 17 | false 18 | true 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | ..\..\packages\WebGrease.1.5.2\lib 27 | 28 | 29 | true 30 | full 31 | false 32 | bin\ 33 | DEBUG;TRACE 34 | prompt 35 | 2 36 | false 37 | bin\CSum.WebApi.xml 38 | 39 | 40 | pdbonly 41 | true 42 | bin\ 43 | TRACE 44 | prompt 45 | 4 46 | false 47 | 48 | 49 | 50 | ..\packages\Antlr.3.4.1.9004\lib\Antlr3.Runtime.dll 51 | 52 | 53 | ..\packages\Common.Logging.3.3.1\lib\net40\Common.Logging.dll 54 | 55 | 56 | ..\packages\Common.Logging.Core.3.3.1\lib\net40\Common.Logging.Core.dll 57 | 58 | 59 | ..\packages\Common.Logging.Log4Net1211.3.3.1\lib\net40\Common.Logging.Log4Net1211.dll 60 | 61 | 62 | ..\packages\log4net.2.0.0\lib\net40-full\log4net.dll 63 | 64 | 65 | 66 | ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll 67 | 68 | 69 | ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll 70 | 71 | 72 | ..\CSum.Lib\Swashbuckle.Core.dll 73 | 74 | 75 | 76 | 77 | 78 | 79 | ..\packages\Microsoft.AspNet.WebApi.Client.5.2.6\lib\net45\System.Net.Http.Formatting.dll 80 | 81 | 82 | ..\packages\Microsoft.AspNet.Cors.5.2.6\lib\net45\System.Web.Cors.dll 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll 93 | 94 | 95 | ..\packages\Microsoft.AspNet.WebApi.Core.5.2.6\lib\net45\System.Web.Http.dll 96 | 97 | 98 | ..\packages\Microsoft.AspNet.WebApi.Cors.5.2.3\lib\net45\System.Web.Http.Cors.dll 99 | 100 | 101 | ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.6\lib\net45\System.Web.Http.WebHost.dll 102 | 103 | 104 | ..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll 105 | 106 | 107 | ..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll 108 | 109 | 110 | ..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll 111 | 112 | 113 | 114 | ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll 115 | 116 | 117 | ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll 118 | 119 | 120 | ..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll 121 | 122 | 123 | 124 | 125 | 126 | 127 | True 128 | ..\packages\Microsoft.Net.Http.2.0.30506.0\lib\net40\System.Net.Http.dll 129 | 130 | 131 | True 132 | ..\packages\Microsoft.Net.Http.2.0.30506.0\lib\net40\System.Net.Http.WebRequest.dll 133 | 134 | 135 | 136 | ..\packages\WebGrease.1.5.2\lib\WebGrease.dll 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | Designer 149 | 150 | 151 | 152 | 153 | {bdcc2b41-41f9-4fd6-903f-db7e02a93929} 154 | CSum.Offices 155 | 156 | 157 | {f5efc00e-7e3f-400f-9777-cca6c6ca4dbc} 158 | CSum.Util 159 | 160 | 161 | {d4238537-e643-4a5e-98aa-1caf53653aab} 162 | CSum 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 10.0 171 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | True 184 | True 185 | 10428 186 | / 187 | http://localhost:13001 188 | False 189 | False 190 | 191 | 192 | False 193 | 194 | 195 | 196 | 197 | 203 | -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/Helper/DataTableHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Data; 5 | using System.Reflection; 6 | 7 | namespace CSum.Offices 8 | { 9 | /// 10 | /// DataTable操作辅助类 11 | /// 12 | public class DataTableHelper 13 | { 14 | public static void AddTableData(DataTable dt1, DataTable dt2) 15 | { 16 | if (dt2 == null || dt2.Rows.Count == 0) return; 17 | // 改进后的方法 18 | DataRow drcalc; 19 | foreach (DataRow dr in dt2.Rows) 20 | { 21 | drcalc = dt1.NewRow(); 22 | drcalc.ItemArray = dr.ItemArray; 23 | dt1.Rows.Add(drcalc); 24 | } 25 | } 26 | 27 | /// 28 | /// 给DataTable增加一个自增列 29 | /// 如果DataTable 存在 identityid 字段 则 直接返回DataTable 不做任何处理 30 | /// 31 | /// DataTable 32 | /// 返回Datatable 增加字段 identityid 33 | public static DataTable AddIdentityColumn(DataTable dt) 34 | { 35 | if (!dt.Columns.Contains("identityid")) 36 | { 37 | dt.Columns.Add("identityid"); 38 | for (var i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["identityid"] = (i + 1).ToString(); 39 | } 40 | 41 | return dt; 42 | } 43 | 44 | /// 45 | /// 检查DataTable 是否有数据行 46 | /// 47 | /// DataTable 48 | /// 49 | public static bool IsHaveRows(DataTable dt) 50 | { 51 | if (dt != null && dt.Rows.Count > 0) 52 | return true; 53 | 54 | return false; 55 | } 56 | 57 | /// 58 | /// DataTable转换成实体列表 59 | /// 60 | /// 实体 T 61 | /// datatable 62 | /// 63 | public static IList DataTableToList(DataTable table) 64 | where T : class 65 | { 66 | if (!IsHaveRows(table)) 67 | return new List(); 68 | 69 | IList list = new List(); 70 | var model = default(T); 71 | foreach (DataRow dr in table.Rows) 72 | { 73 | model = Activator.CreateInstance(); 74 | 75 | foreach (DataColumn dc in dr.Table.Columns) 76 | { 77 | var drValue = dr[dc.ColumnName]; 78 | var pi = model.GetType().GetProperty(dc.ColumnName); 79 | 80 | if (pi != null && pi.CanWrite && drValue != null && !Convert.IsDBNull(drValue)) 81 | pi.SetValue(model, drValue, null); 82 | } 83 | 84 | list.Add(model); 85 | } 86 | 87 | return list; 88 | } 89 | 90 | /// 91 | /// 实体列表转换成DataTable 92 | /// 93 | /// 实体 94 | /// 实体列表 95 | /// 96 | public static DataTable ListToDataTable(IList list) 97 | where T : class 98 | { 99 | if (list == null || list.Count <= 0) return null; 100 | var dt = new DataTable(typeof(T).Name); 101 | DataColumn column; 102 | DataRow row; 103 | 104 | var myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); 105 | 106 | var length = myPropertyInfo.Length; 107 | var createColumn = true; 108 | 109 | foreach (var t in list) 110 | { 111 | if (t == null) continue; 112 | 113 | row = dt.NewRow(); 114 | for (var i = 0; i < length; i++) 115 | { 116 | var pi = myPropertyInfo[i]; 117 | var name = pi.Name; 118 | if (createColumn) 119 | { 120 | column = new DataColumn(name, pi.PropertyType); 121 | dt.Columns.Add(column); 122 | } 123 | 124 | row[name] = pi.GetValue(t, null); 125 | } 126 | 127 | if (createColumn) createColumn = false; 128 | 129 | dt.Rows.Add(row); 130 | } 131 | 132 | return dt; 133 | } 134 | 135 | /// 136 | /// 将泛型集合类转换成DataTable 137 | /// 138 | /// 集合项类型 139 | /// 集合 140 | /// 数据集(表) 141 | public static DataTable ToDataTable(IList list) 142 | { 143 | return ToDataTable(list, null); 144 | } 145 | 146 | /// 147 | /// 将泛型集合类转换成DataTable 148 | /// 149 | /// 集合项类型 150 | /// 集合 151 | /// 需要返回的列的列名 152 | /// 数据集(表) 153 | public static DataTable ToDataTable(IList list, params string[] propertyName) 154 | { 155 | var propertyNameList = new List(); 156 | if (propertyName != null) 157 | propertyNameList.AddRange(propertyName); 158 | 159 | var result = new DataTable(); 160 | if (list.Count > 0) 161 | { 162 | var propertys = list[0].GetType().GetProperties(); 163 | foreach (var pi in propertys) 164 | if (propertyNameList.Count == 0) 165 | { 166 | result.Columns.Add(pi.Name, pi.PropertyType); 167 | } 168 | else 169 | { 170 | if (propertyNameList.Contains(pi.Name)) result.Columns.Add(pi.Name, pi.PropertyType); 171 | } 172 | 173 | for (var i = 0; i < list.Count; i++) 174 | { 175 | var tempList = new ArrayList(); 176 | foreach (var pi in propertys) 177 | if (propertyNameList.Count == 0) 178 | { 179 | var obj = pi.GetValue(list[i], null); 180 | tempList.Add(obj); 181 | } 182 | else 183 | { 184 | if (propertyNameList.Contains(pi.Name)) 185 | { 186 | var obj = pi.GetValue(list[i], null); 187 | tempList.Add(obj); 188 | } 189 | } 190 | 191 | var array = tempList.ToArray(); 192 | result.LoadDataRow(array, true); 193 | } 194 | } 195 | 196 | return result; 197 | } 198 | 199 | /// 200 | /// 根据nameList里面的字段创建一个表格,返回该表格的DataTable 201 | /// 202 | /// 包含字段信息的列表 203 | /// DataTable 204 | public static DataTable CreateTable(List nameList) 205 | { 206 | if (nameList.Count <= 0) 207 | return null; 208 | 209 | var myDataTable = new DataTable(); 210 | foreach (var columnName in nameList) myDataTable.Columns.Add(columnName, typeof(string)); 211 | return myDataTable; 212 | } 213 | 214 | /// 215 | /// 通过字符列表创建表字段,字段格式可以是: 216 | /// 1) a,b,c,d,e 217 | /// 2) a|int,b|string,c|bool,d|decimal 218 | /// 219 | /// 220 | /// 221 | public static DataTable CreateTable(string nameString) 222 | { 223 | var nameArray = nameString.Split(',', ';'); 224 | var nameList = new List(); 225 | var dt = new DataTable(); 226 | foreach (var item in nameArray) 227 | if (!string.IsNullOrEmpty(item)) 228 | { 229 | var subItems = item.Split('|'); 230 | if (subItems.Length == 2) 231 | dt.Columns.Add(subItems[0], ConvertType(subItems[1])); 232 | else 233 | dt.Columns.Add(subItems[0]); 234 | } 235 | 236 | return dt; 237 | } 238 | 239 | private static Type ConvertType(string typeName) 240 | { 241 | typeName = typeName.ToLower().Replace("system.", ""); 242 | var newType = typeof(string); 243 | switch (typeName) 244 | { 245 | case "boolean": 246 | case "bool": 247 | newType = typeof(bool); 248 | break; 249 | case "int16": 250 | case "short": 251 | newType = typeof(short); 252 | break; 253 | case "int32": 254 | case "int": 255 | newType = typeof(int); 256 | break; 257 | case "long": 258 | case "int64": 259 | newType = typeof(long); 260 | break; 261 | case "uint16": 262 | case "ushort": 263 | newType = typeof(ushort); 264 | break; 265 | case "uint32": 266 | case "uint": 267 | newType = typeof(uint); 268 | break; 269 | case "uint64": 270 | case "ulong": 271 | newType = typeof(ulong); 272 | break; 273 | case "single": 274 | case "float": 275 | newType = typeof(float); 276 | break; 277 | 278 | case "string": 279 | newType = typeof(string); 280 | break; 281 | case "guid": 282 | newType = typeof(Guid); 283 | break; 284 | case "decimal": 285 | newType = typeof(decimal); 286 | break; 287 | case "double": 288 | newType = typeof(double); 289 | break; 290 | case "datetime": 291 | newType = typeof(DateTime); 292 | break; 293 | case "byte": 294 | newType = typeof(byte); 295 | break; 296 | case "char": 297 | newType = typeof(char); 298 | break; 299 | } 300 | 301 | return newType; 302 | } 303 | 304 | /// 305 | /// 获得从DataRowCollection转换成的DataRow数组 306 | /// 307 | /// DataRowCollection 308 | /// 309 | public static DataRow[] GetDataRowArray(DataRowCollection drc) 310 | { 311 | var count = drc.Count; 312 | var drs = new DataRow[count]; 313 | for (var i = 0; i < count; i++) drs[i] = drc[i]; 314 | return drs; 315 | } 316 | 317 | /// 318 | /// 将DataRow数组转换成DataTable,注意行数组的每个元素须具有相同的数据结构, 319 | /// 否则当有元素长度大于第一个元素时,抛出异常 320 | /// 321 | /// 行数组 322 | /// 323 | public static DataTable GetTableFromRows(DataRow[] rows) 324 | { 325 | if (rows.Length <= 0) return new DataTable(); 326 | var dt = rows[0].Table.Clone(); 327 | dt.DefaultView.Sort = rows[0].Table.DefaultView.Sort; 328 | for (var i = 0; i < rows.Length; i++) dt.LoadDataRow(rows[i].ItemArray, true); 329 | return dt; 330 | } 331 | 332 | /// 333 | /// 排序表的视图 334 | /// 335 | /// 336 | /// 337 | /// 338 | public static DataTable SortedTable(DataTable dt, params string[] sorts) 339 | { 340 | if (dt.Rows.Count > 0) 341 | { 342 | var tmp = ""; 343 | for (var i = 0; i < sorts.Length; i++) tmp += sorts[i] + ","; 344 | dt.DefaultView.Sort = tmp.TrimEnd(','); 345 | } 346 | 347 | return dt; 348 | } 349 | 350 | /// 351 | /// 根据条件过滤表的内容 352 | /// 353 | /// 354 | /// 355 | /// 356 | public static DataTable FilterDataTable(DataTable dt, string condition) 357 | { 358 | if (condition.Trim() == "") return dt; 359 | var newdt = new DataTable(); 360 | newdt = dt.Clone(); 361 | var dr = dt.Select(condition); 362 | for (var i = 0; i < dr.Length; i++) newdt.ImportRow(dr[i]); 363 | return newdt; 364 | } 365 | 366 | /// 367 | /// 转换.NET的Type到数据库参数的类型 368 | /// 369 | /// 370 | /// 371 | public static DbType TypeToDbType(Type t) 372 | { 373 | DbType dbt; 374 | try 375 | { 376 | dbt = (DbType) Enum.Parse(typeof(DbType), t.Name); 377 | } 378 | catch 379 | { 380 | dbt = DbType.Object; 381 | } 382 | 383 | return dbt; 384 | } 385 | 386 | /// 387 | /// 使用分隔符串联表格字段的内容,如:a,b,c 388 | /// 389 | /// 表格 390 | /// 字段名称 391 | /// 增加的字符串,无则为空 392 | /// 分隔符,如逗号(,) 393 | /// 394 | public static string ConcatColumnValue(DataTable dt, string columnName, string append, char splitChar) 395 | { 396 | var result = append; 397 | if (dt != null && dt.Rows.Count > 0) 398 | foreach (DataRow row in dt.Rows) 399 | result += string.Format("{0}{1}", splitChar, row[columnName]); 400 | return result.Trim(splitChar); 401 | } 402 | 403 | /// 404 | /// 使用逗号串联表格字段的内容,如:a,b,c 405 | /// 406 | /// 表格 407 | /// 字段名称 408 | /// 增加的字符串,无则为空 409 | /// 410 | public static string ConcatColumnValue(DataTable dt, string columnName, string append) 411 | { 412 | var result = append; 413 | if (dt != null && dt.Rows.Count > 0) 414 | foreach (DataRow row in dt.Rows) 415 | result += string.Format(",{0}", row[columnName]); 416 | return result.Trim(','); 417 | } 418 | } 419 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/Excel/ExcelHelper.T.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Drawing; 5 | using System.IO; 6 | using System.Reflection; 7 | using System.Text; 8 | using System.Web; 9 | using NPOI.HPSF; 10 | using NPOI.HSSF.Record; 11 | using NPOI.HSSF.UserModel; 12 | using NPOI.SS.UserModel; 13 | using NPOI.SS.Util; 14 | 15 | namespace CSum.Offices.Excel 16 | { 17 | /// 18 | /// 描 述:NPOI Excel泛型操作类 19 | public class ExcelHelper 20 | { 21 | #region Excel导出方法 ExcelDownload 22 | 23 | /// 24 | /// Excel导出下载 25 | /// 26 | /// List数据源 27 | /// 导出设置包含文件名、标题、列设置 28 | public static void ExcelDownload(List lists, ExcelConfig excelConfig) 29 | { 30 | var curContext = HttpContext.Current; 31 | // 设置编码和附件格式 32 | curContext.Response.ContentType = "application/ms-excel"; 33 | curContext.Response.ContentEncoding = Encoding.UTF8; 34 | curContext.Response.Charset = ""; 35 | curContext.Response.AppendHeader("Content-Disposition", 36 | "attachment;filename=" + HttpUtility.UrlEncode(excelConfig.FileName, Encoding.UTF8)); 37 | //调用导出具体方法Export() 38 | curContext.Response.BinaryWrite(ExportMemoryStream(lists, excelConfig).GetBuffer()); 39 | curContext.Response.End(); 40 | } 41 | 42 | #endregion 43 | 44 | #region DataTable导出到Excel文件excelConfig中FileName设置为全路径 45 | 46 | /// 47 | /// List导出到Excel文件 ExcelImport 48 | /// 49 | /// List数据源 50 | /// 导出设置包含文件名、标题、列设置 51 | public static void ExcelImport(List lists, ExcelConfig excelConfig) 52 | { 53 | using (var ms = ExportMemoryStream(lists, excelConfig)) 54 | { 55 | using (var fs = new FileStream(excelConfig.FileName, FileMode.Create, FileAccess.Write)) 56 | { 57 | var data = ms.ToArray(); 58 | fs.Write(data, 0, data.Length); 59 | fs.Flush(); 60 | } 61 | } 62 | } 63 | 64 | #endregion 65 | 66 | #region DataTable导出到Excel的MemoryStream 67 | 68 | /// 69 | /// DataTable导出到Excel的MemoryStream Export() 70 | /// 71 | /// DataTable数据源 72 | /// 导出设置包含文件名、标题、列设置 73 | public static MemoryStream ExportMemoryStream(List lists, ExcelConfig excelConfig) 74 | { 75 | var workbook = new HSSFWorkbook(); 76 | var sheet = workbook.CreateSheet(); 77 | var type = typeof(T); 78 | var properties = type.GetProperties(); 79 | 80 | #region 右击文件 属性信息 81 | 82 | { 83 | var dsi = PropertySetFactory.CreateDocumentSummaryInformation(); 84 | dsi.Company = "NPOI"; 85 | workbook.DocumentSummaryInformation = dsi; 86 | 87 | var si = PropertySetFactory.CreateSummaryInformation(); 88 | si.Author = "刘晓雷"; //填加xls文件作者信息 89 | si.ApplicationName = "力软信息"; //填加xls文件创建程序信息 90 | si.LastAuthor = "刘晓雷"; //填加xls文件最后保存者信息 91 | si.Comments = "刘晓雷"; //填加xls文件作者信息 92 | si.Title = "标题信息"; //填加xls文件标题信息 93 | si.Subject = "主题信息"; //填加文件主题信息 94 | si.CreateDateTime = DateTime.Now; 95 | workbook.SummaryInformation = si; 96 | } 97 | 98 | #endregion 99 | 100 | #region 设置标题样式 101 | 102 | var headStyle = workbook.CreateCellStyle(); 103 | var arrColWidth = new int[properties.Length]; 104 | var arrColName = new string[properties.Length]; //列名 105 | var arryColumStyle = new ICellStyle[properties.Length]; //样式表 106 | headStyle.Alignment = HorizontalAlignment.Center; // ------------------ 107 | if (excelConfig.Background != new Color()) 108 | if (excelConfig.Background != new Color()) 109 | { 110 | headStyle.FillPattern = FillPattern.SolidForeground; 111 | headStyle.FillForegroundColor = GetXLColour(workbook, excelConfig.Background); 112 | } 113 | 114 | var font = workbook.CreateFont(); 115 | font.FontHeightInPoints = excelConfig.TitlePoint; 116 | if (excelConfig.ForeColor != new Color()) font.Color = GetXLColour(workbook, excelConfig.ForeColor); 117 | font.Boldweight = 700; 118 | headStyle.SetFont(font); 119 | 120 | #endregion 121 | 122 | #region 列头及样式 123 | 124 | var cHeadStyle = workbook.CreateCellStyle(); 125 | cHeadStyle.Alignment = HorizontalAlignment.Center; // ------------------ 126 | var cfont = workbook.CreateFont(); 127 | cfont.FontHeightInPoints = excelConfig.HeadPoint; 128 | cHeadStyle.SetFont(cfont); 129 | 130 | #endregion 131 | 132 | #region 设置内容单元格样式 133 | 134 | var i = 0; 135 | foreach (var column in properties) 136 | { 137 | var columnStyle = workbook.CreateCellStyle(); 138 | columnStyle.Alignment = HorizontalAlignment.Center; 139 | arrColWidth[i] = Encoding.GetEncoding(936).GetBytes(column.Name).Length; 140 | arrColName[i] = column.Name; 141 | 142 | if (excelConfig.ColumnEntity != null) 143 | { 144 | var columnentity = excelConfig.ColumnEntity.Find(t => t.Column == column.Name); 145 | if (columnentity != null) 146 | { 147 | arrColName[i] = columnentity.ExcelColumn; 148 | if (columnentity.Width != 0) arrColWidth[i] = columnentity.Width; 149 | if (columnentity.Background != new Color()) 150 | if (columnentity.Background != new Color()) 151 | { 152 | columnStyle.FillPattern = FillPattern.SolidForeground; 153 | columnStyle.FillForegroundColor = GetXLColour(workbook, columnentity.Background); 154 | } 155 | 156 | if (columnentity.Font != null || columnentity.Point != 0 || 157 | columnentity.ForeColor != new Color()) 158 | { 159 | var columnFont = workbook.CreateFont(); 160 | columnFont.FontHeightInPoints = 10; 161 | if (columnentity.Font != null) columnFont.FontName = columnentity.Font; 162 | if (columnentity.Point != 0) columnFont.FontHeightInPoints = columnentity.Point; 163 | if (columnentity.ForeColor != new Color()) 164 | columnFont.Color = GetXLColour(workbook, columnentity.ForeColor); 165 | columnStyle.SetFont(font); 166 | } 167 | } 168 | } 169 | 170 | arryColumStyle[i] = columnStyle; 171 | i++; 172 | } 173 | 174 | #endregion 175 | 176 | #region 填充数据 177 | 178 | #endregion 179 | 180 | var dateStyle = workbook.CreateCellStyle(); 181 | var format = workbook.CreateDataFormat(); 182 | dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); 183 | var rowIndex = 0; 184 | foreach (var item in lists) 185 | { 186 | #region 新建表,填充表头,填充列头,样式 187 | 188 | if (rowIndex == 65535 || rowIndex == 0) 189 | { 190 | if (rowIndex != 0) sheet = workbook.CreateSheet(); 191 | 192 | #region 表头及样式 193 | 194 | { 195 | if (excelConfig.Title != null) 196 | { 197 | var headerRow = sheet.CreateRow(0); 198 | if (excelConfig.TitleHeight != 0) headerRow.Height = (short) (excelConfig.TitleHeight * 20); 199 | headerRow.HeightInPoints = 25; 200 | headerRow.CreateCell(0).SetCellValue(excelConfig.Title); 201 | headerRow.GetCell(0).CellStyle = headStyle; 202 | sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, lists.Count - 1)); // ------------------ 203 | } 204 | } 205 | 206 | #endregion 207 | 208 | #region 列头及样式 209 | 210 | { 211 | var headerRow = sheet.CreateRow(1); 212 | 213 | #region 如果设置了列标题就按列标题定义列头,没定义直接按字段名输出 214 | 215 | var headIndex = 0; 216 | foreach (var column in properties) 217 | { 218 | headerRow.CreateCell(headIndex).SetCellValue(arrColName[headIndex]); 219 | headerRow.GetCell(headIndex).CellStyle = cHeadStyle; 220 | //设置列宽 221 | sheet.SetColumnWidth(headIndex, (arrColWidth[headIndex] + 1) * 256); 222 | headIndex++; 223 | } 224 | 225 | #endregion 226 | } 227 | 228 | #endregion 229 | 230 | rowIndex = 2; 231 | } 232 | 233 | #endregion 234 | 235 | #region 填充内容 236 | 237 | var dataRow = sheet.CreateRow(rowIndex); 238 | var ordinal = 0; 239 | foreach (var column in properties) 240 | { 241 | var newCell = dataRow.CreateCell(ordinal); 242 | newCell.CellStyle = arryColumStyle[ordinal]; 243 | var drValue = column.GetValue(item, null) == null ? "" : column.GetValue(item, null).ToString(); 244 | SetCell(newCell, dateStyle, column.PropertyType, drValue); 245 | ordinal++; 246 | } 247 | 248 | #endregion 249 | 250 | rowIndex++; 251 | } 252 | 253 | using (var ms = new MemoryStream()) 254 | { 255 | workbook.Write(ms); 256 | ms.Flush(); 257 | ms.Position = 0; 258 | return ms; 259 | } 260 | } 261 | 262 | #endregion 263 | 264 | #region 设置表格内容 265 | 266 | private static void SetCell(ICell newCell, ICellStyle dateStyle, Type dataType, string drValue) 267 | { 268 | switch (dataType.ToString()) 269 | { 270 | case "System.String": //字符串类型 271 | newCell.SetCellValue(drValue); 272 | break; 273 | case "System.DateTime": //日期类型 274 | DateTime dateV; 275 | if (DateTime.TryParse(drValue, out dateV)) 276 | newCell.SetCellValue(dateV); 277 | else 278 | newCell.SetCellValue(""); 279 | newCell.CellStyle = dateStyle; //格式化显示 280 | break; 281 | case "System.Boolean": //布尔型 282 | var boolV = false; 283 | bool.TryParse(drValue, out boolV); 284 | newCell.SetCellValue(boolV); 285 | break; 286 | case "System.Int16": //整型 287 | case "System.Int32": 288 | case "System.Int64": 289 | case "System.Byte": 290 | var intV = 0; 291 | int.TryParse(drValue, out intV); 292 | newCell.SetCellValue(intV); 293 | break; 294 | case "System.Decimal": //浮点型 295 | case "System.Double": 296 | double doubV = 0; 297 | double.TryParse(drValue, out doubV); 298 | newCell.SetCellValue(doubV); 299 | break; 300 | case "System.DBNull": //空值处理 301 | newCell.SetCellValue(""); 302 | break; 303 | default: 304 | newCell.SetCellValue(""); 305 | break; 306 | } 307 | } 308 | 309 | #endregion 310 | 311 | #region 读取excel ,默认第一行为标头 312 | 313 | /// 314 | /// 导入Excel 315 | /// 316 | /// 317 | /// 中文列名对照 318 | /// Excel所在路径 319 | /// 320 | public List ExcelImport(Hashtable head, string workbookFile) 321 | { 322 | try 323 | { 324 | HSSFWorkbook hssfworkbook; 325 | var lists = new List(); 326 | using (var file = new FileStream(workbookFile, FileMode.Open, FileAccess.Read)) 327 | { 328 | hssfworkbook = new HSSFWorkbook(file); 329 | } 330 | 331 | var sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet; 332 | var rows = sheet.GetRowEnumerator(); 333 | var headerRow = sheet.GetRow(0) as HSSFRow; 334 | int cellCount = headerRow.LastCellNum; 335 | //Type type = typeof(T); 336 | PropertyInfo[] properties; 337 | var t = default(T); 338 | for (var i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) 339 | { 340 | var row = sheet.GetRow(i) as HSSFRow; 341 | t = Activator.CreateInstance(); 342 | properties = t.GetType().GetProperties(); 343 | foreach (var column in properties) 344 | { 345 | var j = headerRow.Cells.FindIndex(delegate(ICell c) 346 | { 347 | return c.StringCellValue == (head[column.Name] == null 348 | ? column.Name 349 | : head[column.Name].ToString()); 350 | }); 351 | if (j >= 0 && row.GetCell(j) != null) 352 | { 353 | var value = valueType(column.PropertyType, row.GetCell(j).ToString()); 354 | column.SetValue(t, value, null); 355 | } 356 | } 357 | 358 | lists.Add(t); 359 | } 360 | 361 | return lists; 362 | } 363 | catch (Exception ee) 364 | { 365 | var see = ee.Message; 366 | return null; 367 | } 368 | } 369 | 370 | #endregion 371 | 372 | #region RGB颜色转NPOI颜色 373 | 374 | private static short GetXLColour(HSSFWorkbook workbook, Color SystemColour) 375 | { 376 | short s = 0; 377 | var XlPalette = workbook.GetCustomPalette(); 378 | var XlColour = XlPalette.FindColor(SystemColour.R, SystemColour.G, SystemColour.B); 379 | if (XlColour == null) 380 | { 381 | if (PaletteRecord.STANDARD_PALETTE_SIZE < 255) 382 | { 383 | XlColour = XlPalette.FindSimilarColor(SystemColour.R, SystemColour.G, SystemColour.B); 384 | s = XlColour.Indexed; 385 | } 386 | } 387 | else 388 | { 389 | s = XlColour.Indexed; 390 | } 391 | 392 | return s; 393 | } 394 | 395 | #endregion 396 | 397 | private object valueType(Type t, string value) 398 | { 399 | object o = null; 400 | var strt = "String"; 401 | if (t.Name == "Nullable`1") strt = t.GetGenericArguments()[0].Name; 402 | switch (strt) 403 | { 404 | case "Decimal": 405 | o = decimal.Parse(value); 406 | break; 407 | case "Int": 408 | o = int.Parse(value); 409 | break; 410 | case "Float": 411 | o = float.Parse(value); 412 | break; 413 | case "DateTime": 414 | o = DateTime.Parse(value); 415 | break; 416 | default: 417 | o = value; 418 | break; 419 | } 420 | 421 | return o; 422 | } 423 | } 424 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Util/Time.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Globalization; 3 | 4 | namespace CSum.Util 5 | { 6 | /// 7 | /// 时间操作 8 | /// 9 | public class Time 10 | { 11 | #region 获取某一年有多少周 12 | 13 | /// 14 | /// 获取某一年有多少周 15 | /// 16 | /// 年份 17 | /// 该年周数 18 | public static int GetWeekAmount(int year) 19 | { 20 | var end = new DateTime(year, 12, 31); //该年最后一天 21 | var gc = new GregorianCalendar(); 22 | return gc.GetWeekOfYear(end, CalendarWeekRule.FirstDay, DayOfWeek.Monday); //该年星期数 23 | } 24 | 25 | #endregion 26 | 27 | #region 获取某一日期是该年中的第几周 28 | 29 | /// 30 | /// 获取某一日期是该年中的第几周 31 | /// 32 | /// 日期 33 | /// 该日期在该年中的周数 34 | public static int GetWeekOfYear(DateTime dt) 35 | { 36 | var gc = new GregorianCalendar(); 37 | return gc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday); 38 | } 39 | 40 | #endregion 41 | 42 | #region 根据某年的第几周获取这周的起止日期 43 | 44 | /// 45 | /// 根据某年的第几周获取这周的起止日期 46 | /// 47 | /// 48 | /// 49 | /// 50 | /// 51 | /// 52 | public static void WeekRange(int year, int weekOrder, ref DateTime firstDate, ref DateTime lastDate) 53 | { 54 | //当年的第一天 55 | var firstDay = new DateTime(year, 1, 1); 56 | 57 | //当年的第一天是星期几 58 | var firstOfWeek = Convert.ToInt32(firstDay.DayOfWeek); 59 | 60 | //计算当年第一周的起止日期,可能跨年 61 | var dayDiff = -1 * firstOfWeek + 1; 62 | var dayAdd = 7 - firstOfWeek; 63 | 64 | firstDate = firstDay.AddDays(dayDiff).Date; 65 | lastDate = firstDay.AddDays(dayAdd).Date; 66 | 67 | //如果不是要求计算第一周 68 | if (weekOrder != 1) 69 | { 70 | var addDays = (weekOrder - 1) * 7; 71 | firstDate = firstDate.AddDays(addDays); 72 | lastDate = lastDate.AddDays(addDays); 73 | } 74 | } 75 | 76 | #endregion 77 | 78 | #region 返回两个日期之间相差的天数 79 | 80 | /// 81 | /// 返回两个日期之间相差的天数 82 | /// 83 | /// 两个日期参数 84 | /// 两个日期参数 85 | /// 天数 86 | public static int DiffDays(DateTime dtfrm, DateTime dtto) 87 | { 88 | var tsDiffer = dtto.Date - dtfrm.Date; 89 | return tsDiffer.Days; 90 | } 91 | 92 | #endregion 93 | 94 | #region 返回两个日期之间相差的小时数 95 | 96 | /// 97 | /// 返回两个日期之间相差的小时数 98 | /// 99 | /// 两个日期参数 100 | /// 两个日期参数 101 | /// 小时数 102 | public static int DiffHours(DateTime dtfrm, DateTime dtto) 103 | { 104 | var tsDiffer = dtto.Date - dtfrm.Date; 105 | return tsDiffer.TotalHours.ToInt(); 106 | } 107 | 108 | #endregion 109 | 110 | #region 返回两个日期之间相差的分钟数 111 | 112 | /// 113 | /// 返回两个日期之间相差的分钟数 114 | /// 115 | /// 两个日期参数 116 | /// 两个日期参数 117 | /// 分钟数 118 | public static int DiffMinutes(DateTime dtfrm, DateTime dtto) 119 | { 120 | var tsDiffer = dtto.Date - dtfrm.Date; 121 | return tsDiffer.TotalMinutes.ToInt(); 122 | } 123 | 124 | #endregion 125 | 126 | #region 判断当前年份是否是闰年 127 | 128 | /// 判断当前年份是否是闰年,私有函数 129 | /// 年份 130 | /// 是闰年:True ,不是闰年:False 131 | private static bool IsRuYear(int iYear) 132 | { 133 | //形式参数为年份 134 | //例如:2003 135 | var n = iYear; 136 | return n % 400 == 0 || n % 4 == 0 && n % 100 != 0; 137 | } 138 | 139 | #endregion 140 | 141 | #region 将输入的字符串转化为日期。如果字符串的格式非法,则返回当前日期 142 | 143 | /// 144 | /// 将输入的字符串转化为日期。如果字符串的格式非法,则返回当前日期。 145 | /// 146 | /// 输入字符串 147 | /// 日期对象 148 | public static DateTime ToDate(string strInput) 149 | { 150 | DateTime oDateTime; 151 | 152 | try 153 | { 154 | oDateTime = DateTime.Parse(strInput); 155 | } 156 | catch (Exception) 157 | { 158 | oDateTime = DateTime.Today; 159 | } 160 | 161 | return oDateTime; 162 | } 163 | 164 | #endregion 165 | 166 | #region 将日期对象转化为格式字符串 167 | 168 | /// 169 | /// 将日期对象转化为格式字符串 170 | /// 171 | /// 日期对象 172 | /// 173 | /// 格式: 174 | /// "SHORTDATE"===短日期 175 | /// "LONGDATE"==长日期 176 | /// 其它====自定义格式 177 | /// 178 | /// 日期字符串 179 | public static string ToString(DateTime oDateTime, string strFormat) 180 | { 181 | string strDate; 182 | 183 | try 184 | { 185 | switch (strFormat.ToUpper()) 186 | { 187 | case "SHORTDATE": 188 | strDate = oDateTime.ToShortDateString(); 189 | break; 190 | case "LONGDATE": 191 | strDate = oDateTime.ToLongDateString(); 192 | break; 193 | default: 194 | strDate = oDateTime.ToString(strFormat); 195 | break; 196 | } 197 | } 198 | catch (Exception) 199 | { 200 | strDate = oDateTime.ToShortDateString(); 201 | } 202 | 203 | return strDate; 204 | } 205 | 206 | #endregion 207 | 208 | public static int GetNumberWeekDay(DateTime dt) 209 | { 210 | return dt.DayOfWeek.ToInt() + 1; 211 | } 212 | 213 | public static string GetChineseWeekDay(int y, int m, int d) 214 | { 215 | string[] weekstr = {"日", "一", "二", "三", "四", "五", "六"}; 216 | if (m < 3) 217 | { 218 | m += 12; 219 | y--; 220 | } 221 | 222 | if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0) 223 | d--; 224 | else 225 | d += 1; 226 | return "星期" + weekstr[(d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7]; 227 | } 228 | 229 | public static string GetChineseDateTime(DateTime dt) 230 | { 231 | var dateTime = dt.ToString("yyyy-MM-dd"); 232 | dateTime = dateTime + string.Format("({0})", GetChineseWeekDay(dt.Year, dt.Month, dt.Day)); 233 | var time = string.Format("{0: tt hh:mm:ss }", dt); 234 | time = time.Replace("am", "上午").Replace("pm", "下午"); 235 | dateTime = dateTime + time; 236 | return dateTime; 237 | } 238 | 239 | #region 当前时间 240 | 241 | /// 242 | /// 当前时间 243 | /// 244 | public static DateTime Now => DateTime.Now; 245 | 246 | /// 247 | /// 短日期格式(yyyy/MM/dd) 248 | /// 249 | public static string ShortDate => Now.ToString("yyyy/MM/dd"); 250 | 251 | /// 252 | /// 长日期格式(yyyy月MM日dd日) 253 | /// 254 | public static string LongDate => Now.ToString("yyyy月MM日dd日"); 255 | 256 | /// 257 | /// 日期时间(yyyy/MM/dd HH:mm) 258 | /// 259 | public static string ShortDateTime => Now.ToString("yyyy/MM/dd HH:mm"); 260 | 261 | /// 262 | /// 日期时间(yyyy年MM月dd日 HH时mm分) 263 | /// 264 | public static string LongDateTime => Now.ToString("yyyy年MM月dd日 HH时mm分"); 265 | 266 | /// 267 | /// 日期时间(yyyy年MM月dd日 HH时mm分) 268 | /// 269 | public static string LongTime => Now.ToString("HH时mm分"); 270 | 271 | /// 272 | /// 日期时间(yyyy年MM月dd日 HH时mm分) 273 | /// 274 | public static string ShortTime => Now.ToString("HH:mm"); 275 | 276 | /// 277 | /// 当前日期 278 | /// 279 | /// 280 | public static string GetToday() 281 | { 282 | return DateTime.Now.ToString("yyyy-MM-dd"); 283 | } 284 | 285 | /// 286 | /// 当前日期自定义格式 287 | /// 288 | /// 289 | /// 290 | public static string GetToday(string format) 291 | { 292 | return DateTime.Now.ToString(format); 293 | } 294 | 295 | /// 296 | /// 当前日期 加添加,减天数 -1、1 297 | /// 298 | /// 299 | /// 300 | public static string GetDate(int i) 301 | { 302 | var dt = DateTime.Now; 303 | dt = dt.AddDays(i); 304 | return dt.ToString("yyyy-MM-dd"); 305 | } 306 | 307 | /// 308 | /// 当前日期 加添加,减天数 -1、1 309 | /// 310 | /// 311 | /// 返回的时间格式 312 | /// 313 | public static string GetDate(int i, string dateformat) 314 | { 315 | var dt = DateTime.Now; 316 | dt = dt.AddDays(i); 317 | return dt.ToString(dateformat); 318 | } 319 | 320 | #endregion 321 | 322 | #region 返回本年有多少天 323 | 324 | /// 返回本年有多少天 325 | /// 年份 326 | /// 本年的天数 327 | public static int GetDaysOfYear(int iYear) 328 | { 329 | return IsRuYear(iYear) ? 366 : 365; 330 | } 331 | 332 | /// 本年有多少天 333 | /// 日期 334 | /// 本天在当年的天数 335 | public static int GetDaysOfYear(DateTime dt) 336 | { 337 | return IsRuYear(dt.Year) ? 366 : 365; 338 | } 339 | 340 | #endregion 341 | 342 | #region 返回本月有多少天 343 | 344 | /// 本月有多少天 345 | /// 年 346 | /// 月 347 | /// 天数 348 | public static int GetDaysOfMonth(int iYear, int Month) 349 | { 350 | var days = 0; 351 | switch (Month) 352 | { 353 | case 1: 354 | days = 31; 355 | break; 356 | case 2: 357 | days = IsRuYear(iYear) ? 29 : 28; 358 | break; 359 | case 3: 360 | days = 31; 361 | break; 362 | case 4: 363 | days = 30; 364 | break; 365 | case 5: 366 | days = 31; 367 | break; 368 | case 6: 369 | days = 30; 370 | break; 371 | case 7: 372 | days = 31; 373 | break; 374 | case 8: 375 | days = 31; 376 | break; 377 | case 9: 378 | days = 30; 379 | break; 380 | case 10: 381 | days = 31; 382 | break; 383 | case 11: 384 | days = 30; 385 | break; 386 | case 12: 387 | days = 31; 388 | break; 389 | } 390 | 391 | return days; 392 | } 393 | 394 | 395 | /// 本月有多少天 396 | /// 日期 397 | /// 天数 398 | public static int GetDaysOfMonth(DateTime dt) 399 | { 400 | //--------------------------------// 401 | //--从dt中取得当前的年,月信息 --// 402 | //--------------------------------// 403 | var days = 0; 404 | var year = dt.Year; 405 | var month = dt.Month; 406 | 407 | //--利用年月信息,得到当前月的天数信息。 408 | switch (month) 409 | { 410 | case 1: 411 | days = 31; 412 | break; 413 | case 2: 414 | days = IsRuYear(year) ? 29 : 28; 415 | break; 416 | case 3: 417 | days = 31; 418 | break; 419 | case 4: 420 | days = 30; 421 | break; 422 | case 5: 423 | days = 31; 424 | break; 425 | case 6: 426 | days = 30; 427 | break; 428 | case 7: 429 | days = 31; 430 | break; 431 | case 8: 432 | days = 31; 433 | break; 434 | case 9: 435 | days = 30; 436 | break; 437 | case 10: 438 | days = 31; 439 | break; 440 | case 11: 441 | days = 30; 442 | break; 443 | case 12: 444 | days = 31; 445 | break; 446 | } 447 | 448 | return days; 449 | } 450 | 451 | #endregion 452 | 453 | #region 返回当前日期的 (星期名称or星期编号) 454 | 455 | /// 返回当前日期的星期名称 456 | /// 日期 457 | /// 星期名称 458 | public static string GetWeekNameOfDay(DateTime dt) 459 | { 460 | var week = string.Empty; 461 | switch (dt.DayOfWeek) 462 | { 463 | case DayOfWeek.Monday: 464 | week = "星期一"; 465 | break; 466 | case DayOfWeek.Tuesday: 467 | week = "星期二"; 468 | break; 469 | case DayOfWeek.Wednesday: 470 | week = "星期三"; 471 | break; 472 | case DayOfWeek.Thursday: 473 | week = "星期四"; 474 | break; 475 | case DayOfWeek.Friday: 476 | week = "星期五"; 477 | break; 478 | case DayOfWeek.Saturday: 479 | week = "星期六"; 480 | break; 481 | case DayOfWeek.Sunday: 482 | week = "星期日"; 483 | break; 484 | } 485 | 486 | return week; 487 | } 488 | 489 | 490 | /// 返回当前日期的星期编号 491 | /// 日期 492 | /// 星期数字编号 493 | public static int GetWeekNumberOfDay(DateTime dt) 494 | { 495 | var week = 0; 496 | switch (dt.DayOfWeek) 497 | { 498 | case DayOfWeek.Monday: 499 | week = 1; 500 | break; 501 | case DayOfWeek.Tuesday: 502 | week = 2; 503 | break; 504 | case DayOfWeek.Wednesday: 505 | week = 3; 506 | break; 507 | case DayOfWeek.Thursday: 508 | week = 4; 509 | break; 510 | case DayOfWeek.Friday: 511 | week = 5; 512 | break; 513 | case DayOfWeek.Saturday: 514 | week = 6; 515 | break; 516 | case DayOfWeek.Sunday: 517 | week = 7; 518 | break; 519 | } 520 | 521 | return week; 522 | } 523 | 524 | #endregion 525 | 526 | #region 时间对象与时间戳相互转换 527 | 528 | /// 529 | /// 时间戳转为C#格式时间 530 | /// 531 | /// 精确到秒的时间戳值 532 | /// 533 | public static DateTime StampToDateTime(string timeStamp) 534 | { 535 | var dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 536 | var lTime = long.Parse(timeStamp + "0000000"); 537 | var toNow = new TimeSpan(lTime); 538 | return dateTimeStart.Add(toNow); 539 | } 540 | 541 | /// 542 | /// DateTime时间格式转换为Unix时间戳格式 543 | /// 544 | /// 545 | /// 确到秒的时间戳值 546 | public static long DateTimeToStamp(DateTime time) 547 | { 548 | var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 549 | return (long) (time - startTime).TotalSeconds; 550 | } 551 | 552 | #endregion 553 | } 554 | } -------------------------------------------------------------------------------- /CSum.FK/CSum.Offices/Excel/ExcelHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data; 4 | using System.Drawing; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Web; 9 | using NPOI.HPSF; 10 | using NPOI.HSSF.Record; 11 | using NPOI.HSSF.UserModel; 12 | using NPOI.SS.UserModel; 13 | using NPOI.SS.Util; 14 | using NPOI.XSSF.UserModel; 15 | 16 | namespace CSum.Offices.Excel 17 | { 18 | /// 19 | /// 描 述:NPOI Excel DataTable操作类 20 | public class ExcelHelper 21 | { 22 | #region DataTable导出到Excel文件excelConfig中FileName设置为全路径 23 | 24 | /// 25 | /// DataTable导出到Excel文件 Export() 26 | /// 27 | /// DataTable数据源 28 | /// 导出设置包含文件名、标题、列设置 29 | public static void ExcelExport(DataTable dtSource, ExcelConfig excelConfig) 30 | { 31 | using (var ms = ExportMemoryStream(dtSource, excelConfig)) 32 | { 33 | using (var fs = new FileStream(excelConfig.FileName, FileMode.Create, FileAccess.Write)) 34 | { 35 | var data = ms.ToArray(); 36 | fs.Write(data, 0, data.Length); 37 | fs.Flush(); 38 | } 39 | } 40 | } 41 | 42 | #endregion 43 | 44 | #region DataTable导出到Excel的MemoryStream 45 | 46 | /// 47 | /// DataTable导出到Excel的MemoryStream Export() 48 | /// 49 | /// DataTable数据源 50 | /// 导出设置包含文件名、标题、列设置 51 | public static MemoryStream ExportMemoryStream(DataTable dtSource, ExcelConfig excelConfig) 52 | { 53 | /*var dt = dtSource.Clone(); 54 | int len = dt.Columns.Count; 55 | for (int i = 0; i < len;i++) 56 | { 57 | DataColumn column = dt.Columns[i]; 58 | if (excelConfig.ColumnEntity.Find(p => p.Column == column.ColumnName) == null) 59 | { 60 | dtSource.Columns.Remove(column.ColumnName); 61 | } 62 | }*/ 63 | for (var i = 0; i < excelConfig.ColumnEntity.Count; i++) 64 | { 65 | var modelConfig = excelConfig.ColumnEntity[i]; 66 | if (!dtSource.Columns.Contains(modelConfig.Column)) //如果源数据不包含该列 67 | dtSource.Columns.Add(modelConfig.Column, typeof(string), ""); 68 | } 69 | 70 | var arrCol = excelConfig.ColumnEntity.Select(p => p.Column).ToArray(); 71 | dtSource = dtSource.DefaultView.ToTable(true, arrCol); 72 | //筛选出配置和数据源都存在的列 73 | var eColumns = new List(); 74 | foreach (var item in excelConfig.ColumnEntity) 75 | if (dtSource.Columns[item.Column] != null) 76 | eColumns.Add(item); 77 | 78 | var workbook = new HSSFWorkbook(); 79 | var sheet = workbook.CreateSheet(); 80 | 81 | #region 右击文件 属性信息 82 | 83 | { 84 | var dsi = PropertySetFactory.CreateDocumentSummaryInformation(); 85 | dsi.Company = "NPOI"; 86 | workbook.DocumentSummaryInformation = dsi; 87 | 88 | var si = PropertySetFactory.CreateSummaryInformation(); 89 | si.Author = "system"; //填加xls文件作者信息 90 | si.ApplicationName = "中夏信科"; //填加xls文件创建程序信息 91 | si.LastAuthor = "system"; //填加xls文件最后保存者信息 92 | si.Comments = "system"; //填加xls文件作者信息 93 | si.Title = "标题信息"; //填加xls文件标题信息 94 | si.Subject = "主题信息"; //填加文件主题信息 95 | si.CreateDateTime = DateTime.Now; 96 | workbook.SummaryInformation = si; 97 | } 98 | 99 | #endregion 100 | 101 | #region 设置标题样式 102 | 103 | var headStyle = workbook.CreateCellStyle(); 104 | var arrColWidth = new int[dtSource.Columns.Count]; 105 | var arrColName = new string[dtSource.Columns.Count]; //列名 106 | var arryColumStyle = new ICellStyle[dtSource.Columns.Count]; //样式表 107 | headStyle.Alignment = HorizontalAlignment.Center; // ------------------ 108 | if (excelConfig.Background != new Color()) 109 | if (excelConfig.Background != new Color()) 110 | { 111 | headStyle.FillPattern = FillPattern.SolidForeground; 112 | headStyle.FillForegroundColor = GetXLColour(workbook, excelConfig.Background); 113 | } 114 | 115 | var font = workbook.CreateFont(); 116 | font.FontHeightInPoints = excelConfig.TitlePoint; 117 | if (excelConfig.ForeColor != new Color()) font.Color = GetXLColour(workbook, excelConfig.ForeColor); 118 | font.Boldweight = 700; 119 | headStyle.SetFont(font); 120 | 121 | #endregion 122 | 123 | #region 列头及样式 124 | 125 | var cHeadStyle = workbook.CreateCellStyle(); 126 | cHeadStyle.Alignment = HorizontalAlignment.Center; // ------------------ 127 | var cfont = workbook.CreateFont(); 128 | cfont.FontHeightInPoints = excelConfig.HeadPoint; 129 | cHeadStyle.SetFont(cfont); 130 | 131 | #endregion 132 | 133 | #region 设置内容单元格样式 134 | 135 | foreach (var eColumn in eColumns) 136 | { 137 | var item = dtSource.Columns[eColumn.Column]; 138 | var columnStyle = workbook.CreateCellStyle(); 139 | columnStyle.Alignment = HorizontalAlignment.Center; 140 | arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName).Length; 141 | arrColName[item.Ordinal] = item.ColumnName; 142 | if (excelConfig.ColumnEntity != null) 143 | { 144 | var columnentity = excelConfig.ColumnEntity.Find(t => t.Column == item.ColumnName); 145 | if (columnentity != null) 146 | { 147 | arrColName[item.Ordinal] = columnentity.ExcelColumn; 148 | if (columnentity.Width != 0) arrColWidth[item.Ordinal] = columnentity.Width; 149 | if (columnentity.Background != new Color()) 150 | if (columnentity.Background != new Color()) 151 | { 152 | columnStyle.FillPattern = FillPattern.SolidForeground; 153 | columnStyle.FillForegroundColor = GetXLColour(workbook, columnentity.Background); 154 | } 155 | 156 | if (columnentity.Font != null || columnentity.Point != 0 || 157 | columnentity.ForeColor != new Color()) 158 | { 159 | var columnFont = workbook.CreateFont(); 160 | columnFont.FontHeightInPoints = 10; 161 | if (columnentity.Font != null) columnFont.FontName = columnentity.Font; 162 | if (columnentity.Point != 0) columnFont.FontHeightInPoints = columnentity.Point; 163 | if (columnentity.ForeColor != new Color()) 164 | columnFont.Color = GetXLColour(workbook, columnentity.ForeColor); 165 | columnStyle.SetFont(font); 166 | } 167 | 168 | columnStyle.Alignment = getAlignment(columnentity.Alignment); 169 | } 170 | } 171 | 172 | arryColumStyle[item.Ordinal] = columnStyle; 173 | } 174 | 175 | //foreach (DataColumn item in dtSource.Columns) 176 | //{ 177 | // ICellStyle columnStyle = workbook.CreateCellStyle(); 178 | // columnStyle.Alignment = HorizontalAlignment.Center; 179 | // arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; 180 | // arrColName[item.Ordinal] = item.ColumnName.ToString(); 181 | // if (excelConfig.ColumnEntity != null) 182 | // { 183 | // ColumnEntity columnentity = excelConfig.ColumnEntity.Find(t => t.Column == item.ColumnName); 184 | // if (columnentity != null) 185 | // { 186 | // arrColName[item.Ordinal] = columnentity.ExcelColumn; 187 | // if (columnentity.Width != 0) 188 | // { 189 | // arrColWidth[item.Ordinal] = columnentity.Width; 190 | // } 191 | // if (columnentity.Background != new Color()) 192 | // { 193 | // if (columnentity.Background != new Color()) 194 | // { 195 | // columnStyle.FillPattern = FillPattern.SolidForeground; 196 | // columnStyle.FillForegroundColor = GetXLColour(workbook, columnentity.Background); 197 | // } 198 | // } 199 | // if (columnentity.Font != null || columnentity.Point != 0 || columnentity.ForeColor != new Color()) 200 | // { 201 | // IFont columnFont = workbook.CreateFont(); 202 | // columnFont.FontHeightInPoints = 10; 203 | // if (columnentity.Font != null) 204 | // { 205 | // columnFont.FontName = columnentity.Font; 206 | // } 207 | // if (columnentity.Point != 0) 208 | // { 209 | // columnFont.FontHeightInPoints = columnentity.Point; 210 | // } 211 | // if (columnentity.ForeColor != new Color()) 212 | // { 213 | // columnFont.Color = GetXLColour(workbook, columnentity.ForeColor); 214 | // } 215 | // columnStyle.SetFont(font); 216 | // } 217 | // columnStyle.Alignment = getAlignment(columnentity.Alignment); 218 | // } 219 | // } 220 | // arryColumStyle[item.Ordinal] = columnStyle; 221 | //} 222 | if (excelConfig.IsAllSizeColumn) 223 | { 224 | #region 根据列中最长列的长度取得列宽 225 | 226 | for (var i = 0; i < dtSource.Rows.Count; i++) 227 | for (var j = 0; j < dtSource.Columns.Count; j++) 228 | if (arrColWidth[j] != 0) 229 | { 230 | var intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length; 231 | if (intTemp > arrColWidth[j]) arrColWidth[j] = intTemp; 232 | } 233 | 234 | #endregion 235 | } 236 | 237 | #endregion 238 | 239 | var rowIndex = 0; 240 | 241 | #region 表头及样式 242 | 243 | if (excelConfig.Title != null) 244 | { 245 | var headerRow = sheet.CreateRow(rowIndex); 246 | rowIndex++; 247 | if (excelConfig.TitleHeight != 0) headerRow.Height = (short) (excelConfig.TitleHeight * 20); 248 | headerRow.HeightInPoints = 25; 249 | headerRow.CreateCell(0).SetCellValue(excelConfig.Title); 250 | headerRow.GetCell(0).CellStyle = headStyle; 251 | sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1)); // ------------------ 252 | } 253 | 254 | #endregion 255 | 256 | #region 列头及样式 257 | 258 | { 259 | var headerRow = sheet.CreateRow(rowIndex); 260 | rowIndex++; 261 | 262 | #region 如果设置了列标题就按列标题定义列头,没定义直接按字段名输出 263 | 264 | for (var i = 0; i < eColumns.Count; i++) 265 | { 266 | var eColumn = eColumns[i]; 267 | var column = dtSource.Columns[eColumn.Column]; 268 | headerRow.CreateCell(i).SetCellValue(arrColName[column.Ordinal]); 269 | headerRow.GetCell(i).CellStyle = cHeadStyle; 270 | //设置列宽 271 | sheet.SetColumnWidth(i, (arrColWidth[column.Ordinal] + 1) * 256); 272 | } 273 | 274 | //foreach (DataColumn column in dtSource.Columns) 275 | //{ 276 | // headerRow.CreateCell(column.Ordinal).SetCellValue(arrColName[column.Ordinal]); 277 | // headerRow.GetCell(column.Ordinal).CellStyle = cHeadStyle; 278 | // //设置列宽 279 | // sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); 280 | //} 281 | 282 | #endregion 283 | } 284 | 285 | #endregion 286 | 287 | var dateStyle = workbook.CreateCellStyle(); 288 | var format = workbook.CreateDataFormat(); 289 | dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd"); 290 | 291 | foreach (DataRow row in dtSource.Rows) 292 | { 293 | #region 新建表,填充表头,填充列头,样式 294 | 295 | if (rowIndex == 65535) 296 | { 297 | sheet = workbook.CreateSheet(); 298 | rowIndex = 0; 299 | 300 | #region 表头及样式 301 | 302 | { 303 | if (excelConfig.Title != null) 304 | { 305 | var headerRow = sheet.CreateRow(rowIndex); 306 | rowIndex++; 307 | if (excelConfig.TitleHeight != 0) headerRow.Height = (short) (excelConfig.TitleHeight * 20); 308 | headerRow.HeightInPoints = 25; 309 | headerRow.CreateCell(0).SetCellValue(excelConfig.Title); 310 | headerRow.GetCell(0).CellStyle = headStyle; 311 | sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 312 | dtSource.Columns.Count - 1)); // ------------------ 313 | } 314 | } 315 | 316 | #endregion 317 | 318 | #region 列头及样式 319 | 320 | { 321 | var headerRow = sheet.CreateRow(rowIndex); 322 | rowIndex++; 323 | 324 | #region 如果设置了列标题就按列标题定义列头,没定义直接按字段名输出 325 | 326 | for (var i = 0; i < eColumns.Count; i++) 327 | { 328 | var eColumn = eColumns[i]; 329 | var column = dtSource.Columns[eColumn.Column]; 330 | headerRow.CreateCell(i).SetCellValue(arrColName[column.Ordinal]); 331 | headerRow.GetCell(i).CellStyle = cHeadStyle; 332 | //设置列宽 333 | sheet.SetColumnWidth(i, (arrColWidth[column.Ordinal] + 1) * 256); 334 | } 335 | 336 | //foreach (DataColumn column in dtSource.Columns) 337 | //{ 338 | // headerRow.CreateCell(column.Ordinal).SetCellValue(arrColName[column.Ordinal]); 339 | // headerRow.GetCell(column.Ordinal).CellStyle = cHeadStyle; 340 | // //设置列宽 341 | // sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); 342 | //} 343 | 344 | #endregion 345 | } 346 | 347 | #endregion 348 | } 349 | 350 | #endregion 351 | 352 | #region 填充内容 353 | 354 | var dataRow = sheet.CreateRow(rowIndex); 355 | for (var i = 0; i < eColumns.Count; i++) 356 | { 357 | var eColumn = eColumns[i]; 358 | var column = dtSource.Columns[eColumn.Column]; 359 | var newCell = dataRow.CreateCell(i); 360 | newCell.CellStyle = arryColumStyle[column.Ordinal]; 361 | var drValue = row[column] == null ? "" : row[column].ToString(); 362 | ; 363 | SetCell(newCell, dateStyle, column.DataType, drValue); 364 | } 365 | 366 | //foreach (DataColumn column in dtSource.Columns) 367 | //{ 368 | // ICell newCell = dataRow.CreateCell(column.Ordinal); 369 | // newCell.CellStyle = arryColumStyle[column.Ordinal]; 370 | // string drValue = row[column].ToString(); 371 | // SetCell(newCell, dateStyle, column.DataType, drValue); 372 | //} 373 | 374 | #endregion 375 | 376 | rowIndex++; 377 | } 378 | 379 | //锁定标题行和表头行 380 | sheet.CreateFreezePane(0, 2); 381 | 382 | var ms = new MemoryStream(); 383 | workbook.Write(ms); 384 | ms.Flush(); 385 | ms.Position = 0; 386 | return ms; 387 | } 388 | 389 | #endregion 390 | 391 | #region 设置表格内容 392 | 393 | private static void SetCell(ICell newCell, ICellStyle dateStyle, Type dataType, string drValue) 394 | { 395 | switch (dataType.ToString()) 396 | { 397 | case "System.String": //字符串类型 398 | newCell.SetCellValue(drValue); 399 | break; 400 | case "System.DateTime": //日期类型 401 | DateTime dateV; 402 | if (DateTime.TryParse(drValue, out dateV)) 403 | newCell.SetCellValue(dateV); 404 | else 405 | newCell.SetCellValue(""); 406 | newCell.CellStyle = dateStyle; //格式化显示 407 | break; 408 | case "System.Boolean": //布尔型 409 | var boolV = false; 410 | bool.TryParse(drValue, out boolV); 411 | newCell.SetCellValue(boolV); 412 | break; 413 | case "System.Int16": //整型 414 | case "System.Int32": 415 | case "System.Int64": 416 | case "System.Byte": 417 | var intV = 0; 418 | int.TryParse(drValue, out intV); 419 | newCell.SetCellValue(intV); 420 | break; 421 | case "System.Decimal": //浮点型 422 | case "System.Double": 423 | double doubV = 0; 424 | double.TryParse(drValue, out doubV); 425 | newCell.SetCellValue(doubV); 426 | break; 427 | case "System.DBNull": //空值处理 428 | newCell.SetCellValue(""); 429 | break; 430 | default: 431 | newCell.SetCellValue(""); 432 | break; 433 | } 434 | } 435 | 436 | #endregion 437 | 438 | #region RGB颜色转NPOI颜色 439 | 440 | private static short GetXLColour(HSSFWorkbook workbook, Color SystemColour) 441 | { 442 | short s = 0; 443 | var XlPalette = workbook.GetCustomPalette(); 444 | var XlColour = XlPalette.FindColor(SystemColour.R, SystemColour.G, SystemColour.B); 445 | if (XlColour == null) 446 | { 447 | if (PaletteRecord.STANDARD_PALETTE_SIZE < 255) 448 | { 449 | XlColour = XlPalette.FindSimilarColor(SystemColour.R, SystemColour.G, SystemColour.B); 450 | s = XlColour.Indexed; 451 | } 452 | } 453 | else 454 | { 455 | s = XlColour.Indexed; 456 | } 457 | 458 | return s; 459 | } 460 | 461 | #endregion 462 | 463 | #region 设置列的对齐方式 464 | 465 | /// 466 | /// 设置对齐方式 467 | /// 468 | /// 469 | /// 470 | private static HorizontalAlignment getAlignment(string style) 471 | { 472 | switch (style) 473 | { 474 | case "center": 475 | return HorizontalAlignment.Center; 476 | case "left": 477 | return HorizontalAlignment.Left; 478 | case "right": 479 | return HorizontalAlignment.Right; 480 | case "fill": 481 | return HorizontalAlignment.Fill; 482 | case "justify": 483 | return HorizontalAlignment.Justify; 484 | case "centerselection": 485 | return HorizontalAlignment.CenterSelection; 486 | case "distributed": 487 | return HorizontalAlignment.Distributed; 488 | } 489 | 490 | return HorizontalAlignment.General; 491 | } 492 | 493 | #endregion 494 | 495 | #region Excel导出方法 ExcelDownload 496 | 497 | /// 498 | /// Excel导出下载 499 | /// 500 | /// DataTable数据源 501 | /// 导出设置包含文件名、标题、列设置 502 | public static void ExcelDownload(DataTable dtSource, ExcelConfig excelConfig) 503 | { 504 | var curContext = HttpContext.Current; 505 | // 设置编码和附件格式 506 | curContext.Response.ContentType = "application/ms-excel"; 507 | curContext.Response.ContentEncoding = Encoding.UTF8; 508 | curContext.Response.Charset = ""; 509 | curContext.Response.AppendHeader("Content-Disposition", 510 | "attachment;filename=" + HttpUtility.UrlEncode(excelConfig.FileName, Encoding.UTF8)); 511 | //调用导出具体方法Export() 512 | curContext.Response.BinaryWrite(ExportMemoryStream(dtSource, excelConfig).GetBuffer()); 513 | curContext.Response.End(); 514 | } 515 | 516 | /// 517 | /// Excel导出下载 518 | /// 519 | /// 数据源 520 | /// 模板文件名 521 | /// 文件名 522 | public static void ExcelDownload(List list, string templdateName, string newFileName) 523 | { 524 | var response = HttpContext.Current.Response; 525 | response.Clear(); 526 | response.Charset = "UTF-8"; 527 | response.ContentType = "application/vnd-excel"; //"application/vnd.ms-excel"; 528 | HttpContext.Current.Response.AddHeader("Content-Disposition", 529 | string.Format("attachment; filename=" + newFileName)); 530 | HttpContext.Current.Response.BinaryWrite(ExportListByTempale(list, templdateName).ToArray()); 531 | } 532 | 533 | #endregion 534 | 535 | #region ListExcel导出(加载模板) 536 | 537 | /// 538 | /// List根据模板导出ExcelMemoryStream 539 | /// 540 | /// 541 | /// 542 | public static MemoryStream ExportListByTempale(List list, string templdateName) 543 | { 544 | var templatePath = HttpContext.Current.Server.MapPath("/") + "/Resource/ExcelTemplate/"; 545 | var templdateName1 = string.Format("{0}{1}", templatePath, templdateName); 546 | 547 | var fileStream = new FileStream(templdateName1, FileMode.Open, FileAccess.Read); 548 | ISheet sheet = null; 549 | if (templdateName.IndexOf(".xlsx") == -1) //2003 550 | { 551 | var hssfworkbook = new HSSFWorkbook(fileStream); 552 | sheet = hssfworkbook.GetSheetAt(0); 553 | SetPurchaseOrder(sheet, list); 554 | sheet.ForceFormulaRecalculation = true; 555 | using (var ms = new MemoryStream()) 556 | { 557 | hssfworkbook.Write(ms); 558 | ms.Flush(); 559 | return ms; 560 | } 561 | } 562 | 563 | var xssfworkbook = new XSSFWorkbook(fileStream); 564 | sheet = xssfworkbook.GetSheetAt(0); 565 | SetPurchaseOrder(sheet, list); 566 | sheet.ForceFormulaRecalculation = true; 567 | using (var ms = new MemoryStream()) 568 | { 569 | xssfworkbook.Write(ms); 570 | ms.Flush(); 571 | return ms; 572 | } 573 | } 574 | 575 | /// 576 | /// 赋值单元格 577 | /// 578 | /// 579 | /// 580 | private static void SetPurchaseOrder(ISheet sheet, List list) 581 | { 582 | foreach (var item in list) 583 | { 584 | IRow row = null; 585 | ICell cell = null; 586 | row = sheet.GetRow(item.row); 587 | if (row == null) row = sheet.CreateRow(item.row); 588 | cell = row.GetCell(item.cell); 589 | if (cell == null) cell = row.CreateCell(item.cell); 590 | cell.SetCellValue(item.value); 591 | } 592 | } 593 | 594 | #endregion 595 | 596 | #region 从Excel导入 597 | 598 | /// 599 | /// 读取excel ,默认第一行为标头 600 | /// 601 | /// excel文档路径 602 | /// 603 | public static DataTable ExcelImport(string strFileName) 604 | { 605 | var dt = new DataTable(); 606 | 607 | ISheet sheet = null; 608 | using (var file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) 609 | { 610 | if (strFileName.IndexOf(".xlsx") == -1) //2003 611 | { 612 | var hssfworkbook = new HSSFWorkbook(file); 613 | sheet = hssfworkbook.GetSheetAt(0); 614 | } 615 | else //2007 616 | { 617 | var xssfworkbook = new XSSFWorkbook(file); 618 | sheet = xssfworkbook.GetSheetAt(0); 619 | } 620 | } 621 | 622 | var rows = sheet.GetRowEnumerator(); 623 | 624 | var headerRow = sheet.GetRow(0); 625 | int cellCount = headerRow.LastCellNum; 626 | 627 | for (var j = 0; j < cellCount; j++) 628 | { 629 | var cell = headerRow.GetCell(j); 630 | dt.Columns.Add(cell.ToString()); 631 | } 632 | 633 | for (var i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) 634 | { 635 | var row = sheet.GetRow(i); 636 | var dataRow = dt.NewRow(); 637 | 638 | for (int j = row.FirstCellNum; j < cellCount; j++) 639 | if (row.GetCell(j) != null) 640 | dataRow[j] = row.GetCell(j).ToString(); 641 | 642 | dt.Rows.Add(dataRow); 643 | } 644 | 645 | return dt; 646 | } 647 | 648 | /// 649 | /// 读取excel ,默认第一行为标头 650 | /// 651 | /// 文件数据流 652 | /// 653 | public static DataTable ExcelImport(Stream fileStream, string flieType) 654 | { 655 | var dt = new DataTable(); 656 | ISheet sheet = null; 657 | if (flieType == ".xls") 658 | { 659 | var hssfworkbook = new HSSFWorkbook(fileStream); 660 | sheet = hssfworkbook.GetSheetAt(0); 661 | } 662 | else 663 | { 664 | var xssfworkbook = new XSSFWorkbook(fileStream); 665 | sheet = xssfworkbook.GetSheetAt(0); 666 | } 667 | 668 | var rows = sheet.GetRowEnumerator(); 669 | var headerRow = sheet.GetRow(0); 670 | int cellCount = headerRow.LastCellNum; 671 | for (var j = 0; j < cellCount; j++) 672 | { 673 | var cell = headerRow.GetCell(j); 674 | dt.Columns.Add(cell.ToString()); 675 | } 676 | 677 | for (var i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) 678 | { 679 | var row = sheet.GetRow(i); 680 | var dataRow = dt.NewRow(); 681 | 682 | for (int j = row.FirstCellNum; j < cellCount; j++) 683 | if (row.GetCell(j) != null) 684 | dataRow[j] = row.GetCell(j).ToString(); 685 | 686 | dt.Rows.Add(dataRow); 687 | } 688 | 689 | return dt; 690 | } 691 | 692 | /// 693 | /// 读取excel ,默认第一行为标头 694 | /// 读取所有页 695 | /// 696 | /// excel文档路径 697 | /// 698 | public static DataTable ExcelImport(string strFileName, List lstCol, bool allSheets) 699 | { 700 | if (!allSheets) return ExcelImport(strFileName); 701 | var dt = DataTableHelper.CreateTable(lstCol); 702 | 703 | ISheet sheet = null; 704 | var countSheet = 0; 705 | using (var file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) 706 | { 707 | if (strFileName.IndexOf(".xlsx") == -1) //2003 708 | { 709 | var hssfworkbook = new HSSFWorkbook(file); 710 | countSheet = hssfworkbook.NumberOfSheets; 711 | // sheet = hssfworkbook.GetSheetAt(0); 712 | } 713 | else //2007 714 | { 715 | var xssfworkbook = new XSSFWorkbook(file); 716 | countSheet = xssfworkbook.NumberOfSheets; 717 | //sheet = xssfworkbook.GetSheetAt(0); 718 | } 719 | } 720 | 721 | for (var i = 0; i < countSheet; i++) DataTableHelper.AddTableData(dt, ExcelImport(strFileName, i, lstCol)); 722 | return dt; 723 | } 724 | 725 | /// 726 | /// 读取excel ,默认第一行为标头 727 | /// 读取所有页 728 | /// 729 | /// excel文档路径 730 | /// 731 | private static DataTable ExcelImport(string strFileName, int sheetsIndex, List lstCol) 732 | { 733 | var dt = DataTableHelper.CreateTable(lstCol); 734 | 735 | ISheet sheet = null; 736 | using (var file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)) 737 | { 738 | if (strFileName.IndexOf(".xlsx") == -1) //2003 739 | { 740 | var hssfworkbook = new HSSFWorkbook(file); 741 | sheet = hssfworkbook.GetSheetAt(sheetsIndex); 742 | } 743 | else //2007 744 | { 745 | var xssfworkbook = new XSSFWorkbook(file); 746 | sheet = xssfworkbook.GetSheetAt(sheetsIndex); 747 | } 748 | } 749 | 750 | var rows = sheet.GetRowEnumerator(); 751 | 752 | var headerRow = sheet.GetRow(0); 753 | if (headerRow == null) return dt; 754 | int cellCount = headerRow.LastCellNum; 755 | var lstCell = new List(); 756 | for (var j = 0; j < cellCount; j++) 757 | { 758 | var cell = headerRow.GetCell(j); 759 | if (cell == null) continue; 760 | if (lstCol.Contains(cell.ToString())) lstCell.Add(new CellDto {Name = cell.ToString(), Index = j}); 761 | } 762 | 763 | 764 | for (var i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) 765 | { 766 | var row = sheet.GetRow(i); 767 | var dataRow = dt.NewRow(); 768 | 769 | //for (int j = row.FirstCellNum; j < cellCount; j++) 770 | //{ 771 | // if (row.GetCell(j) != null) 772 | // dataRow[j] = row.GetCell(j).ToString(); 773 | //} 774 | foreach (var item in lstCell) 775 | { 776 | var cell = row.GetCell(item.Index); 777 | if (cell != null) 778 | dataRow[item.Name] = cell.ToString(); 779 | } 780 | 781 | dt.Rows.Add(dataRow); 782 | } 783 | 784 | return dt; 785 | } 786 | 787 | #endregion 788 | } 789 | 790 | public class CellDto 791 | { 792 | /// 793 | /// 列名 794 | /// 795 | public string Name { get; set; } 796 | 797 | /// 798 | /// 序号 799 | /// 800 | public int Index { get; set; } 801 | } 802 | } --------------------------------------------------------------------------------