本文介绍了如何从excel中读取数据并在视图中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我需要使用mvc arquitecture创建一个应用程序。我需要显示一份学生名单。这个数据是excel,仍然不太了解mvc的逻辑,我创建了以下类但我不知道如何在视图中显示数据。 学生类别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 studentspublic 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连接Excel conectionpublic 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 datapublic 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 + "]"); } } 我尝试了什么: 我试过这个,但我不知道是不是请我需要帮助:c,我没有控制器中的代码What I have tried:I tried this but i dont know is well please i need help :c , i dont have code in controllers推荐答案一旦你有了活动表,就像你在变量 objsheet 中存储的那样,它非常简单:Once you have the active sheet, as you did store in you variable objsheet it's really simple://This sets the top right cell, Excel starts its index on 1!objsheet.Cells[1,1] = "5"; 获取值是恰恰相反:Getting the value is just the reverse:double MyValue = objsheet.Cells[1,1]; 你甚至可以将公式插入单元格:You could even insert formulas into the cell:objsheet.Cells[1, 5] = "=SUM(D2:D" + (4).ToString() + ")"; 但请务必使用英文输入以使其正常工作。这可能听起来很奇怪,但有些Excel版本的编程语言使用不同的语言。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. 这篇关于如何从excel中读取数据并在视图中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 18:03