澳门金莎娱乐手机版:所有的数据都在一个单元

日期:2019-12-04编辑作者:澳门金莎娱乐手机版

摘要: 最近有一个非项目的小需求,就是将项目开发分工文件化,方便后期管理维护。但是开发时,分工安排都是以json格式记录的,所以就做了一个将json数据以csv格式下载到本地。

datagrid导出excel格式时,所有的数据都在一个单元格怎么解决?
代码如下:
function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {

转自:

复制代码 代码如下:

//If JSONData is not an object then JSON.parse will parse the JSON string in an Object

function JSONToExcelConvertor(JSONData, FileName, ShowLabel) {
//先转化json
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

download csv

var arrData = typeof JSONData != 'object' ? JSON.parse

var excel = '<table>';

Enter JSON data

[{"Vehicle":"BMW","Date":"30 Jul 2013 09:24 AM","Location":"Hauz Khas","Speed":42},{"Vehicle":"Honda CBR","Date":"30 Jul 2013 12:00 AM","Location":"Military Road","Speed":0},{"Vehicle":"Supra","Date":"30 Jul 2013 07:53 AM","Location":"Sec-45","Speed":58},{"Vehicle":"Land Cruiser","Date":"30 Jul 2013 09:35 AM","Location":"DLF Phase I","Speed":83}]

Download CSV

download.js

复制代码 代码如下:$.ready { "use strict"; var mo = { init: function.click { var data = $; if { return; } mo.JSONToCSVConvertor; }, JSONToCSVConvertor: function { var arrData = typeof JSONData !== 'object' ? JSON.parse : JSONData; var CSV = ''; if { var row = ""; for (var index in arrData[0]) { row += index + ','; } row = row.slice; CSV += row + 'rn'; } for (var i = 0; i < arrData.length; i++) { var row = ""; for (var index in arrData[i]) { var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"'; row += arrValue + ','; } row.slice; CSV += row + 'rn'; } if { growl.error; return; } var fileName = "Result"; if { var IEwindow = window.open(); IEwindow.document.write; IEwindow.document.close(); IEwindow.document.execCommand('SaveAs', true, fileName + ".csv"); IEwindow.close(); } else { var uri = 'data:application/csv;charset=utf-8,' + escape; var link = document.createElement; link.href = uri; link.style = "visibility:hidden"; link.download = fileName + ".csv"; document.body.appendChild; document.body.removeChild; } }, msieversion: function() { var ua = window.navigator.userAgent; var msie = ua.indexOf; if (msie > 0 || !!navigator.userAgent.match // If Internet Explorer, return version number { return true; } else { // If another browser, return false; } return false; }, main: function; } }; mo.main;

小结: 注意json格式[{},{}],文件名是在js中定义的变量fileName。主要问题是他会自动添加一行空行,且每个元素都会在值前面加个'='。

下载下来的数据格式为:

: JSONData;

//设置表头
var row = "<tr>";
for (var i = 0, l = ShowLabel.length; i < l; i++) {
row += "<td>" + ShowLabel[i] + '</td>';
}

var CSV = '';

//换行
excel += row + "</tr>";

//Set Report title in first row or line

//设置数据
for (var i = 0; i < arrData.length; i++) {
var row = "<tr>";

//CSV += ReportTitle + 'rnn';

for (var index in arrData[i]) {
var value = arrData[i][index] === "." ? "" : arrData[i][index];
row += '<td>' + value + '</td>';
}

//This condition will generate the Label/Header

excel += row + "</tr>";
}

if (ShowLabel) {

excel += "</table>";

var row = "";

var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns=';";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>";

//This loop will extract the label from 1st index of on array

var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);

/*for ( var index in arrData[0]) {

var link = document.createElement("a");
link.href = uri;

//Now convert each value to string and comma-seprated

link.style = "visibility:hidden";
link.download = FileName + ".xls";

row += index + ',';

document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

} */
row = row.slice;

 

//append Label row with line break

//var datas=[];
//var grid=getCmp("grid");
//var grid_data = grid.getStore();

CSV += row + 'rn';

//for(var i= 0 ;i< grid_data.data.length;i++){ //获取extjs grid的数据
//var row = grid_data.getAt(i).data;
//datas.push({"列头1":row["OrgName"],...});
//}

}

var datas=[{key:value}......];

//1st loop is to extract each row

JSONToExcelConvertor(datas,"汇总",["列头1","头2","头3"]);

for (var i = 0; i < arrData.length; i++) {

 

var row = "";

3252DFE9-8E3E-4CC5-9414-F05F2E6447DA From: 

//2nd loop will extract each column and convert it in string comma-seprated

for ( var index in arrData[i]) {

row += '"' + arrData[i][index] + '",';

}

row=row.slice(0, row.length-1);

//add a line break after each row

CSV += row + 'rn';

}

if (CSV == '') {

alert("Invalid data");

return;

}

//Generate a file name

var fileName = "考勤结果";

//this will remove the blank-spaces from the title and replace it with an underscore

fileName += ReportTitle.replace(/ /g, "_");

//Initialize file format you want csv or xls

//alert;
var uri = 'data:text/csv;charset=utf-8,uFEFF' + encodeURI;

// Now the little tricky part.

// you can use either>> window.open;

// but this will not work in some browsers

// or you will not get the correct file extension

//this trick will generate a temp tag

var link = document.createElement;

link.href = uri;

//set the visibility hidden so it will not effect on your web-layout

link.style = "visibility:hidden";

link.download = fileName + ".xls";

//this part will append the anchor tag and remove it after automatic click

document.body.appendChild;

link.click();

document.body.removeChild;

}

本文由澳门金莎娱乐网站发布于澳门金莎娱乐手机版,转载请注明出处:澳门金莎娱乐手机版:所有的数据都在一个单元

关键词:

form表单通过ajax提交文件

前面分享了用js将json数据下载为csv文件,方便后期管理。但是对于测试人员更希望能够以页面的形式展现任务,所以...

详细>>

方法_基础知识_脚本之家,Math对象有关方法以及

JavaScript Math.floor 方法 Math.floor方法用于对数值向下取整,即获取小于或等于该数值的最大整数。语法如下: num++(后...

详细>>

cookie用法解析,方法的使用

jquery.cookie.js是一个轻量级的cookie插件,可以读取、写入、删除cookie。 jquery.cookie() 方法的使用(读取、写入、删除) j...

详细>>

in锚点的动态创建,收集整理的四个方向的滚动

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 向左滚动 测试状态栏字符的滚动 JavaScript 无缝左右滚动加定高定宽停...

详细>>