Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
           try
           {
               // 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;
       }
Posted
Updated 12-Dec-17 22:23pm
v2
Comments
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)



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