将Excel/CSV文件中的数据导入angularjs-json对象中

import data from Excel /CSV file to in angularjs json object

本文关键字:数据 导入 angularjs-json 对象 CSV 文件 Excel      更新时间:2023-09-26

我正试图让用户从他们的计算机上传一个Excel/CSV文件,然后将其转换为JSON。我需要这些文件作为JSON。我有它,所以他们可以上传CSV到JSON。现在我想要Excel/CSV到JSON。如有任何建议或指导,我们将不胜感激。这需要使用angularJS来完成。谢谢

您必须以任何方式读取数据(File Reader、Ajax调用等),然后使用正则表达式解析数据。然后,当您有一个字符串时,使用JSON解析

这不是我的代码,但这里是链接的jsfiddle 的片段

function CSVToArray(strData, strDelimiter) {
  // Check to see if the delimiter is defined. If not,
  // then default to comma.
  strDelimiter = (strDelimiter || ",");
  // Create a regular expression to parse the CSV values.
  var objPattern = new RegExp((
  // Delimiters.
  "(''" + strDelimiter + "|''r?''n|''r|^)" +
  // Quoted fields.
  "(?:'"([^'"]*(?:'"'"[^'"]*)*)'"|" +
  // Standard fields.
  "([^'"''" + strDelimiter + "''r''n]*))"), "gi");
  // Create an array to hold our data. Give the array
  // a default empty first row.
  var arrData = [[]];
  // Create an array to hold our individual pattern
  // matching groups.
  var arrMatches = null;
  // Keep looping over the regular expression matches
  // until we can no longer find a match.
  while (arrMatches = objPattern.exec(strData)) {
  ...

http://jsfiddle.net/sturtevant/AZFvQ/