Click here to Skip to main content
12,948,385 members (74,761 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 datatable =null not work.Pls help me

My code:

string dirpath = Server.MapPath("~") + "uploadfile\\";
                string LocalFilePath = dirpath + fileName;
                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);
                    System.Data.DataTable dtl = new DataTable();
                    System.Data.DataTable dts = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con);
                    if ((dtl.Rows.Count >= 1 || dtl.Columns.Count >= 1) && !dtl.Columns.Contains("F1"))
/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..Pls help me

Thanks in advance
Posted 15-Nov-12 19:00pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.[^]


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,344
CHill60 3,275
Maciej Los 2,703
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web01 | 2.8.170524.1 | Last Updated 27 May 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100