POI 中如何设置列宽?急问!

苏丹-陈 2004-09-12 07:02:50
我在JSP中使用POI生成Excel文件时,需要设置列宽。如果期望列宽17.15,或者5.13。
但是我再网上看了这个例子
sheet.setColumnWidth((short) i, (short) (261 * 25));
并且测试几个数据后,发现Excel中的列宽和这个函数设置的列宽参数并不成正比。
真是找不到其中的规律。希望大家帮帮吗。
谢谢各位了 !!!
...全文
1653 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
苏丹-陈 2004-09-22
  • 打赏
  • 举报
回复
s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
它为什么不直接写成
s.setColumnWidth( (short) 2, (short) ( 50 * 160 ) );
160为常量。要紧的是它不是常量关系。

黑马 2004-09-13
  • 打赏
  • 举报
回复
看看这段代码中的setColumnWidth方法吧:

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle();
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();

cs = wb.createCellStyle();

cs.setFont( f2 );
//Word Wrap MUST be turned on
cs.setWrapText( true );

r = s.createRow( (short) 2 );
r.setHeight( (short) 0x349 );
c = r.createCell( (short) 2 );
c.setCellType( HSSFCell.CELL_TYPE_STRING );
c.setCellValue( "Use \n with word wrap on to create a new line" );
c.setCellStyle( cs );
s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );

FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
wb.write( fileOut );
fileOut.close();
苏丹-陈 2004-09-13
  • 打赏
  • 举报
回复
大家帮帮码!!!

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧