Click here to Skip to main content
13,835,588 members
Rate this:
Please Sign up or sign in to vote.
See more:
How to get specific column header and its value in excel using ASP.NET MVC C#
Example: I need to fine the header column name "All Section", but this column is positioned dynamically. some times  there were "All Section" in column D, E, AA etc.
Once I found all these header ("All Section"), then I need to get it's column value and sum its total.

Thank you very much in advance. just need some guidelines or sample code here.

What I have tried:

See below my initial code.

Here's my initial code using this
<pre>using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Excel = Microsoft.Office.Interop.Excel;

public static List<Tuple<int, string>> GetExcelNamesPerFile(string reportFileName, ref Error error)

           List<Tuple<int, string>> excelNamesPerFile = new List<Tuple<int, string>>();
           int? reportFileID = null;
               // get excelFile ID
               using (var dbContext = new EXCELEntities())
                   reportFileID = (from reportFile in dbContext.R_ReportFile where reportFile.FileName == reportFileName select reportFile.ID).First();

               // get excel names
               using (var dbContext = new EXCELEntities())
                   var excelReports = (from reportsDB in dbContext.SchedNo_Loc where reportsDB.ReportFile_ID == reportFileID select reportsDB).ToList();

                   foreach (var item in excelReports)
                       excelNamesPerFile.Add(new Tuple<int, string>(item.ReportFile_ID, item.ScheduleNo));
           catch (Exception ex)
               error = new Error()
                   ErrorCode = "-99",
                   ErrorMessage = ex.Message

               return reportNamesPerFile;

           return excelNamesPerFile;
Updated 12-Dec-17 23:23pm
Richard Deeming 13-Dec-17 10:12am
using Excel = Microsoft.Office.Interop.Excel;

Read the following Microsoft KB article:

Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

There are various ways to read and create Excel spreadsheets on the server without using Office interop. For example:
* EPPlus[^];
* ClosedXML[^];
* ExcelDataReader[^];
* The OpenXML SDK[^];

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

Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190114.1 | Last Updated 13 Dec 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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