如何在不删除其列和数据的情况下将Excel文件添加到QTableWidget?

发布于 2025-02-11 17:39:19 字数 1045 浏览 3 评论 0原文

如何将Excel数据加载到QTableWidget? 之后出现。

我希望它在最后一个收件人数据

“ https://i.sstatic.net/bpjad.png” alt =“这是我导入我的excel文件的时候。”>

这是我使用的代码:

 def addExcel(self, excel_file_dir, worksheet_name):
    df = pd.read_excel(excel_file_dir, worksheet_name)
    if df.size == 0:
            return 

    df.fillna('', inplace=True)
    self.tableWidget.setRowCount(df.shape[0])
    self.tableWidget.setColumnCount(df.shape[1])

    for row in df.iterrows():
            values = row[1]
            for col_index, value in enumerate(values):
                    if isinstance(value, (float, int)):
                            value = '{0:0,.0f}'.format(value)
                    tableItem = QTableWidgetItem(str(value))
                    self.tableWidget.setItem(row[0], col_index, tableItem)

How do i load excel data to QTableWidget? I want it to appear after the last recipient data

this is my columns and data

This is when i import my excel file.

This is the code i used:

 def addExcel(self, excel_file_dir, worksheet_name):
    df = pd.read_excel(excel_file_dir, worksheet_name)
    if df.size == 0:
            return 

    df.fillna('', inplace=True)
    self.tableWidget.setRowCount(df.shape[0])
    self.tableWidget.setColumnCount(df.shape[1])

    for row in df.iterrows():
            values = row[1]
            for col_index, value in enumerate(values):
                    if isinstance(value, (float, int)):
                            value = '{0:0,.0f}'.format(value)
                    tableItem = QTableWidgetItem(str(value))
                    self.tableWidget.setItem(row[0], col_index, tableItem)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2025-02-18 17:39:19

作为

将该表模型中的行数设置为行。如果这小于RowCount(),则丢弃了不需要的行中的数据。

对于setColumnCount(),也显然也是如此。

如果要保留现有数据,则显然必须根据新数据添加

考虑到数据模型通常会在保留列时增加记录的数量( lows ),您必须通过增加当前来调用setRowCount()行计算新记录的数量,并根据现有列计数和检索到的列之间的最大值设置列计数。

def addExcel(self, excel_file_dir, worksheet_name):
    df = pd.read_excel(excel_file_dir, worksheet_name)
    if df.size == 0:
            return 

    df.fillna('', inplace=True)

    newFirstRow = self.tableWidget.rowCount()
    self.tableWidget.setRowCount(newFirstRow + df.shape[0])
    self.tableWidget.setColumnCount(
        max(self.tableWidget.columnCount(), df.shape[1]))

    for rowData in df.iterrows():
        row = newFirstRow + rowData[0]
        values = rowData[1]
        for col_index, value in enumerate(values):
            if isinstance(value, (float, int)):
                value = '{0:0,.0f}'.format(value)
                tableItem = QTableWidgetItem(str(value))
                self.tableWidget.setItem(row, col_index, tableItem)

注意:'{0:0,.0f}'。格式(value)在大多数情况下是毫无意义的:您只需使用str(round> str(round(value))。

As the documentation of setRowCount() explains:

Sets the number of rows in this table's model to rows. If this is less than rowCount(), the data in the unwanted rows is discarded.

The same clearly goes for setColumnCount() as well.

If you want to preserve existing data, you obviously have to add the row and column count based on the new data.

Considering that data models usually increase the number of records (rows) while keeping the columns, you have to call setRowCount() by increasing the current row count by the number of new records, and set the column count based on the maximum between the existing column count and the retrieved one.

def addExcel(self, excel_file_dir, worksheet_name):
    df = pd.read_excel(excel_file_dir, worksheet_name)
    if df.size == 0:
            return 

    df.fillna('', inplace=True)

    newFirstRow = self.tableWidget.rowCount()
    self.tableWidget.setRowCount(newFirstRow + df.shape[0])
    self.tableWidget.setColumnCount(
        max(self.tableWidget.columnCount(), df.shape[1]))

    for rowData in df.iterrows():
        row = newFirstRow + rowData[0]
        values = rowData[1]
        for col_index, value in enumerate(values):
            if isinstance(value, (float, int)):
                value = '{0:0,.0f}'.format(value)
                tableItem = QTableWidgetItem(str(value))
                self.tableWidget.setItem(row, col_index, tableItem)

Note: '{0:0,.0f}'.format(value) is quite pointless in most cases: you can just use str(round(value)) instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文