WebView Show CSV

Source: Internet
Author: User

Projects in iOS display XLS and xlsx easily, using the following code can be, but Android is more troublesome, in order to unify processing, select Excel save CSV to parse the display table.

Https://developer.apple.com/library/ios/qa/qa1630/_index.html    func loaddocument (documentname:string, Webview:uiwebview)    {let        path:string = Nsbundle.mainbundle (). Pathforresource (DocumentName, Oftype:nil)!;        Let Url:nsurl = Nsurl (fileurlwithpath:path) let        request:nsurlrequest = Nsurlrequest (url:url)        Webview.loadrequest (Request)    }

Using D3.js to parse the server's CSV file, but D3 parse out is the Key-value array, because the content is changed, so I directly use the return value, and then I parse

function response (Request) {        return request.responsetext;//dsv.parse (Request.responsetext);}

One problem is that if you don't handle it, the CSV file that is turned out is the ANSI encoded JS direct fetch is garbled, so use D3 when you need to set the following:

function Callpase () {            var csv = D3.DSV (",", "text/csv;charset=gb2312");            CSV ("Http://7xnhdv.com1.z0.glb.clouddn.com/test.csv", function (error,csvdata) {                gentable (csvdata);            });        
use GetTable method parsing to generate tables after getting data

function gentable (param) {        var isarray=param instanceof Array;        var html = "";        var data=null;        if (IsArray) {//array directly with            Data=param;        } Else{//csv strings are converted to arrays            data=new Array ();            var lines=param.split (' \ r \ n ');            for (Var i=0;i<lines.length;i++) {                var cells=lines[i].split (', ');                Data.push (cells);            }        }        var Table = document.getElementById ("TableId");        for (Var i=0;i<data.length;i++) {            var NewRow = Table.insertrow ();                        Add row            var cells=data[i];            for (Var j=0;j<cells.length;j++) {                var newcell= newrow.insertcell ();                newcell.innerhtml = Cells[j];                NewCell.style.height = "20px";}}    }

The effect is as follows:


Full HTML code:

<! DOCTYPE html>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

WebView Show CSV

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.