上传到datatable时找到Empty excel文件 [英] Find the Empty excel file when upload to datatable

查看:77
本文介绍了上传到datatable时找到Empty excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户通过Excel文件上传输入。

我想找到excel文件为空或者。我检查内容长度和文件大小以查找空文件.size是不同的空文件(没有类型和用户类型数据并删除所有数据以使其为空)例如空文件8714的内容长度但是用户键入任何数据并删除所有数据然后内容长度为8104就像那样



所以我将excel文件转换为datatable并检查datatable是否为null。当将空文件转换为datatable时,datatable具有默认列F1.so datatable = null not work.Pls help我



我的代码:



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)..如果我用这个条件甚至Excel包含单元格中的数据F1然后返回Empty ..并且F1不是所有服务器的默认列...所以它也不好......请帮助我





提前致谢




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..Pls help me


Thanks in advance

推荐答案

,con);
da.Fill(dtl );


if ((dtl。 Rows.Count > = 1 || dtl.Columns.Count > = 1 )&& !dtl.Columns.Contains( F1))
{
}
其他
{
/空档案
}
", 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包含单元格F1中的数据,然后返回空......并且F1不是所有服务器的默认列...所以它也不行......请帮助我





谢谢提前




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..Pls help me


Thanks in advance


您好
如果您在连接字符串中指定 HDR =否,那么当您的Excel工作表为数据时,数据表中不会有包含数据的行空的。



HDR =是;表示第一行包含列名,而不是数据。 HDR =无;表示相反。

http://www.connectionstrings.com/excel [ ^ ]





问候

Dominic
Hi If you are specify HDR = No in your connection string, then datatable will not have rows with data when your excel sheet is empty.

"HDR=Yes;" indicates that the first row contains columnnames, not data. "HDR=No;" indicates the opposite.
http://www.connectionstrings.com/excel[^]


Regards
Dominic


这篇关于上传到datatable时找到Empty excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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