思路:
1.知道文件路径;
2.打开excel文件;
3.根据sheet,row,cell_value获取值,创建的数据对象中;
4.把创建的数据对象,追加到list列表中;
5.输出到打印台上;
实例1:
创建一个基础数据类用于存放对象数据
#基础数据信息类
class StudentBaseInfo():
def __init__(self,stu_id,stu_name,stu_sex,stu_age,stu_score):
self.stu_id = stu_id
self.stu_name = stu_name
self.stu_sex = stu_sex
self.stu_age = stu_age
self.stu_score = stu_score
def show(self):
pass
实例2:
打开excel文件,并获取数据,util类。
import os
import xlrd
from excel_read.student_base_info import StudentBaseInfo
def read_excel_get_studentinfo(file_path):
workbook = xlrd.open_workbook(file_path)
sheet = workbook.sheet_by_index(0)
student_infos=[]
for i in range(1,sheet.nrows):
new01 = StudentBaseInfo(sheet.cell_value(i,0),
sheet.cell_value(i,1),
sheet.cell_value(i,2),
sheet.cell_value(i,3),
sheet.cell_value(i,4))
student_infos.append(new01)
return student_infos
if __name__ == '__main__':
current_path = os.path.dirname(__file__)
excel_path = current_path + '/../data/stu.xls'
stus = read_excel_get_studentinfo(excel_path)
print(stus[0].stu_name)
实例3:
调用封装方法
from excel_read import excel_utils
import os
current_path = os.path.dirname(__file__)
excel_path = current_path + '/../data/stu.xls'
stus = excel_utils.read_excel_get_studentinfo(excel_path)
print(stus[0].stu_name)