首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在xlwt中编写包含多列的单元格?

如何在xlwt中编写包含多列的单元格?
EN

Stack Overflow用户
提问于 2013-10-30 10:28:47
回答 1查看 29.7K关注 0票数 25

我想写一个这样的表:

代码语言:javascript
复制
----------------
| Long Cell    |
----------------
| 1    | 2     |
----------------

如何编写单元格Long Cell?谢谢。

我试着这样做:

代码语言:javascript
复制
sheet.write(0, 0, 'Long Cell')
sheet.write(1, 0, 1)
sheet.write(1, 1, 2)

但它最终是这样的:

代码语言:javascript
复制
--------------------
| Long Cell |      |
--------------------
| 1         | 2    |
--------------------
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-30 11:26:35

据我所知,这是没有文档记录的-你必须阅读源代码才能找到它。Worksheet类上有两个方法可以完成此操作:write_mergemergemerge接受现有的单元格并合并它们,而write_merge编写一个标签(就像write一样),然后做与merge相同的事情。

两者都接受要合并的单元格作为r1, r2, c1, c2,并接受一个可选的style参数。

在您的示例中,这将是最简单的调用:

代码语言:javascript
复制
sheet.write_merge(0, 0, 0, 1, 'Long Cell')
sheet.write(1, 0, 1)
sheet.write(1, 1, 2)

更明确地说,调用是如何工作的:

代码语言:javascript
复制
top_row = 0
bottom_row = 0
left_column = 0
right_column = 1
sheet.write_merge(top_row, bottom_row, left_column, right_column, 'Long Cell')

或者,使用merge

代码语言:javascript
复制
sheet.write(top_row, left_column, 'Long Cell')
sheet.merge(top_row, bottom_row, left_column, right_column)

merge在源代码中有一些评论,指出了潜在的问题:

# Problems: (1) style to be used should be existing style of # the top-left cell, not an arg. # (2) should ensure that any previous data value in # non-top-left cells is nobbled. # Note: if a cell is set by a data record then later # is referenced by a [MUL]BLANK record, Excel will blank # out the cell on the screen, but OOo & Gnu will not # blank it out. Need to do something better than writing # multiple records. In the meantime, avoid this method and use # write\_merge() instead.

但对于像这样的简单案例来说,这是可以接受的。

票数 66
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19672760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档