设置Apache POI中的列宽 [英] Setting Column width in Apache POI

查看:621
本文介绍了设置Apache POI中的列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中编写一个工具,使用Apache POI API将XML转换为MS Excel。在我的XML输入中,我以点为单位接收列宽。但是,Apache POI API具有稍微奇怪的逻辑,用于根据字体大小等设置列宽(参见 API文档



是否有一个公式将点转换为Excel预期的宽度?有没有人这样做?



有一个 setRowHeightInPoints()方法虽然:(但没有列。 / p>

PS:输入的XML是ExcelML格式,我必须转换为MS Excel。

解决方案

不幸的是,只有函数 class Sheet 的%29rel =noreferrer> setColumnWidth(int columnIndex,
int width)
;其中宽度是多个字符在标准字体(工作簿中的第一个字体)中,如果您的字体正在更改,则无法使用。
解释了如何计算字体大小的函数宽度,公式为:$ {pre $ p $ width = 256

您可以随时使用 autoSizeColumn(int column,boolean useMergedCells) 输入表格中的数据



希望有帮助。 / p>

I am writing a tool in Java using Apache POI API to convert an XML to MS Excel. In my XML input, I receive the column width in points. But the Apache POI API has a slightly queer logic for setting column width based on font size etc. (refer API docs)

Is there a formula for converting points to the width as expected by Excel? Has anyone done this before?

There is a setRowHeightInPoints() method though :( but none for column.

P.S.: The input XML is in ExcelML format which I have to convert to MS Excel.

解决方案

Unfortunately there is only the function setColumnWidth(int columnIndex, int width) from class Sheet; in which width is a number of characters in the standard font (first font in the workbook) if your fonts are changing you cannot use it. There is explained how to calculate the width in function of a font size. The formula is:

width = Truncate([{NumOfVisibleChar} * {MaxDigitWidth} + {5PixelPadding}] / {MaxDigitWidth}*256) / 256

You can always use autoSizeColumn(int column, boolean useMergedCells) after inputting the data in your Sheet.

Hope it helped.

这篇关于设置Apache POI中的列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆