显示excel内容的回调函数 def show_excel(self): style = ttk.Style() style.configure("MyTreeview1.Treeview", rowheight=25, borderwidth=2, relief="solid", font=('Arial', 10)) style.configure("MyTreeview1.Treeview.Heading", font=('Arial', 10, 'bold')) style.layout("MyTreeview1.Treeview", [('MyTreeview1.Treeview.treearea', {'sticky': 'nswe'})]) # 创建表格用于显示第一个工作表 columns1 = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True)) treeview1 = ttk.Treeview(self.container1, columns=columns1, show="headings", style="MyTreeview1.Treeview") treeview1.grid(row=1, column=3, rowspan=1, padx=5, pady=5, sticky="nsew") # 设置表格列的标题和宽度 for col in columns1: treeview1.heading(col, text=col) treeview1.column(col, width=100, anchor="center") # 显示第一个工作表的内容 for row in self.record_sheet.iter_rows(min_row=2, values_only=True): row_values = [cell if cell is not None else "" for cell in row] if all(not bool(cell) for cell in row_values): continue treeview1.insert("", tk.END, values=row_values) columns2 = next(self.data_sheet.iter_rows(min_row=1, max_row=1, values_only=True)) treeview2 = ttk.Treeview(self.container1, columns=columns2, show="headings") treeview2.grid(row=3, column=3, padx=5, pady=5, sticky="nsew") # 设置表格列的标题和宽度 for col in columns2: treeview2.heading(col, text=col) treeview2.column(col, width=100, anchor="center") # 显示第二个工作表的内容 for row in self.data_sheet.iter_rows(min_row=2, values_only=True): row_values = [cell if cell is not None else "" for cell in row] if all(not bool(cell) for cell in row_values): continue treeview2.insert("", tk.END, values=row_values)修改這個代碼將顯示數據時不要顯示公式,請顯示最後的結果
时间: 2024-03-16 13:44:46 浏览: 54
可以在显示工作表内容的 for 循环中,针对数据类型为公式的单元格,使用 cell.value 获取计算后的结果,而不是 cell 获取公式本身,从而显示最终结果。修改后的代码如下:
```python
def show_excel(self):
style = ttk.Style()
style.configure("MyTreeview1.Treeview", rowheight=25, borderwidth=2, relief="solid", font=('Arial', 10))
style.configure("MyTreeview1.Treeview.Heading", font=('Arial', 10, 'bold'))
style.layout("MyTreeview1.Treeview", [('MyTreeview1.Treeview.treearea', {'sticky': 'nswe'})])
# 创建表格用于显示第一个工作表
columns1 = next(self.record_sheet.iter_rows(min_row=1, max_row=1, values_only=True))
treeview1 = ttk.Treeview(self.container1, columns=columns1, show="headings", style="MyTreeview1.Treeview")
treeview1.grid(row=1, column=3, rowspan=1, padx=5, pady=5, sticky="nsew")
# 设置表格列的标题和宽度
for col in columns1:
treeview1.heading(col, text=col)
treeview1.column(col, width=100, anchor="center")
# 显示第一个工作表的内容
for row in self.record_sheet.iter_rows(min_row=2, values_only=True):
row_values = []
for cell in row:
if cell is None:
row_values.append("")
elif cell.data_type == 'f':
row_values.append(cell.value)
else:
row_values.append(cell.value)
if all(not bool(cell) for cell in row_values):
continue
treeview1.insert("", tk.END, values=row_values)
# 创建表格用于显示第二个工作表
columns2 = next(self.data_sheet.iter_rows(min_row=1, max_row=1, values_only=True))
treeview2 = ttk.Treeview(self.container1, columns=columns2, show="headings")
treeview2.grid(row=3, column=3, padx=5, pady=5, sticky="nsew")
# 设置表格列的标题和宽度
for col in columns2:
treeview2.heading(col, text=col)
treeview2.column(col, width=100, anchor="center")
# 显示第二个工作表的内容
for row in self.data_sheet.iter_rows(min_row=2, values_only=True):
row_values = []
for cell in row:
if cell is None:
row_values.append("")
elif cell.data_type == 'f':
row_values.append(cell.value)
else:
row_values.append(cell.value)
if all(not bool(cell) for cell in row_values):
continue
treeview2.insert("", tk.END, values=row_values)
```
阅读全文