如何从Excel文件导入特定单元格以将其显示为Javascript文本框 [英] How Can I Import Specific Cells From Excel File To Display It Into Textboxes In Javascript

查看:143
本文介绍了如何从Excel文件导入特定单元格以将其显示为Javascript文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,该任务包括从excel表格的特定单元格导入数据并在javascript的文本框中显示数据的任务.仅出于显示目的,我需要将excel单元格导入javascript文本框.请提供我要完成任务的代码.谢谢.

i am working on a project ,which consist of a task to import data from specific cell of excel sheets and display the data in textboxes in javascript.just for display purpose i need to import the excel cells into javascript textboxes.please provide me the code to complete the task..thank you.

推荐答案

我建​​议从Excel工作表中获取数据,作为页面方法中的对象列表.
然后将其转换为JSON格式的字符串,然后从方法返回.

现在在JavaScript中,使用jQuery .ajax()方法获取数据并在您要查看的适当输入元素中进行查看.

例子:-

假设我有一个包含个人详细信息的Excel工作表,其中包含三个字段,例如ID,Name&位置.

为它创建一个类:-
I will suggest to get the data from the Excel sheet as a list of objects in a page method.
Then convert it to JSON formatted string and return from the method.

Now in JavaScript use jQuery .ajax() method to get the data and view in the proper input element you want it to view.

Example :-

Let say i have an Excel sheet having Person details with three fields like ID, Name & Location.

Create an class for it :-
public class Person
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
    }




所需的名称空间如下:-




Name spaces required are as below :-

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;



首先,我必须将以下方法放在要调用javascript方法以从XLS文件获取人员详细信息的aspx文件后面的代码中.



First i have to put the below method in the code behind of the aspx file for which i want to call the javascript method to get person details from XLS file.

[System.Web.Services.WebMethod]
        public static string GetPersons()
        {
            List<person> persons = new List<person>();
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"D:\Demo.xlsx");
            Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            for (int i = 1; i <= rowCount; i++)
            {
                Person person = new Person();
                for (int j = 1; j <= colCount; j++)
                {
                    switch (j)
                    {
                        case 1:
                            person.ID = xlRange.Cells[i, j].Value2.ToString();
                            break;
                        case 2:
                            person.Name = xlRange.Cells[i, j].Value2.ToString();
                            break;
                        case 3:
                            person.Location = xlRange.Cells[i, j].Value2.ToString();
                            break;
                    }
                }
                persons.Add(person);
            }

            return new JavaScriptSerializer().Serialize(persons);
        }




现在,使用jQuery .ajax()方法在javascript中调用此方法,以从XLS工作表获取人员详细信息,如下所示:-




Now call this method in javascript using jQuery .ajax() method to get person details from XLS sheet as below :-


(函数(){


. ajax({ 类型:" , url:" , 数据:' {}', contentType:" , dataType:" , 成功:功能(响应){
.ajax({ type: "POST", url: "/ExcelDataView.aspx/GetPersons", data: '{}', contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) {


这篇关于如何从Excel文件导入特定单元格以将其显示为Javascript文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆