废话不多说,看代码,不懂的留言。
from openpyxl import *
class ExcelUtil:
''' 读取excel文件内容'''
def create_work_book(self, title, index, maxRow, maxColumn, fileName):
"""
:param title: 表名称,
:param index: 表所处位置(第一位从0开始)
:param maxRow: 创建的最大行数
:param maxColumn: 创建的最大列数
:param maxColumn: 文件保存路劲
"""
wb = Workbook()
sheet = wb.create_sheet(title=title, index=index)
sheet.insert_rows(0, maxRow)
sheet.insert_cols(0, maxColumn)
wb.save(fileName)
def fill_cell(self, fileName, sheetName, cellIndex, value):
"""
:param fileName: 文件路径
:param sheetName: 表名称
:param cellIndex: 单元格所在位置,如'A1'
:param value: 写入单元格的值
"""
wb = load_workbook(fileName)
sheet = wb[sheetName]
sheet[cellIndex.upper()] = value
wb.save(fileName)
def read_cell(self, fileName, sheetName, cellIndex):
"""
:param fileName: 文件路径
:param sheetName: 表名称
:param cellIndex: 单元格所在位置,如'A1'
:return:单元格值
"""
wb = load_workbook(fileName)
sheet = wb[sheetName]
return sheet[cellIndex.upper()].value
def batch_read_cell(self, fileName, sheetName):
"""
:param fileName: 文件路径
:param sheetName: 表名
:return: 二维数组
"""
wb = load_workbook(fileName)
sheet = wb[sheetName]
lists = []
for row in sheet.rows:
list=[]
for cell in row:
list.append(cell.value)
lists.append(list)
return lists