pyqt怎么把QTablewidget的内容另存为为Excel表格
下面是我现在写的代码def _save(self):
savefile_name = QFileDialog.getSaveFileName(self,'选择保存路径','','Excel files(*.xlsx)')
global path_savefile_name
path_savefile_name = savefile_name
row = self.tableWidget.rowCount()
col = self.tableWidget.columnCount()
for i in range(row):
for j in range(col):
print(self.tableWidget.item(i,j).text())
请问一下,接下来怎么操作,才能把QTablewidget里面的内容另存为到excel表格中! def _save(self):
savefile_name = QFileDialog.getSaveFileName(self,'选择保存路径','','Excel files(*.xlsx)')
global path_savefile_name
path_savefile_name = savefile_name
row = self.tableWidget.rowCount()
col = self.tableWidget.columnCount()
book = xlwt.Workbook()
sheet = book.add_sheet('sheet1')
for i in range(row):
for j in range(col):
sheet.write(i, j, self.tableWidget.item(i,j).text())
# print(self.tableWidget.item(i,j).text())
book.save(savefile_name)
你试试 lirenbing01 发表于 2020-9-8 15:18
你试试
这个我写出来,但是表头我还没有加进去。。。 lirenbing01 发表于 2020-9-8 15:18
你试试
你用xlwt函数的时候,excel表格的后缀不能是.xlsx lirenbing01 发表于 2020-9-8 15:18
你试试
我写出来了,虽然写的不咋地
def _save(self):
savefile_name = QFileDialog.getSaveFileName(self,'选择保存路径','','Excel files(*.xls)')
global path_savefile_name
path_savefile_name = savefile_name
book = xlwt.Workbook()
sheet = book.add_sheet('新数据')
row = self.tableWidget.rowCount()
col = self.tableWidget.columnCount()
content = []
for i in range(col):
#self.tableWidget.horizontalHeaderItem(m).text()
content.append(self.tableWidget.horizontalHeaderItem(i).text())
#print(content)
for i in range(1):
for j in range(col):
sheet.write(i, j, content)
for i in range(row):
for j in range(col):
try:
sheet.write(i+1, j, self.tableWidget.item(i, j).text())
except:
continue
#print(self.tableWidget.item(i,j).text())
book.save(path_savefile_name)
页:
[1]