看下我写的这段代码有什么改进之处。
class xlsdocument(): __head=""" <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=gb2312"> <meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 11"> <style>%s</style> </head> <body> <table>%s</table> </body> </html> """ class table(): def __init__(self): self.rows=[] class row(): class cell(): def __init__(self,style="",value="",width=0,height=0,align="",klass="",colspan=0,rowspan=0): self.style=style self.value=value self.width=width self.height=height self.align=align self.klass=klass self.colspan=colspan self.rowspan=rowspan def __init__(self,style="",width=0,height=0,align="",klass=""): self.style=style self.width=width self.height=height self.align=align self.klass=klass self.cells=[] __html="" __tr="<tr%s>%s</tr>" __td="<td%s>%s</td>" def __init__(self): self.table=self.table() def getHtml(self): for r in self.table.rows: assert isinstance(r,self.table.row)==True,"rows.append时的类型非table.row类型。" style="" width="" height="" align="" klass="" if r.style: style=" style='%s'"%r.style if r.width: width=" width=%s"%r.width if r.height: height=" height=%s"%r.height if r.align: align=" align=%s"%r.align if r.klass: klass=" klass=%s"%r.klass attrbute="%s%s%s%s%s"%(style,width,height,align,klass) self.__html+=self.__tr%(attrbute,"%s") tmp="" for c in r.cells: style="" width="" height="" align="" klass="" colspan="" rowspan="" if c.style: style=" style='%s'"%c.style if c.width: width=" width=%s"%c.width if c.height: height=" height=%s"%c.height if c.align: align=" align=%s"%c.align if c.klass: klass=" klass=%s"%c.klass if c.colspan: colspan=" colspan=%s"%c.colspan if c.rowspan: rowspan=" rowspan=%s"%c.rowspan attrbute="%s%s%s%s%s%s%s"%(style,width,height,align,klass,colspan,rowspan) tmp+=self.__td%(attrbute,c.value) self.__html=self.__html%(tmp) return self.__head%('',self.__html)#测试doc=xlsdocument()for r in range(10): row=xlsdocument.table.row() row.width=10 row.height=20 for i in range(10): tmp=xlsdocument.table.row.cell() tmp.value=i tmp.width=200 row.cells.append(tmp) doc.table.rows.append(row)#doc.rows.append("asdf") #异常测试print doc.getHtml()