Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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..Pls help me
 

Thanks in advance
Posted 15-Nov-12 19:00pm
__PP__588
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why not use another method? you can extract this excel file data to .txt file format, if the txt file has no data, it means there is no data in excel, it is an empty file, while,if there are data in txt, it means this excel file is not empty!Look at below code:
             Workbook workbook = new Workbook();
             workbook.LoadFromFile(@"D:\FandH.xls", ExcelVersion.Version97to2003);
             Worksheet sheet = workbook.Worksheets(0);
             sheet.SaveToFile("XLStoTXT.txt", ",");
details in Xls/Xlsx to text C#[^]
  Permalink  
Comments
__PP__ at 16-Nov-12 1:34am
   
I want to check the column header and row values in excel.thats y i go with datatable method..SO i need this in datatable..pls give any suggesions

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

  Print Answers RSS
0 CHill60 360
1 Sarvesh Kumar Gupta 238
2 OriginalGriff 218
3 Sergey Alexandrovich Kryukov 153
4 DamithSL 153


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid