使用python将excel文件转化为json文件。
Excel文件格式:
转换后的json文件格式:
在html页面使用ajax读取json文件,获取数据并使用:
<!DOCTYPE html>
<html>
<!-- Mirrored from www.hyattech.cn/data/upload/image/20220601/1654077224890216.html by HTTrack Website Copier/3.x [XR&CO'2014], Fri, 03 Nov 2023 13:36:47 GMT -->
<head>
<meta charset="utf-8"/>
<title>信息展示</title>
<script src="../../../../../assets.pyecharts.org/assets/echarts.min.js" type="text/javascript"></script>
<script src="../../../../../assets.pyecharts.org/assets/bmap.min.js" type="text/javascript"></script>
<script src="http://api.tianditu.gov.cn/api?v=4.0&tk=秘钥" type="text/javascript"></script>
<script type="text/javascript" src="http://api.tianditu.com/js/maps.js"></script>
<script src="http://t0.tianditu.gov.cn/vec_w/wmts?tk=秘钥" type="text/javascript"></script>
<script src="../14.5/static/js/jquery-3.6.4.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body style="background-color:#333333;"><div align="center" style="width:1920px;">
<span style="font-size:28px;font face='黑体';color:#F8F8F8"><b>2021吉林省图们市农村公路网信息展示</b></span></div><div style="font-size:16px;font face='黑体';color:#F8F8F8;width:1920px">
</div>
<style>.box { justify-content:center; display:flex; flex-wrap:wrap; }; </style>
<div class="box">
<br/> <div class="chart-container" id="791e3b8d32c34ee389ad805fcbd17f21" style="left:0px;top:120px;width:660px;height:606px;position:absolute"></div>
<script>
<!--定义地图基本信息-->
var map = new T.Map('791e3b8d32c34ee389ad805fcbd17f21');
var point = new T.LngLat(129.798891, 42.999593);
map.centerAndZoom(point, 10);
map.enableScrollWheelZoom(true);
$.ajax({
url:"./data2.json",
type:"get",
dataType:"json",
success:function(data){
var points = new Array();
var color = new Array();
var wInfo = new Array();
for (var i=0; i<data.length; i++) {
points[i]=data[i].line;
color[i]=data[i].color;
wInfo[i]=data[i].clickInfo;
}
console.log(points);
console.log(color);
console.log(wInfo);
for (var j = 0; j < points.length; j++) {
<!--颜色:-->
var opts={
color: color[j], //线的颜色
weight: 5, //线的宽度
opacity: 0.5, //线的透明度
lineStyle:"solid" //线的样式
};
<!--分解点一条线坐标线:-->
var arr = points[j].split(";")
var lineArr = [];
for (var i = 0; i < arr.length; i++) {
lineArr.push(new T.LngLat(arr[i].split(",")[0], arr[i].split(",")[1]));
}
<!--绘制:-->
var polyline = new T.Polyline(lineArr, opts);
map.addOverLay(polyline);
<!--注册点击事件-->
(function () {//重点在这里,闭包的作用体现出来了,不然,信息框一直指向最后一个坐标
var index = i;
var _iw = new T.InfoWindow(wInfo[j]);
polyline.addEventListener("click", function () {
this.openInfoWindow(_iw);
});
})()
}
},
});
</script>
<br/>
<style>
.fl-table_2 {
margin: 1dpx;
font-size: 9px;
word-wrap:break-word;
word-break:break-all;
}
.fl-table_2 tbody {
display: block;
height : 264px;
overflow-y: scroll;
overflow-x: hidden;
}
.fl-table_2 tbody tr {
display: table;
width: 660px;
table-layout: fixed;
}
.fl-table_2 thead tr{
display: table;
width: 660px;
table-layout: fixed;
text-align: center;
}
.fl-table_2 th {
font-size: 14px;
border-top: 1px solid #dbdbdb;
border-right: 1px solid #dbdbdb;
}
.fl-table_2 tr td {
border-right: 1px solid #dbdbdb;
text-align: center;
padding: .5em .55em;
}
.fl-table_2 tr:hover td {
background: #00d1b2;
color: #F8F8F8;
}
.fl-table_2 tr {
border-bottom: 1px solid #dbdbdb;
border-left: 1px solid #dbdbdb;
color:#F8F8F8
}
</style>
<div class="chart-container" id="492b8b83c0644ba49f715b7578c5749a" style="left:630px;top:732px;width:660px;height:348px;position:absolute">
<p class="title" style="font-size: 14px;color:#F8F8F8;font-weight:bold;"> 各路线技术状况评价结果</p>
<p class="subtitle" style="font-size: 12px;"> </p>
</div>
<br/> </div>
<script>
</script>
</body>
</html>
excel转python文件代码:
import sys
import xlrd
import json
# 打开 Excel 文件
workbook = xlrd.open_workbook('data.xls')
# 获取第一个工作表
worksheet = workbook.sheet_by_index(0)
# 创建一个空列表
data = []
# 遍历每一行
for row_index in range(1,worksheet.nrows):
# 读取每一行的数据
row_data = worksheet.row_values(row_index)
# 将读取到的数据添加到列表中
data.append(row_data)
# 将数据转换为 JSON 格式
json_data = json.dumps(data)
print("输出 JSON 格式的数据:")
# 输出 JSON 格式的数据
print(json_data)
print("excel转字典:")
# xlrd -- excel转字典
import xlrd
def get_data(sheet1):
data = xlrd.open_workbook('data.xls')
table = data.sheets()[0]
# print(table)
nor = table.nrows
nol = table.ncols
dict = {}
for i in range(1,nor):
for j in range(nol):
title = table.cell_value(0,j)
value = table.cell_value(i,j)
dict[title] = value
yield dict
if __name__ == '__main__':
for i in get_data(0):
print(i)
print("转json文件:")
import xlrd
import json
import sys
#读取Excel文件的sheet1
sheet = xlrd.open_workbook("data2.xls").sheets()[0]
#按行读取
rows = sheet.nrows
#保存关键字
keys = []
#保存结果
result = []
for i in range(rows):
if i == 0:
keys = sheet.row_values(i)#保存关键字
else:
record = {}
cnt = 0
# 将Excel文件的数据存入字典中
for item in sheet.row_values(i):
record[keys[cnt]] = item
cnt += 1
# 将字典存入列表
result.append(record)
# 重定向并输出json文件
with open("data2.json", "w+") as outputFile:
sys.stdout = outputFile
print(json.dumps(result, indent=4))
# print("存入集合中:")
# import numpy as np
# import xlrd #读取excel的库
# resArray=[] #先声明一个空list
# data = xlrd.open_workbook("demo.xlsx") #读取文件
# table = data.sheet_by_index(0) #按索引获取工作表,0就是工作表1
# for i in range(table.nrows): #table.nrows表示总行数
# line=table.row_values(i) #读取每行数据,保存在line里面,line是list
# resArray.append(line) #将line加入到resArray中,resArray是二维list
# resArray=np.array(resArray) #将resArray从二维list变成数组
# print(resArray)