Click here to Skip to main content
15,895,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create an application with mvc arquitecture. I need to display a list of students. This data is in excel, still do not understand very well the logic of mvc, I created the following classes but I have no idea how to display the data in views.

Class of students
C#
public class Alumno
   {

       public string nombre { get; set; }
       public string apellido { get; set; }
       public int generacion { get; set; }

       public void crearAlumno( string nombre , string apellido , string generacion)
       {
           throw new NotImplementedException();
       }

   }

Excel conection
C#
public class UtilExcel
    {

        private static readonly ILog Log = LogManager.GetLogger(typeof(UtilExcel));

        private static Microsoft.Office.Interop.Excel.Application appExcel;
        private static Workbook newWorkbook = null;
        private static _Worksheet objsheet = null;

        //Method to initialize opening Excel
        public bool excel_init(String path)
        {
            appExcel = new Microsoft.Office.Interop.Excel.Application();
            bool retVal = false;

            if (System.IO.File.Exists(path))
            {
                // then go and load this into excel
                newWorkbook = appExcel.Workbooks.Open(path, true, true);
                objsheet = (_Worksheet)appExcel.ActiveWorkbook.ActiveSheet;
                retVal = true;
            }
            else
            {
                Log.Info("No es posible abrir archivo [" + path + "]");
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                appExcel = null;
                //System.Windows.Forms.Application.Exit();
            }
            return retVal;
        }

        //Method to get value; cellname is A1,A2, or B1,B2 etc...in excel.
        public string excel_getValue(string cellname)
        {
            string value = string.Empty;
            try
            {
                value = objsheet.get_Range(cellname).get_Value().ToString();
            }
            catch
            {
                value = null;
            }

            return value;
        }

        //Method to close excel connection
        public void excel_close()
        {
            if (appExcel != null)
            {
                try
                {
                    newWorkbook.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
                    appExcel = null;
                    objsheet = null;
                }
                catch (Exception ex)
                {
                    appExcel = null;
                    Log.Info("No es posible liberar el objeto: " + ex.ToString());
                }
                finally
                {
                    GC.Collect();
                }
            }
        }



    }


Class to read data

C#
public class ProcesoService
   {

       private static readonly ILog Log = LogManager.GetLogger(typeof(ProcesoService));

       public void procesarCargaDatos(string archivo)
       {
           Log.Info("Inicio proceso archivo [" + archivo + "]");
           UtilExcel utlXls = new UtilExcel();
           string path = @"C:\students.xlsx" + archivo;
           if (utlXls.excel_init(path))
           {
              Alumno prodSrv = new Alumno();
               //
               int fila = 2;
               bool continuar = true;
               while (continuar)
               {
                   //A: Producto
                   string nombre = utlXls.excel_getValue(string.Format("A{0}", fila));
                   if (nombre != null && !nombre.Equals(string.Empty))
                   {
                       //B: Apellido
                       string apellido = utlXls.excel_getValue(string.Format("B{0}", fila));
                       //C: Añoingreso
                       string generacion = utlXls.excel_getValue(string.Format("C{0}", fila));



                       prodSrv.crearAlumno(nombre, apellido, generacion);
                       //
                       fila++;
                   }
                   else continuar = false;
               }

               //
               utlXls.excel_close();
               prodSrv = null;

           }
           utlXls = null;
           Log.Info("Término proceso archivo [" + archivo + "]");
       }



   }


What I have tried:

I tried this but i dont know is well please i need help :c , i dont have code in controllers
Posted
Updated 21-Jul-16 15:51pm

1 solution

Once you have the active sheet, as you did store in you variable objsheet it's really simple:
C#
//This sets the top right cell, Excel starts its index on 1!
objsheet.Cells[1,1] = "5";

Getting the value is just the reverse:
C#
double MyValue = objsheet.Cells[1,1];


You could even insert formulas into the cell:
C#
objsheet.Cells[1, 5] = "=SUM(D2:D" + (4).ToString() + ")";


But be sure to type it in English to make it work. It may sound strange, but some Excel versions have a programming language that is in a different language.
 
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