简体   繁体   English

如果 Excel 列在 asp.net c# 中具有某些值,则提示消息

[英]Prompt message if Excel column has some value in asp.net c#

I have a criteria where user uploads excel.我有一个用户上传 excel 的标准。 So in that I want to check if column Is Replacement (Y/N) has values as Y then I want Replacement SAP ID cannot be blank.因此,我想检查列Is Replacement (Y/N)的值是否为Y ,然后我希望Replacement SAP ID不能为空。 If the column is blank then prompt a alert message.如果该列为空,则提示一条警报消息。 Below is the image for the same.下面是相同的图像。

擅长

System.Data.OleDb.OleDbConnection connExcel = new System.Data.OleDb.OleDbConnection(conStr);
            System.Data.OleDb.OleDbCommand cmdExcel = new System.Data.OleDb.OleDbCommand();
            System.Data.OleDb.OleDbDataAdapter oda = new System.Data.OleDb.OleDbDataAdapter();
            cmdExcel.Connection = connExcel;
            connExcel.Open();

            System.Data.DataTable dtExcelSchema = connExcel.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
            System.Data.DataTable dtExcelColumnsTable = connExcel.GetSchema("Columns");
            string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString().Replace('\'', ' ').Trim();  //nadeem
            cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
            oda.SelectCommand = cmdExcel;
            oda.Fill(dtExcelRows);

I take all columns in dtExcelRows我取dtExcelRows中的所有列

I think that It's good to use your Business Exception and when the Replacement SAP ID is blank you should raise the Exception.我认为使用您的业务例外很好,当替换 SAP ID为空白时,您应该提出例外。

foreach(var item in dtExcelRows["ColumnName"]){
    if(string.IsEmptyOrNull(item){
           throw new HttpException((int)HttpStatusCode.BadRequest, "SAP ID is Empty.");
      }
}

I just write some pseudo Code I'm not sure about reading data from a column.我只是写了一些伪代码,我不确定从列中读取数据。

And if you handle BadRequest exception in the front-end, you can prompt an alert message to the client.如果你在前端处理 BadRequest 异常,你可以向客户端提示一条警报消息。

Index指数

Example answer is almost at the end - just above references section.示例答案几乎在最后 - 就在参考部分的上方。 Hope it helps you.希望它可以帮助你。

Unclear scope范围不明确

It is hard to grasp exact tech stack You are referring to.很难掌握您所指的确切技术堆栈。 Are you limited to OleDb?您是否仅限于 OleDb? or can you use any nuget package?或者你可以使用任何 nuget 包吗? What are the restrictions / technical base for "promt a alert message" ? “提示警报消息”的限制/技术基础是什么? webforms?网络表格? ASP.NET 2.0? ASP.NET 2.0? I mean, is it an AJAX control toolkit alert or a simple我的意思是,它是一个 AJAX 控制工具包警报还是一个简单的

 window.alert("message");

type Javascript ?输入 Javascript 吗? maybe one within jQuery which should only open after the page ( if rendered ) is rendered :可能是 jQuery 中的一个,它应该只在页面(如果呈现)被呈现后打开:

 jQuery(document).ready(function() {window.alert("message");})

?? ??

Tip how to get a clearer view about what is happening提示如何更清楚地了解正在发生的事情


Basically at基本上在

 cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
        oda.SelectCommand = cmdExcel;
        oda.Fill(dtExcelRows);

it seems for me that you actually fill the dataset with whole sheet data, not only the names from the first row在我看来,您实际上用整个工作表数据填充数据集,而不仅仅是第一行的名称

If you have Visual Studio, write something trivial just after the oda.Fill line, like如果您有 Visual Studio,请在 oda.Fill 行之后写一些微不足道的内容,例如

String t= "";

place a breakpoint on this trivial line (or any line with code just after that Fill one在这个平凡的行(或任何紧随其后的代码行)上放置一个断点填充一个

then press F5.然后按 F5。

After IISExpress is launched, in the page go to the upload scenario where this code will run, and then when debugger pauses on the debug point you just set, IISExpress启动后,在页面中转到将运行此代码的上传场景,然后当调试器在您刚刚设置的调试点上暂停时,

select word dtExcelRows then right click Add to Watch选择单词 dtExcelRows 然后右键单击 Add to Watch

lower you will see Watch toolbar there near the dtExcelRows is magnifiying glass.在下方,您会看到 dtExcelRows 附近的 Watch 工具栏是放大镜。

That is DataSet visualizer.那就是 DataSet 可视化工具。 Click on that and you will see what was filled actually inside the dtExcelRows object单击它,您将看到 dtExcelRows 对象中实际填充的内容

Now you can figure out what exactly you need.现在你可以弄清楚你到底需要什么。

Answer to Your question回答你的问题


Unfortunately unless I know more exact details about approximate technical limits (version of ASP.NET or other technology, it will be long to try to write all the possible variants how this could be implemented.)不幸的是,除非我知道有关近似技术限制(ASP.NET 或其他技术的版本)的更确切细节,否则尝试编写所有可能的变体如何实现这一点将很长。)

Therefore I will limit myself to a "pseudocode"因此,我将自己限制为“伪代码”

 // i think you could alse refer to an index
//(dtExcelRows as DataSet).Tables[0].Rows[0].ToString();
DataSet dtExcelRowsShoulBeDataSet = (dtExcelRows as DataSet);

if (dtExcelRowsShoulBeDataSet != null) {
  if (dtExcelRowsShoulBeDataSet.Tables.Length > 0) {
    int numberOfColumns = dtExcelRowsShoulBeDataSet.Tables[0].Columns.Count;
    String columnNameDoYouNeedItOrNumberIsEnoug_Question_Column = "";
    String columnNameDoYouNeedItOrNumberIsEnoug_REPLACEMENTSAPID_Column = "";

    foreach (DataColumn column in dtExcelRowsShoulBeDataSet.Tables[0].Columns) {
      if (row [column]
              .ToString()
              .Trim()
              .StartsWith("Is Replacement")) {
        columnNameDoYouNeedItOrNumberIsEnoug_Question_Column =
            column.ColumnName;
      } else if (row [column]
                     .ToString()
                     .Trim()
                     .StartsWith("Replacement SAP")) {
        columnNameDoYouNeedItOrNumberIsEnoug_REPLACEMENTSAPID_Column =
            column.ColumnName;
      }
    }
//now you know the column names, so can use them alike dictionary indexes
    for (int i = 1; i < dtExcelRowsShoulBeDataSet.Tables[0].Rows.Count; i++) {
      if (row [columnNameDoYouNeedItOrNumberIsEnoug_Question_Column]
                  .ToString()
                  .Trim()
                  .ToUpper() != "Y" &&
          row [columnNameDoYouNeedItOrNumberIsEnoug_Question_Column]
                  .ToString()
                  .Trim()
                  .ToUpper() != "N") {
        // later .net versions you can use IsNullOrWhiteSpace method here
        if (String.IsNullOrEmpty(
                row
                [columnNameDoYouNeedItOrNumberIsEnoug_REPLACEMENTSAPID_Column]
                    .ToString()
                    .Trim())) {
          // if AJAX then registerscript, addstartupscript etc..
          Response.Write(
              "<scipt type=\"text/javascript\">window.alert(\"hey, you forgot to specify the Replacement SAP ID at Excel row Nr " +
              (i) + " !\")</script>");
          Response.End();
        }
      }
    }
  }
}

References参考


Microsoft .NET C# documentation pages. Microsoft .NET C# 文档页面。

Just add the viewFallbackFrom parameter (or simply edit the view= parameter from current default 6 to your older tech stack version. So you can currently do in any Microsoft documentation site to see what is avaliable for your version_只需添加 viewFallbackFrom 参数(或简单地将 view= 参数从当前默认 6 编辑到您的旧技术堆栈版本。因此您目前可以在任何 Microsoft 文档站点中查看适用于您的版本的内容_

https://docs.microsoft.com/en-us/dotnet/api/system.data.datacolumn?view=net-6.0&viewFallbackFrom=net-3.0 https://docs.microsoft.com/en-us/dotnet/api/system.data.datacolumn?view=net-6.0&viewFallbackFrom=net-3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM