添加读取excel文件功能
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import sys
|
||||
from PySide6 import QtCore, QtGui, QtWidgets
|
||||
from PySide6.QtGui import QStandardItemModel, QStandardItem
|
||||
from PySide6.QtUiTools import QUiLoader
|
||||
from pathlib import Path
|
||||
from PySide6.QtWidgets import QFileDialog, QMainWindow, QMessageBox, QLabel
|
||||
from PySide6.QtWidgets import QFileDialog, QMainWindow, QMessageBox, QLabel, QComboBox, QVBoxLayout, QSizePolicy, \
|
||||
QTableWidgetItem
|
||||
import pandas as pd
|
||||
|
||||
class MainWindows(QMainWindow):
|
||||
@@ -34,18 +36,39 @@ class MainWindows(QMainWindow):
|
||||
#使用pandas读取EXCEL文件
|
||||
try:
|
||||
# 读取第一行表头
|
||||
df = pd.read_excel(file_path, nrows=0) # 不加载数据,只加载列名
|
||||
df = pd.read_excel(file_path, nrows=10) # 不加载数据,只加载列名
|
||||
headers = list(df.columns)
|
||||
except Exception as e:
|
||||
QMessageBox.critical(self,"错误",f"读取文件失败:\n{e}")
|
||||
return
|
||||
|
||||
# 2. 建 model
|
||||
model = QStandardItemModel(self)
|
||||
model.setRowCount(df.shape[0])
|
||||
model.setColumnCount(df.shape[1])
|
||||
model.setHorizontalHeaderLabels(df.columns)
|
||||
# 3. 填单元格
|
||||
for r, row in df.iterrows():
|
||||
for c, val in enumerate(row):
|
||||
model.setItem(r, c, QStandardItem(str(val)))
|
||||
|
||||
# 4. 挂到视图
|
||||
self.ui.tableView.setModel(model)
|
||||
|
||||
|
||||
|
||||
#动态创建标签
|
||||
print(headers)
|
||||
for col in headers:
|
||||
# 生成每个LABEL标签的内容
|
||||
lbl = QLabel(f"· {col}")
|
||||
self.ui.addWidget()
|
||||
# 在窗体中的垂直布局内放置标签
|
||||
self.ui.verticalLayout_2.addWidget(lbl)
|
||||
|
||||
self.ui.comboBox.addItems(headers)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
UI_FILE = "uv/main_window.ui"
|
||||
app = QtWidgets.QApplication(sys.argv)
|
||||
# 设置应用程序样式
|
||||
app.setStyle("Fusion")
|
||||
|
||||
Reference in New Issue
Block a user