테이블 위젯 이름은 tbl_result 라고 가정한다.
[code]
import pandas as pd
class MyWindow(QMainWindow, form_class):
….
def exportToFile(self, type, filename):
columnHeaders = []
# create column header list
for j in range(self.tbl_result.model().columnCount()):
columnHeaders.append(
self.tbl_result.horizontalHeaderItem(j).text())
df = pd.DataFrame(columns=columnHeaders)
# create dataframe object recordset
for row in range(self.tbl_result.rowCount()):
for col in range(self.tbl_result.columnCount()):
df.at[row, columnHeaders[col]] = self.tbl_result.item(
row, col).text()
def download(self, type):
if type == ‘excel’:
df.to_excel(f”{filename}.xlsx”, index=False)
elif type == ‘csv’:
df.to_csv(f”{filename}.csv”, index=False)
[/code]