将数据从Excel / CSV文件导入到angularjs json对象中 [英] import data from Excel /CSV file to in angularjs json object

查看:159
本文介绍了将数据从Excel / CSV文件导入到angularjs json对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to have a user upload an Excel /CSV file from their computer then convert it to JSON. I am needing these files as JSON. I have it so they can upload a CSV to JSON. Now I am wanting Excel /CSV to JSON. Any advice or guidance on this would be appreciated. This needs to be done using angularJS. Thanks!

推荐答案

您将不得不读取您想要的数据(文件阅读器,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/

这篇关于将数据从Excel / CSV文件导入到angularjs json对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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