且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据从Excel / CSV文件导入到angularjs json对象中

更新时间:2022-11-02 23:36:36

您将不得不读取您想要的数据(文件阅读器,Ajax调用等)然后使用正则表达式解析数据。然后,当你有一个字符串,使用JSON解析

You are going to have to read the data in however you want (File Reader, Ajax call, etc.) and then parse the data using Regular Expressions. Then when you have a string, use JSON parse

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

this isn't my code but here is a snippet of the linked 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/