Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I have an ASP.NET web application in C#.

I want to import data from excel to Repeater using linq.

The excel file contains company logo(Image), empty rows and multiple table with column names and row names.

Example:
-----------------------------------------------------------------------------
 ClientInfo         |         FieldInfo              |   RigInfo            |
-----------------------------------------------------------------------------
Name | abc          |         Field | f1             |   Rig | r1           |
-----------------------------------------------------------------------------
Div  | d1           |         Field | f2             |   Rig | r3           |
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
 ClientInfo         |         FieldInfo              |   RigInfo            |
-----------------------------------------------------------------------------
Name | abs          |         Field | f3             |   Rig | r2           |
-----------------------------------------------------------------------------
Div  | d3           |         Field | f4             |   Rig | r4           |
-----------------------------------------------------------------------------


What I have tried:

C#
public class ImportFromExcel
   {
       public string Client { get; set; }
       public string Division { get; set; }
       public string Superintendent { get; set; }
       public string GS { get; set; }
       public string Engineer { get; set; }
       public string Foreman { get; set; }
       public string Field { get; set; }
       public string Well { get; set; }
       public string SITE { get; set; }
       public string Operations { get; set; }
       public string Equipment_Name { get; set; }
       public long Distance { get; set; }
       public string Last_24_hrs_Operations { get; set; }
       public string Next_24_hrs_Operations { get; set; }
       public string Issues { get; set; }
       public DateTime EquipmentMovement_in { get; set; }
       public DateTime EquipmentMovement_out { get; set; }
       public long No_OfHours { get; set; }
       public string Crew { get; set; }
       public long TotalNoEmployees { get; set; }

   }


List<ImportFromExcel> regCandidates = new List<ImportFromExcel> { };
            if (fuExcelfile.HasFile)
            {
                string csvFilePath = "";

                if (fuExcelfile.HasFile)
                {
                    csvFilePath = "" + @"" + SaveDocument(fuExcelfile); //Excel file path
                    hdnfilepath.Value = csvFilePath;
                }
                else
                {
                    csvFilePath = hdnfilepath.Value.ToString();
                }


                List<string> Fulltext=  File.ReadAllLines(csvFilePath).Where(line => !string.IsNullOrWhiteSpace(line)).ToList(); //getting some rubbish data

                var strText = Fulltext;
            }


I tried reading as CSV but i'm getting fail to get.

Can any one please help me.

Thanks in adv.
Posted
Updated 7-May-18 3:52am
v4
Comments
Maciej Los 7-May-18 5:58am    
Share your code...
CHill60 7-May-18 7:14am    
Could you try to explain your problem?
abdul subhan mohammed 7-May-18 7:18am    
There are multiple tables in the excel and in the excel file, there are column and rows names.. So, how to fetch data as it having rows and column names.

1 solution

I'd suggest to use EPPlus[^].

var package = new ExcelPackage(new FileInfo(@"fullFIleName.xlsx"));
ExcelWorksheet sheet = package.Workbook.Worksheets[1];

foreach(var tb in sheet.Tables)
{
    //read data from table
    //ignore headers
}


For further details, please see:
c# - EPPlus - Read Excel Table[^]
c# - How to parse excel rows back to types using EPPlus[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900