如何使用ASP.NET C#验证Excel工作表? [英] How to validate the excel sheet using ASP.NET C#?

查看:53
本文介绍了如何使用ASP.NET C#验证Excel工作表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传一个包含多张工作表的excel文档,并将其作为数据表存储在数据集中。现在我要验证数据集。

如果'item no'是必需的。项目编号必须是excel表中强制所有表格。

如果所有表格中第7行的项目编号但列值不同。如何验证,

*所有必填字段的行在所有工作表中都相同但列不同如何验证它。



我尝试过:



  public   Boolean  validate(DataSet ds)
{
int mn = 0 ;
int count = 0 ;
DataTable dt = new DataTable();
dt.Columns.Add( 错误日志);
string msg = 请检查上传的Excel文件;

int tagcountvalue = 0 ;
int Columncountvalue = 0 ;

for int s = 0 ; s < ds.Tables.Count; s ++)
{

for int z = 0 ; z < ds.Tables [s] .Columns.Count; z ++)
{
if (ds .Tables [s] .Rows [ 7 ] [z] .ToString()。ToUpper()。Trim()== item no // 基于项目编号的计数
{
tagcountvalue = z - 1 ;
Columncountvalue = z;
z = ds.Tables [ 0 ]。Columns.Count;
}
}
for int z = 0 ; z < ds.Tables [s] .Columns.Count; z ++)
{
if (ds.Tables [s] .Rows [ 10 ] [z] .ToString() .ToUpper()。修剪()== 项目编号 || count!= 0 // 基于项目编号的计数
{
count = count + 1 ;
}
}
for int i = 2 ; i < tagcountvalue; i ++)
{
if (ds.Tables [ 0 ]。行[ 2 ] [i ] .ToString()!= && ds.Tables [ 0 ]。行[ 10 ] [i] .ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 标签号应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[ 1 ] [i] .ToString()== && ds.Tables [ 0 ]。行[ 7 ] [i]。 ToString()!=
{
dt.Rows.Add ();
dt.Rows [mn] [ 0 ] = 标签说明对标签号码是强制性的;
mn = mn + 1 ;
}
int tagpart = 0 ;
for int j = 21 ; j < ds.Tables [ 0 ]。Rows.Count; j ++)
{
if (ds.Tables [ 0 ]。行[j] [i] .ToString()!= && ds.Tables [ 0 ]。行[ 7 ] [i] .ToString()!=
{
tagpart = tagpart + 1 ;
}
if (ds.Tables [ 0 ]。行[j] [i] .ToString()== && ds.Tables [ 0 ]。行[ 7 ] [i] .ToString()==
{
tagpart = tagpart + 1 ;
}
}
如果(tagpart == 0
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 每个单位的部件应对标签号是强制性的;
mn = mn + 1 ;
}
for int j = 12 ; j < ds.Tables [ 0 ]。Rows.Count; j ++)
{

if (ds.Tables [ 0 ] .Rows [j] [i] .ToString()!= && ds .Tables [ 0 ]。行[j] [Columncountvalue + 2 ]。ToString()== < span class =code-string>

{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 已安装的数量应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[j] [i] .ToString()!= &&(ds.Tables [ 0 ]。行[j] [Columncountvalue + 9 ]。ToString()== && ds.Tables [ 0 ]。行[j] [Columncountvalue + 10 ]。ToString()== ))
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 供应商/制造商PartNumber应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[j] [i] .ToString()!= && ds.Tables [ 0 ]。行[j] [Columncountvalue + 23 ]。ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 提前期应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[j] [i] .ToString()!= && ds.Tables [ 0 ]。行[j] [Columncountvalue + 22 ]。ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 单价应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[j] [i] .ToString()!= && ds.Tables [ 0 ]。行[j] [Columncountvalue + 20 ]。ToString()!=
{
string units = ds.Tables [ 0 ]。行[j] [Columncountvalue + 20 ]。ToString();
string [] arrayString = units.Split(' ');
int counts = arrayString.Count();
如果(计数== 1
{
dt。 Rows.Add();
dt.Rows [mn] [ 0 ] = 货币应提供单价;
mn = mn + 1 ;
}
}
如果(ds.Tables [ 0 ] .Rows [j] [i] .ToString()!= &&(ds .Tables [ 0 ]。行[j] [Columncountvalue + 13 ]。ToString()== < span class =code-string>
&& ds.Tables [ 0 ]。行[j] [Columncountvalue + 14 ]。ToString()== && ds.Tables [ 0 ]。行[j] [Columncountvalue + 16 ]。ToString()== && ; ds.Tables [ 0 ]。行[j] [Columncountvalue + 17 ]。ToString()= = ))
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 供应商/制造商推荐数量应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[j] [i] .ToString()!= && ds.Tables [ 0 ]。行[j] [Columncountvalue] .ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 项目编号应为必填项;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[j] [i] .ToString()!= && ds.Tables [ 0 ]。行[j] [Columncountvalue + 4 ]。ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 项目描述应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[j] [i] .ToString()!= && ds.Tables [ 0 ]。行[j] [Columncountvalue + 12 ]。ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 制造商名称应为必填项;
mn = mn + 1 ;
}
int tagpart1 = 0 ;
for int k = 21 ; k < ds.Tables [ 0 ]。Rows.Count; k ++)
{
if ((ds.Tables [ 0 ]。行[j] [Columncountvalue ] .ToString()== ds.Tables [ 0 ]。行[k] [Columncountvalue] .ToString())&& ds.Tables [ 0 ]。行[k] [Columncountvalue] .ToString()!= && ds.Tables [ 0 ]。行[k] [Columncountvalue] .ToString()!= 0
{
tagpart1 = tagpart1 + 1 ;
}
}
如果(tagpart1 > 1
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 项目编号应该是唯一的;
mn = mn + 1 ;
}
}
如果(ds.Tables [ 0 ] .Rows [ 7 ] [Columncountvalue + 12 ]。ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 设备描述应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[ 1 ] [Columncountvalue + 14 ]。ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 修订号不应该是强制性的;
mn = mn + 1 ;
}
if (ds.Tables [ 0 ]。行[ 10 ] [Columncountvalue + 16 ]。ToString()==
{
dt.Rows.Add();
dt.Rows [mn] [ 0 ] = 供应商名称应为必填项;
mn = mn + 1 ;
}
}

}
如果(dt.Rows.Count > 0
{
dt = dt.DefaultView.ToTable( true ); // 提供的表值唯一用途
grview .Visible = true ;
grerror.DataSource = dt;
grerror.DataBind();
lnkDownload.Visible = true ;
ExportDataTabletoFile(dt, true );
return false ;
}
其他
{
grview.Visible = false ;
lnkDownload.Visible = false ;
return true ;
}


}



我使用过此代码但工作不正常。

解决方案

通过以下链接: -

c# - 导入Excel工作表并验证松散耦合导入的数据 - 堆栈溢出 [ ^ ]

使用调试器查看代码正在执行的操作以及无法正常工作的内容。



调试器允许您关注逐行执行,检查变量,你会看到有一点它停止了你所期望的。

调试器 - 维基百科,免费的百科全书 [ ^ ]

< a href =http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn>在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

代码时不要做预期的事情,你接近一个错误。


i upload one excel document with more than one sheet and stored in dataset as data table.now i want to validate the dataset.
if 'item no' is mandatory.the item number must be mandatory all sheets in excel sheet.
if item number in 7 th row in all sheets but column value is different. how to validate,
*All mandatory fields rows are same in all sheets but column is difference how to validate it.

What I have tried:

public Boolean validate(DataSet ds)
    {
        int mn = 0;
        int count = 0;
        DataTable dt = new DataTable();
        dt.Columns.Add("Error Log");
        string msg = "Please Check Uploaded Excel File";

        int tagcountvalue = 0;
        int Columncountvalue = 0;
 
for (int s = 0; s < ds.Tables.Count; s++)
        {

            for (int z = 0; z < ds.Tables[s].Columns.Count; z++)
            {
                if (ds.Tables[s].Rows[7][z].ToString().ToUpper().Trim() == "item no")//Item number based count taken
                {
                    tagcountvalue = z - 1;
                    Columncountvalue = z;
                    z = ds.Tables[0].Columns.Count;
                }
            }
for (int z = 0; z < ds.Tables[s].Columns.Count; z++)
            {
                if (ds.Tables[s].Rows[10][z].ToString().ToUpper().Trim() == "ITEM NUMBER" || count != 0)//Item number based count taken
                {
                    count = count + 1;
                }
            }
 for (int i = 2; i < tagcountvalue; i++)
            {
                if (ds.Tables[0].Rows[2][i].ToString() != "" && ds.Tables[0].Rows[10][i].ToString() == "")
                {
                    dt.Rows.Add();
                    dt.Rows[mn][0] = "Tag Number Should be mandatory";
                    mn = mn + 1;
                }
                if (ds.Tables[0].Rows[1][i].ToString() == "" && ds.Tables[0].Rows[7][i].ToString() != "")
                {
                    dt.Rows.Add();
                    dt.Rows[mn][0] = "Tag Description Should be mandatory against Tag Number";
                    mn = mn + 1;
                }
                int tagpart = 0;
                for (int j = 21; j < ds.Tables[0].Rows.Count; j++)
                {
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && ds.Tables[0].Rows[7][i].ToString() != "")
                    {
                        tagpart = tagpart + 1;
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() == "" && ds.Tables[0].Rows[7][i].ToString() == "")
                    {
                        tagpart = tagpart + 1;
                    }
                }
                if (tagpart == 0)
                {
                    dt.Rows.Add();
                    dt.Rows[mn][0] = "Parts per unit Should be mandatory against Tag Number";
                    mn = mn + 1;
                }
 for (int j = 12; j < ds.Tables[0].Rows.Count; j++)
                {

                    if (ds.Tables[0].Rows[j][i].ToString() != "" && ds.Tables[0].Rows[j][Columncountvalue + 2].ToString() == "")
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Installed Qty Should be mandatory";
                        mn = mn + 1;
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && (ds.Tables[0].Rows[j][Columncountvalue + 9].ToString() == "" && ds.Tables[0].Rows[j][Columncountvalue + 10].ToString() == ""))
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Supplier/Manufacturer PartNumber Should be mandatory";
                        mn = mn + 1;
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && ds.Tables[0].Rows[j][Columncountvalue + 23].ToString() == "")
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Lead time Should be mandatory";
                        mn = mn + 1;
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && ds.Tables[0].Rows[j][Columncountvalue + 22].ToString() == "")
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Unit Price Should be mandatory";
                        mn = mn + 1;
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && ds.Tables[0].Rows[j][Columncountvalue + 20].ToString() != "")
                    {
                        string units = ds.Tables[0].Rows[j][Columncountvalue + 20].ToString();
                        string[] arrayString = units.Split(' ');
                        int counts = arrayString.Count();
                        if (counts == 1)
                        {
                            dt.Rows.Add();
                            dt.Rows[mn][0] = "Currency Should be provided with Unit Price";
                            mn = mn + 1;
                        }
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && (ds.Tables[0].Rows[j][Columncountvalue + 13].ToString() == "" && ds.Tables[0].Rows[j][Columncountvalue + 14].ToString() == "" && ds.Tables[0].Rows[j][Columncountvalue + 16].ToString() == "" && ds.Tables[0].Rows[j][Columncountvalue + 17].ToString() == ""))
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Supplier/Manufacturer Recommended Qty Should be mandatory";
                        mn = mn + 1;
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && ds.Tables[0].Rows[j][Columncountvalue].ToString() == "")
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Item Number Should be mandatory";
                        mn = mn + 1;
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && ds.Tables[0].Rows[j][Columncountvalue + 4].ToString() == "")
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Item Description Should be mandatory";
                        mn = mn + 1;
                    }
                    if (ds.Tables[0].Rows[j][i].ToString() != "" && ds.Tables[0].Rows[j][Columncountvalue + 12].ToString() == "")
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Manufacturer Name Should be mandatory";
                        mn = mn + 1;
                    }
                    int tagpart1 = 0;
                    for (int k = 21; k < ds.Tables[0].Rows.Count; k++)
                    {
                        if ((ds.Tables[0].Rows[j][Columncountvalue].ToString() == ds.Tables[0].Rows[k][Columncountvalue].ToString()) && ds.Tables[0].Rows[k][Columncountvalue].ToString() != "" && ds.Tables[0].Rows[k][Columncountvalue].ToString() != "0")
                        {
                            tagpart1 = tagpart1 + 1;
                        }
                    }
                    if (tagpart1 > 1)
                    {
                        dt.Rows.Add();
                        dt.Rows[mn][0] = "Item Number Should be Unique";
                        mn = mn + 1;
                    }
                }
                if (ds.Tables[0].Rows[7][Columncountvalue + 12].ToString() == "")
                {
                    dt.Rows.Add();
                    dt.Rows[mn][0] = "Equipment Description Should be mandatory";
                    mn = mn + 1;
                }
                if (ds.Tables[0].Rows[1][Columncountvalue + 14].ToString() == "")
                {
                    dt.Rows.Add();
                    dt.Rows[mn][0] = "Revision No Should be mandatory";
                    mn = mn + 1;
                }
                if (ds.Tables[0].Rows[10][Columncountvalue + 16].ToString() == "")
                {
                    dt.Rows.Add();
                    dt.Rows[mn][0] = "Supplier Name Should be mandatory";
                    mn = mn + 1;
                }
            }

        }
        if (dt.Rows.Count > 0)
        {
            dt = dt.DefaultView.ToTable(true);//Table value unique purpose provided
            grview.Visible = true;
            grerror.DataSource = dt;
            grerror.DataBind();
            lnkDownload.Visible = true;
            ExportDataTabletoFile(dt, " ", true);
            return false;
        }
        else
        {
            grview.Visible = false;
            lnkDownload.Visible = false;
            return true;
        }


    }


I have used this code but not working properly.

解决方案

Go through following link:-
c# - Importing an Excel Sheet and Validate the Imported Data with Loosely Coupled - Stack Overflow[^]


Use the debugger to see what your code is doing and what it is "not working properly".

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于如何使用ASP.NET C#验证Excel工作表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆