查看: 502|回复: 0

[文章教程] JQuery读取CSV文件范例

[复制链接]
xuanxiao 发表于 2022-6-9 14:59:11 | 显示全部楼层 |阅读模式
建议读取的时候将csv文件编码转换为UTF-8
  1.    
  2. <script src="ttasp/lib/js/jquery.min.js" ></script>
  3. <script src="ttasp/lib/js/jquery.csv.js" ></script>

  4. var csv_file_API = '/202269102423.csv';
  5.     $.ajax({
  6.         type: 'GET',
  7.         url: csv_file_API,
  8.         dataType: 'text',
  9.         success: function (data) {

  10.                         var data=$.csv.toObjects(data);
  11.                        
  12.                         var theHtml=createTable(data);
  13.                          
  14.                         $('#theResult').html(theHtml);
  15.                        
  16.         }, // end: Ajax success API call
  17.         error: function (e) {
  18.             alert('An error occurred while processing API calls');
  19.             console.log("API call Failed: ", e);
  20.         },

  21.     });
  22.        
  23. function createTable(data){
  24.   var html='<table border=1>';
  25.   if(data[0].constructor===Object){
  26.     html+='<tr>\r\n';
  27.     for(var item in data[0]){
  28.       html+='<th>'+item+'</th>\r\n';
  29.     }
  30.     html+='</tr>\r\n';
  31.     for(var row in data){
  32.       html+='<tr>\r\n';
  33.       for(var item in data[row]){
  34.         html+='<td>'+data[row][item]+'</td>\r\n';
  35.       }
  36.       html+='</tr>\r\n';
  37.     }
  38.   }

  39.   return html;
  40. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表

在线客服

售前咨询
售后咨询
服务热线
023-58418553
微信公众号