Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to read .xlsx sheet in c# whose contents are spread over sheet. It's not a single table.Please look at the image http://i.stack.imgur.com/aaXxz.png[^].

I've highlighted the things which I want to read separately, in dataTables , if possible.
I've used Following code but it only works when there is only one table is present in the sheet.


Please help. Thanks.

What I have tried:

public DataSet ReadXlsx(string filepath)
{
    try
    {
        FileStream stream = File.Open(filepath, FileMode.Open, FileAccess.Read);

        //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
        IExcelDataReader excelReader2 = ExcelReaderFactory.CreateOpenXmlReader(stream);

        //...
        //4. DataSet - Create column names from first row
        excelReader2.IsFirstRowAsColumnNames = true;
        DataSet result2 = excelReader2.AsDataSet();

        return result2;
    }
    catch (Exception ex)
    {

        return null;
    }
}
Posted
Updated 19-Sep-16 1:17am
Comments
Maciej Los 19-Sep-16 6:30am    
What is ExcelReaderFactory?
Er. Shailesh 19-Sep-16 7:57am    
Its a class from MetaData.
public static class ExcelReaderFactory
{
public static IExcelDataReader CreateBinaryReader(Stream fileStream);
public static IExcelDataReader CreateOpenXmlReader(Stream fileStream);
}
xszaboj 19-Sep-16 6:42am    
Can you provide example excel document?
Er. Shailesh 19-Sep-16 7:59am    
How can I provide that document ? I did not find any option to attach file while posting the question.

1 solution

You get your code from exceldatareader.codeplex.com. if you get that component and want to complete your code based on that component you can continue like this:

You have your DataSet now, so continue reading it as you do with database results:

C#
            DataSet ds= ReadXlsx(filepath);

            string theOrder = ds.Tables[0].Rows[0][0].ToString();
            string theSite = ds.Tables[0].Rows[2][0].ToString();
            List<job> jobs= new List<job>();
            for (int i = 4; i < ds.Tables[0].Rows.Count; i++)
            {
                Job job=new Job();
                job.Number = ds.Tables[0].Rows[i][0];
                job.Facility = ds.Tables[0].Rows[i][1].ToString();
                job.Date = ds.Tables[0].Rows[i][2];
                job.TrackingNumber = ds.Tables[0].Rows[i][3];
                jobs.Add(job);
            }

</job></job>


I also recommend you to use EPPlus with great features.
you can download it via NuGet:

https://www.nuget.org/packages/EPPlus[^]

Learn more about it here:
Create/Read/Edit Advance Excel 2007/2010 Report in C#.Net using EPPlus[^]
 
Share this answer
 
Comments
Er. Shailesh 19-Sep-16 8:06am    
@zamanipour, You've asked me to use my method , i.e. DataSet ds= ReadXlsx(filepath);
but that method returns blank dataSet.
zamanipour 19-Sep-16 8:23am    
If you have no errors and dlls works fine then:
Try to delete this line or change it like this, your first row dosnt contains column Names:
excelReader2.IsFirstRowAsColumnNames = false;

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