sqlserver查询特定重复列引用Excel可配置设计

224 篇文章 3 订阅
62 篇文章 0 订阅
SELECT ExcelName, ExcelShell,COUNT(0) AS ExcelShellCount FROM Comparative where ExcelName = '专项附加扣除' GROUP BY ExcelName,ExcelShell HAVING COUNT(ExcelShell) > 0  
 select * from Comparative where ExcelName = '专项附加扣除' 

 

表结构:

create table Comparative(
	Id int primary key identity(1,1), --  主键
	ExcelName nvarchar(500) , -- excel 名称全称

	ExcelShell int ,--第几个shell
	ExcelRow int, -- shell 中的第几个行开始对应
	ExcelCell int , -- shell中的几个行中的第几个列
	ExcelEndRow int, --  shell 中的第几个行结束
	ExcelEndCell int , -- shell中的几个行中的第几个列结束
	ExcelRowCellName nvarchar(500), -- shell 中行里面的列的对应名称
	UserTableName nvarchar(500), -- 表名
	TableCell nvarchar(500), -- 对应的表列 
	Remake nvarchar(700) , -- 备注信息
)

--select * from Comparative
--insert into Comparative values('专项附加扣除',1,1,11,0,0,'累计子女教育','CompanyWelfareSalary','AccumulatedChild','暂无')
--insert into Comparative values('专项附加扣除',1,1,12,0,0,'累计继续教育','CompanyWelfareSalary','AccumulatedContinuingEducation','暂无')  
--insert into Comparative values('专项附加扣除',1,1,13,0,0,'累计住房贷款利息','CompanyWelfareSalary','AccumulatedHousingLoans','暂无')  
--insert into Comparative values('专项附加扣除',1,1,14,0,0,'累计住房租金','CompanyWelfareSalary','AccumulatedHousingRent','暂无')  
--insert into Comparative values('专项附加扣除',1,1,15,0,0,'累计赡养老人','CompanyWelfareSalary','AccumulatedSuperElderly','暂无')  
-----上 专项附加扣除  下 保险和减免
--insert into Comparative values('专项附加扣除',1,1,17,0,0,'商业健康保险','CompanyWelfareSalary','CommercialHealthInsurance','暂无')
--insert into Comparative values('专项附加扣除',1,1,18,0,0,'税延养老保险','CompanyWelfareSalary','DeferredPensionInsurance','暂无')  
--insert into Comparative values('专项附加扣除',1,1,19,0,0,'其它 (保险扣除想)','CompanyWelfareSalary','BuckleOtherBase','暂无')  
--insert into Comparative values('专项附加扣除',1,1,20,0,0,'准予扣除的捐赠额','CompanyWelfareSalary','AllowedDeductionDonationsQuota','暂无')  
--insert into Comparative values('专项附加扣除',1,1,21,0,0,'减免税额','CompanyWelfareSalary','ReductionQuota','暂无')  
--insert into Comparative values('专项附加扣除',1,1,22,0,0,'备注信息','CompanyWelfareSalary','Remarks','暂无')  
--CompanyWelfareSalary  五险二金 的表 中 个税和其它扣除

在这里我们可以看到,我们进行将所需要的信息进行存储到表中,例如第1调数据表示的是 :  当前的 excel中 从第1个shell 中第1行row 第11列 结束行和列都是0  标识的读取excel类型是累计子女教育,是需要存放于CompanyWelfareSalary 表 中AccumulatedChild字段的内容值。

对此我们可以进行在c# 中进行以2中方式进行读取不同的信息内容

1.我们要获取当前专项附加扣除有多少个shell 每个shell 中有几列所需要的读取信息

2.获取我们指定需要的列的信息的内容

sqlserver存储过程如下:


-----------------------分隔符
---创建带参存储过程
go
if exists(select * from sysobjects where name='VMode_Select_ExcelShellModel')
drop procedure VMode_Select_ExcelShellModel
go
--创建存储过程
-- =============================================
-- Author:		<milijiangjun>
-- Create date: <2019-09-18>
-- Description:	<sql server>查询 当前类型 的excel 
-- 有几个shell 每个shell 提及几个cell
--- 
-- ============================================= 
create procedure VMode_Select_ExcelShellModel  
@CheckType nvarchar(200)--类别参数
as

/*******************************************
* 存储过程调用方法:
	exec VMode_Select_ExcelShellModel @CheckType = '专项附加扣除'
 *******************************************/ 
begin
 
 SELECT ExcelName, ExcelShell,COUNT(0) AS ExcelShellCount FROM Comparative where ExcelName = @CheckType GROUP BY ExcelName,ExcelShell HAVING COUNT(ExcelShell) > 0  

end
go

-- 调用存储过程 
-----------------------分隔符 

-----------------------分隔符
---创建带参存储过程
go
if exists(select * from sysobjects where name='VMode_Select_ExcelComparative')
drop procedure VMode_Select_ExcelComparative
go
--创建存储过程
-- =============================================
-- Author:		<milijiangjun>
-- Create date: <2019-09-18>
-- Description:	<sql server>查询 当前类型 的excel 
-- 有几个shell 每个shell 提及几个cell
--- 
-- ============================================= 
create procedure VMode_Select_ExcelComparative  

@CheckType nvarchar(200),--excel类别参数 专项附加扣除
@ExcelRowCellName nvarchar(200) --excel名称
as

/*******************************************
* 存储过程调用方法:
	exec VMode_Select_ExcelComparative @CheckType = '专项附加扣除',@ExcelRowCellName = '累计子女教育'
 *******************************************/ 
begin
 
  select * from Comparative where ExcelName = @CheckType and ExcelRowCellName = @ExcelRowCellName

end
go

-- 调用存储过程 
-----------------------分隔符 

c# 实体如下:

    public class ExcelShellComparative {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// Excel 名称
        /// </summary>
        public string ExcelName { get; set; }

        /// <summary>
        /// 第几个shell
        /// </summary>
        public int ExcelShell { get; set; }
        /// <summary>
        ///  shell 中的第几个行开始对应
        /// </summary>
        public int ExcelRow { get; set; }
        /// <summary>
        ///  shell中的几个行中的第几个列
        /// </summary>
        public int ExcelCell { get; set; }
        /// <summary>
        ///  shell 中的第几个行结束
        /// </summary>
        public int ExcelEndRow { get; set; }
        /// <summary>
        /// shell中的几个行中的第几个列结束
        /// </summary>
        public int ExcelEndCell { get; set; }
        /// <summary>
        ///  shell 中行里面的列的对应名称
        /// </summary>
        public string ExcelRowCellName { get; set; }
        /// <summary>
        /// 表名
        /// </summary>
        public string UserTableName { get; set; }
        /// <summary>
        /// 对应的表列
        /// </summary>
        public string TableCell { get; set; }
        /// <summary>
        /// 备注信息
        /// </summary>
        public string Remake { get; set; }
       
    }
    public class ExcelShellMode
    {
        /// <summary>
        /// Excel 名称
        /// </summary>
        public string ExcelName { get; set; }
        /// <summary>
        /// Excel Shell 页数
        /// </summary>
        public int ExcelShell { get; set; }
        /// <summary>
        /// Excel Shell 页码中有几列 要获取的 
        /// </summary>
        public int ExcelShellCount { get; set; }


    }

调用的方法如下:

 #region 上传专项附加扣除信息
        public static Dictionary<string, string> PostMonthSpecialExempt(IEnumerable<HttpPostedFileBase> files, string Time)
        {
            #region 逻辑处理 
            Dictionary<string, string> DicMess = new Dictionary<string, string>();
            DicMess.Add("OK", "");
            DicMess.Add("Err", "");
            string CreateLoaadtime = Time + "01日";
            DateTime CheckTime = Convert.ToDateTime(CreateLoaadtime); 
            Models.SalaryModel salaryModel = new Models.SalaryModel();
            //这句话是获得当前的有信息的页数
            List<ExcelShellMode> list_ExcelShell = salaryModel.Database.SqlQuery<ExcelShellMode>("exec VMode_Select_ExcelShellModel @CheckType = '专项附加扣除'").ToList();
            for (int a = 0; a < list_ExcelShell.Count; a++)
            {
                // 这里需要进行更新添加
                int ExcelShell = list_ExcelShell[a].ExcelShell;//当前页数
                                                               // 传入页数 , 名称, 类别 
            }

            // int C_Name_Index = salaryModel.Database.SqlQuery<int>("select ExcelCell from Comparative where ExcelName = '年金' and ExcelRowCellName = '姓名'").ToList()[0];
            ExcelShellComparative excelShellComparative_zn = salaryModel.Database.SqlQuery<ExcelShellComparative>("exec VMode_Select_ExcelComparative @CheckType = '专项附加扣除',@ExcelRowCellName = '累计子女教育'").FirstOrDefault();
            


            foreach (var file in files)
            {
                #region 专项附加扣除 文件部分 
                ISheet sheet;
                string filename = Path.GetFileName((file.FileName));
                var fileExt = Path.GetExtension(filename);
                //这里循环shell 
                if (fileExt == ".xls" || fileExt == ".xlsx")
                {
                    #region MyRegion
                    if (fileExt == ".xls")
                    {
                        HSSFWorkbook hssfwb = new HSSFWorkbook(file.InputStream);
                        #region saves 在这里进行循环添加shell 页码的数据到 datatable 到数据库
                        for (int a = 0; a < list_ExcelShell.Count; a++)
                        {
                            // 这里需要进行更新添加
                            int ExcelShell = list_ExcelShell[a].ExcelShell;//当前页数
                            // 传入页数 , 名称, 类别 
                        }
                        #endregion

                    }
                    else
                    {
                        XSSFWorkbook hssfwb = new XSSFWorkbook(file.InputStream);
                        #region saves 在这里进行循环添加shell 页码的数据到 datatable 到数据库
                        string Save_C_Name = ""; // 这个是找到这行的名字
                        string Save_C_IdentiId_Index = "";//身份证号
                        decimal Save_C_YearBase_Index = 0; // 年金基数
                        decimal Save_C_PersonalMoney_Index = 0;// 个人缴费金额
                        decimal Save_C_Enterprise_Index = 0;//企业缴费金额
                        decimal Save_C_YearBaseCount_Index = 0;// 缴费小计  
                        for (int a = 0; a < list_ExcelShell.Count; a++)
                        {
                            #region 循环不同的shell 页
                            sheet = hssfwb.GetSheetAt(a); // 在这里循环
                            try
                            {
                                DataTable table = new DataTable();
                                IRow headerRow = sheet.GetRow(0);// 从第0行进行读取
                                int cellCount = headerRow.LastCellNum;
                                for (int i = headerRow.FirstCellNum; i < cellCount; i++)
                                {
                                    DataColumn column = new DataColumn(headerRow.GetCell(i).ToString());
                                    table.Columns.Add(column);
                                }
                                int rowCount = sheet.LastRowNum;
                                // for (int i = 1; i < sheet.LastRowNum; i++)// 从第3行循环
                                for (int i = (sheet.FirstRowNum) + 1; i < sheet.LastRowNum; i++)
                                {
                                    IRow row = sheet.GetRow(i);
                                    DataRow dataRow = table.NewRow();
                                    for (int j = row.FirstCellNum; j < cellCount; j++)
                                    {

                                        #region 判断类型
                                        ICell RCells = row.GetCell(j);
                                        if (RCells != null)
                                        {
                                            try
                                            {
                                                switch (RCells.CellType)  //注意按单元格格式分类取值
                                                {
                                                    case CellType.Numeric:    //用于取出数值和公式类型的数据 
                                                        dataRow[j] = RCells.NumericCellValue;

                                                        break;
                                                    case CellType.Error:
                                                        dataRow[j] = ErrorEval.GetText(row.GetCell(j).ErrorCellValue);
                                                        break;
                                                    case CellType.Formula:
                                                        switch (row.GetCell(j).CachedFormulaResultType)
                                                        {
                                                            case CellType.String:
                                                                string strFORMULA = row.GetCell(j).StringCellValue;
                                                                if (strFORMULA != null && strFORMULA.Length > 0)
                                                                {
                                                                    dataRow[j] = strFORMULA.ToString();
                                                                }
                                                                else
                                                                {
                                                                    dataRow[j] = null;
                                                                }
                                                                break;
                                                            case CellType.Numeric:
                                                                dataRow[j] = Convert.ToString(row.GetCell(j).NumericCellValue);
                                                                break;
                                                            case CellType.Boolean:
                                                                dataRow[j] = Convert.ToString(row.GetCell(j).BooleanCellValue);
                                                                break;
                                                            case CellType.Error:
                                                                dataRow[j] = ErrorEval.GetText(row.GetCell(j).ErrorCellValue);
                                                                break;
                                                            default:
                                                                dataRow[j] = "";
                                                                break;
                                                        }
                                                        break;
                                                    case CellType.Boolean:
                                                        // Boolean type
                                                        dataRow[j] = RCells.BooleanCellValue.ToString();
                                                        break;

                                                    case CellType.Blank:
                                                        break;

                                                    default:
                                                        // String type
                                                        dataRow[j] = RCells.StringCellValue.Trim();
                                                        break;
                                                }
                                            }
                                            catch (Exception e)
                                            {

                                                DicMess["Err"] = "导入成功,读取数据发生错误";
                                            }


                                        }
                                        else
                                        {
                                            dataRow[j] = "";
                                        }

                                        #endregion
                                    }
                                    #region 查询信息进行存储
                                    //1.查询出当前的 用户信息 Salarybaseinfo 的用户信息
                                    //2.关联出当前月份的 CompanyWelfareSalary 的年金信息 进行更新
                                    //if () { }
                                    #endregion
                                    table.Rows.Add(dataRow);
                                    //Save_C_Name = (dataRow[C_Name_Index] == null || string.IsNullOrWhiteSpace(dataRow[C_Name_Index].ToString())) ? "" : dataRow[C_Name_Index].ToString();
                                    //Save_C_IdentiId_Index = (dataRow[C_IdentiId_Index] == null || string.IsNullOrWhiteSpace(dataRow[C_IdentiId_Index].ToString())) ? "" : dataRow[C_IdentiId_Index].ToString();
                                    //Save_C_YearBase_Index = (dataRow[C_YearBase_Index] == null || string.IsNullOrWhiteSpace(dataRow[C_YearBase_Index].ToString())) ? 0 : decimal.Parse(dataRow[C_YearBase_Index].ToString());
                                    //Save_C_PersonalMoney_Index = (dataRow[C_PersonalMoney_Index] == null || string.IsNullOrWhiteSpace(dataRow[C_PersonalMoney_Index].ToString())) ? 0 : decimal.Parse(dataRow[C_PersonalMoney_Index].ToString());
                                    //Save_C_Enterprise_Index = (dataRow[C_Enterprise_Index] == null || string.IsNullOrWhiteSpace(dataRow[C_Enterprise_Index].ToString())) ? 0 : decimal.Parse(dataRow[C_Enterprise_Index].ToString());
                                      Save_C_YearBaseCount_Index = (dataRow[C_YearBaseCount_Index] == null || string.IsNullOrWhiteSpace(dataRow[C_YearBaseCount_Index].ToString())) ? 0 : decimal.Parse(dataRow[C_YearBaseCount_Index].ToString());
                                    if (Save_C_PersonalMoney_Index < 0)
                                    {
                                        Save_C_PersonalMoney_Index = Save_C_PersonalMoney_Index + 0;
                                    }
                                    var BaseInfo = salaryModel.SalaryBaseInfoes.Where(n => n.UserName == Save_C_Name).FirstOrDefault();
                                    if (BaseInfo != null)
                                    {
                                        int Id = BaseInfo.Id;//用户id
                                        var ComWel = salaryModel.CompanyWelfareSalaries.Where(n => n.BaseInfoId == Id & n.CompanyWelfareTime == CheckTime).FirstOrDefault();
                                        if (ComWel != null)
                                        {
                                            //   AnnuityYearSalaryEnterpriseProportion decimal(18, 5) , --企业年金企业缴费比例 
                                            //   AnnuityYearSalaryPersonalProportion decimal(18, 5) , --企业年金个人缴费比例 



                                            ComWel.AnnuityYearSalaryBase = Save_C_YearBase_Index;//基数 
                                            ComWel.AnnuityYearSalaryEnterpriseProportion = decimal.Round((Save_C_Enterprise_Index / Save_C_YearBase_Index), 2, MidpointRounding.AwayFromZero); //企业年金企业缴费比例 
                                            ComWel.AnnuityYearSalaryPersonalProportion = decimal.Round((Save_C_PersonalMoney_Index / Save_C_YearBase_Index), 2, MidpointRounding.AwayFromZero); //企业年金个人缴费比例 

                                            ComWel.AnnuityYearSalaryEnterpriseQuota = Save_C_Enterprise_Index;//企业年金企业补缴额(税前)	
                                            ComWel.AnnuityYearSalaryPersonalQuota = Save_C_PersonalMoney_Index; //--企业年金个人补缴额




                                        }
                                        else
                                        {
                                            ComWel.AnnuityYearSalaryBase = 0;//基数 
                                            ComWel.AnnuityYearSalaryEnterpriseProportion = 0; //企业年金企业缴费比例 
                                            ComWel.AnnuityYearSalaryPersonalProportion = 0; //企业年金个人缴费比例 

                                            ComWel.AnnuityYearSalaryEnterpriseQuota = 0;//企业年金企业补缴额(税前)	
                                            ComWel.AnnuityYearSalaryPersonalQuota = 0; //--企业年金个人补缴额

                                        }
                                    }
                                }

                            }
                            catch (Exception e)
                            {
                                DicMess["Err"] = "导入成功,读取数据发生错误";

                            }

                            #endregion

                        }
                        #endregion
                    }

                    #endregion
                    DicMess["OK"] = "导入成功" + DicMess["OK"];

                }
                else
                {
                    DicMess["Err"] = "导入文件不是Excel请从新上传";

                }


                #endregion 
            }
            salaryModel.SaveChanges();


            return DicMess;
            #endregion


        }

        #endregion

获取的信息是这样的

  ExcelShellComparative excelShellComparative_zn = salaryModel.Database.SqlQuery<ExcelShellComparative>("exec VMode_Select_ExcelComparative @CheckType = '专项附加扣除',@ExcelRowCellName = '累计子女教育'").FirstOrDefault();
           

这样,就能看的到,获得了,我们想要的数据的信息,至此。我们就可以根据这些进行分情况进行获取需要的信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值