简体   繁体   English

无论如何,我可以将 Excel 文件附加到我的 HTML 或 Javascript 中,以用作数据库吗?

[英]Is there anyway I can attach an Excel file to my HTML or Javascript, to be used as a database?

I want to auto-populate a person data on my website, and I need it to get data from an Excel file.我想在我的网站上自动填充个人数据,我需要它来从 Excel 文件中获取数据。 So that, the data on my website will automatically change if its updated on Excel.因此,如果在 Excel 上更新,我网站上的数据将自动更改。 Sadly I need to do this using only vanilla Javascript.可悲的是,我只需要使用 vanilla Javascript 来做到这一点。

No. You cannot* use vanilla JavaScript to pull data from an Excel file located on your server.不可以。您不能*使用普通 JavaScript 从位于您服务器上的 Excel 文件中提取数据。 However, if you want to populate data on your page from a markup file and use something like JSON or XML , that is much more doable and I think answers your use case.但是,如果您想从标记文件中填充页面上的数据并使用JSONXML 之类的内容,那更可行,我认为可以回答您的用例。 Here's a simple example:这是一个简单的例子:

data.js : data.js

var xhr = new XMLHttpRequest();

xhr.responseType = 'json';
xhr.open('GET', 'data.json');

xhr.onload = function() {
  build_table(xhr.response);
}

function build_table(data) {
  var result = '';
  for (var i = 0; i < data.length; i++) {
    result += build_row(data[i]);
  }
  document.getElementById('data').innerHTML += result;
}

function build_row(data) {
  var result = '<tr>';
  result += '<td>' + data.firstname + '</td>';
  result += '<td>' + data.lastname + '</td>';
  result += '<td>' + data.country + '</td>';
  result += '</tr>';
  return result;
}

index.html : index.html

<head>
  <title>AJAX Example</title>
  <script src="data.js"></script>
</head>
<body>
  <table id="data">
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Country</th>
    </tr>
  </table>
</body>

data.json : data.json

[
    {
        "firstname": "John",
        "lastname": "Smith",
        "country": "United States"
    },
    {
        "firstname": "Alice",
        "lastname": "Lastname",
        "country": "Germany"
    },
    {
        "firstname": "Bob",
        "lastname": "Bobson",
        "country": "United States"
    }
]

If you must work with Excel data, I would use the Python openpyxl and json libraries to read and write your data to this markup file, respectively, though I'm sure similar libraries exist for the language of your choice.如果您必须使用 Excel 数据,我会分别使用 Python openpyxljson库来读取您的数据并将您的数据写入此标记文件,但我确信对于您选择的语言存在类似的库。

I don't think the following applies, but just for the sake of completeness: if it's an option for you, you could have your webserver build this HTML before it's served, using one of a number of options for your data access layer ( openpyxl being one of them).我不认为以下适用,但只是为了完整性:如果它是您的一个选项,您可以让您的网络服务器在提供此 HTML 之前构建它,使用数据访问层的多个选项之一( openpyxl成为其中之一)。

* It is, literally speaking, possible to access an .xlsx file via AJAX, unzip it using zip.js or similar, and process its XML contents. * 从字面上讲,可以通过 AJAX 访问.xlsx文件,使用zip.js或类似方法解压缩它,并处理其 XML 内容。 I do not think you want to do this.我不认为你想这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM