<small id='9TK0D'></small><noframes id='9TK0D'>

        <tfoot id='9TK0D'></tfoot>
      1. <i id='9TK0D'><tr id='9TK0D'><dt id='9TK0D'><q id='9TK0D'><span id='9TK0D'><b id='9TK0D'><form id='9TK0D'><ins id='9TK0D'></ins><ul id='9TK0D'></ul><sub id='9TK0D'></sub></form><legend id='9TK0D'></legend><bdo id='9TK0D'><pre id='9TK0D'><center id='9TK0D'></center></pre></bdo></b><th id='9TK0D'></th></span></q></dt></tr></i><div id='9TK0D'><tfoot id='9TK0D'></tfoot><dl id='9TK0D'><fieldset id='9TK0D'></fieldset></dl></div>
        <legend id='9TK0D'><style id='9TK0D'><dir id='9TK0D'><q id='9TK0D'></q></dir></style></legend>
          <bdo id='9TK0D'></bdo><ul id='9TK0D'></ul>

        PyQt:QTableWidget到.xls文件

        时间:2024-08-11
          <bdo id='yoIJb'></bdo><ul id='yoIJb'></ul>
            <i id='yoIJb'><tr id='yoIJb'><dt id='yoIJb'><q id='yoIJb'><span id='yoIJb'><b id='yoIJb'><form id='yoIJb'><ins id='yoIJb'></ins><ul id='yoIJb'></ul><sub id='yoIJb'></sub></form><legend id='yoIJb'></legend><bdo id='yoIJb'><pre id='yoIJb'><center id='yoIJb'></center></pre></bdo></b><th id='yoIJb'></th></span></q></dt></tr></i><div id='yoIJb'><tfoot id='yoIJb'></tfoot><dl id='yoIJb'><fieldset id='yoIJb'></fieldset></dl></div>

              <small id='yoIJb'></small><noframes id='yoIJb'>

              <tfoot id='yoIJb'></tfoot>

                <legend id='yoIJb'><style id='yoIJb'><dir id='yoIJb'><q id='yoIJb'></q></dir></style></legend>

                    <tbody id='yoIJb'></tbody>
                • 本文介绍了PyQt:QTableWidget到.xls文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  因此,我有一个QTableWidget,我想使用xlwt模块将其保存到.xls文件中.

                  代码如下:

                  def savefile(self):
                          filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))    
                          wbk = xlwt.Workbook()
                          self.sheet = wbk.add_sheet("sheet")
                          self.row = 0
                          self.col = 0
                          self.add2(self.row, self.col)
                          wbk.save(filename)    
                  
                  
                  def add2(self, row, col):
                      for i in range(self.tableWidget.columnCount()):
                          for x in range(self.tableWidget.rowCount()):
                              try:
                                  teext = str(self.tableWidget.item(row, col).text())
                                  self.sheet.write(row, col, teext)
                                  row += 1
                              except AttributeError:
                                  pass                   
                          col += 1
                  

                  但这只写出单元格0,0中的文本,不写出其他内容.

                  我想我犯了一些严重的错误…

                  更新:

                  def savefile(self):
                          filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))    
                          wbk = xlwt.Workbook()
                          self.sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
                          self.add2()
                          wbk.save(filename)    
                  
                  
                  def add2(self):
                      row = 0
                      col = 0         
                      for i in range(self.tableWidget.columnCount()):
                          for x in range(self.tableWidget.rowCount()):
                              try:             
                                  teext = str(self.tableWidget.item(row, col).text())
                                  self.sheet.write(row, col, teext)
                                  row += 1
                              except AttributeError:
                                  row += 1
                          row = 0
                          col += 1             
                  

                  已解决问题.

                  推荐答案

                  您可能还会发现,使用范围(或xrange)的输出作为tableWidget.Item调用的索引更简洁、更容易,而不是担心增加您自己的计数器。您可能会在代码的其他位置使用工作表本身,但是如果您没有这样做,那么不将工作表指定为您的类的属性变量会节省一些内存:

                  def savefile(self):
                      filename = unicode(QtGui.QFileDialog.getSaveFileName(self, 'Save File', '', ".xls(*.xls)"))    
                      wbk = xlwt.Workbook()
                      sheet = wbk.add_sheet("sheet", cell_overwrite_ok=True)
                      self.add2(sheet)
                      wbk.save(filename)
                  
                  def add2(self, sheet):
                      for currentColumn in range(self.tableWidget.columnCount()):
                          for currentRow in range(self.tableWidget.rowCount()):
                              try:
                                  teext = str(self.tableWidget.item(currentRow, currentColumn).text()
                                  sheet.write(currentRow, currentColumn, teext)
                              except AttributeError:
                                  pass
                  

                  因为您使用的是Range命令,currentColumn变量将从0递增到column nCount(),currentRow将从0递增到currentRow()

                  这篇关于PyQt:QTableWidget到.xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PyQt中sys.exit(app.exec_())的必要性是什么? 下一篇:PyQt:我可以制作一个带有图标和文本的工具栏按钮吗?

                  相关文章

                  <tfoot id='sEwPy'></tfoot>

                  <small id='sEwPy'></small><noframes id='sEwPy'>

                • <legend id='sEwPy'><style id='sEwPy'><dir id='sEwPy'><q id='sEwPy'></q></dir></style></legend>
                • <i id='sEwPy'><tr id='sEwPy'><dt id='sEwPy'><q id='sEwPy'><span id='sEwPy'><b id='sEwPy'><form id='sEwPy'><ins id='sEwPy'></ins><ul id='sEwPy'></ul><sub id='sEwPy'></sub></form><legend id='sEwPy'></legend><bdo id='sEwPy'><pre id='sEwPy'><center id='sEwPy'></center></pre></bdo></b><th id='sEwPy'></th></span></q></dt></tr></i><div id='sEwPy'><tfoot id='sEwPy'></tfoot><dl id='sEwPy'><fieldset id='sEwPy'></fieldset></dl></div>

                        <bdo id='sEwPy'></bdo><ul id='sEwPy'></ul>