上传到DataTable时查找空的Excel文件[英] Find the Empty excel file when upload to datatable

本文是小编为大家收集整理的关于上传到DataTable时查找空的Excel文件的处理方法,想解了上传到DataTable时查找空的Excel文件的问题怎么解决?上传到DataTable时查找空的Excel文件问题的解决办法?上传到DataTable时查找空的Excel文件问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

用户通过Excel文件上传输入. 我想查找Excel文件是空的,否则.我检查"查找空文件"的内容长度和文件大小..大小在空文件之间有所不同(没有类型和用户类型数据,并删除所有数据以使一个空)适用于空).示例emptyfile 8714的内容长度,但用户键入任何数据并删除所有数据,然后内容长度为8104,例如

因此,我将Excel文件转换为DataTable并检查数据词是否为空.

我的代码:

string dirpath = Server.MapPath("~") + "uploadfile\\";
                string LocalFilePath = dirpath + fileName;
                fileUpload.PostedFile.SaveAs(LocalFilePath);

                string fileType = Path.GetExtension(fileName);        
                string SourceConstr = string.Empty;
                var Inputs = ProjectAttributeMasterService.GetInputAttributes(7);

                if (fileType == ".xls" || fileType == ".xlsx")
                {



                    if (fileType == ".xls")
                    {
                        SourceConstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + LocalFilePath + "';;Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";

                    }


                    OleDbConnection con = new OleDbConnection(SourceConstr);
                    con.ResetState();
                    con.Open();

                    System.Data.DataTable dtl = new DataTable();
                    System.Data.DataTable dts = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                    OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                    da.Fill(dtl);


                    if ((dtl.Rows.Count >= 1 || dtl.Columns.Count >= 1) && !dtl.Columns.Contains("F1")){
            }
          else
          {
         /empty file
          }

dtl.columns.contains(" f1")..如果我使用此条件,即使Excel都包含单元格中的数据,然后返回空.不好.

推荐答案

使用以下代码:

     OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
     da.Fill(ds);
    if ((ds.Table.Count >= 1)
     {    
          if (ds.Tables[0].Rows.Count > 0)
          {
             //Excel contains data
          }
          else
          {
             //file is empty
          }   
     }
     else
     {
       //file is empty
     }

本文地址:https://www.itbaoku.cn/post/1154703.html

问题描述

User upload the inputs through the Excel file. I want to find that excel file is empty or else.I check the content length and file size for find empty file..size is vary between empty files(without type and user type data and delete the all data to make an empty)For example content length for emptyfile 8714 but user type any data and delete all the data then the content length is 8104 like that

So I convert the excel file to datatable and check the datatable is null or not.when convert empty file to datatable the datatable have the default column F1.so datatable =null not work.Pls help me

My code:

string dirpath = Server.MapPath("~") + "uploadfile\\";
                string LocalFilePath = dirpath + fileName;
                fileUpload.PostedFile.SaveAs(LocalFilePath);

                string fileType = Path.GetExtension(fileName);        
                string SourceConstr = string.Empty;
                var Inputs = ProjectAttributeMasterService.GetInputAttributes(7);

                if (fileType == ".xls" || fileType == ".xlsx")
                {



                    if (fileType == ".xls")
                    {
                        SourceConstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + LocalFilePath + "';;Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";

                    }


                    OleDbConnection con = new OleDbConnection(SourceConstr);
                    con.ResetState();
                    con.Open();

                    System.Data.DataTable dtl = new DataTable();
                    System.Data.DataTable dts = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                    OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                    da.Fill(dtl);


                    if ((dtl.Rows.Count >= 1 || dtl.Columns.Count >= 1) && !dtl.Columns.Contains("F1")){
            }
          else
          {
         /empty file
          }

dtl.Columns.Contains("F1")..If i use this condition even the Excel contain Data in cell "F1" then return Empty..and F1 is not default column for all server...So Its also not fine.

推荐答案

Use the code as follows:

     OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
     da.Fill(ds);
    if ((ds.Table.Count >= 1)
     {    
          if (ds.Tables[0].Rows.Count > 0)
          {
             //Excel contains data
          }
          else
          {
             //file is empty
          }   
     }
     else
     {
       //file is empty
     }
查看更多