java生成Excel(JXL类库)(二)

2014-11-24 07:34:30 · 作者: · 浏览: 3
0 );
// 合并第一列第一行到第六列第一行的所有单元格
sheet.mergeCells( 0 , 0 , 5 , 0 );
合并既可以是横向的,也可以是纵向的。合并后的单元格不能再次进行合并,否则会触发异常。
2、 行高和列宽
WritableSheet.setRowView( int i, int height);
// 作用是指定第i+1行的高度,比如:
// 将第一行的高度设为200
sheet.setRowView( 0 , 200 );
WritableSheet.setColumnView( int i, int width);
// 作用是指定第i+1列的宽度,比如:
// 将第一列的宽度设为30
sheet.setColumnView( 0 , 30 );
jExcelAPI还有其他的一些功能,比如插入图片等,这里就不再一一介绍,读者可以自己探索。其中:如果读一个excel,需要知道它有多少行和多少列,如下操作:
Workbook book = Workbook.getWorkbook( new File( " 测试1.xls " ));
// 获得第一个工作表对象
Sheet sheet = book.getSheet( 0 );
// 得到第一列第一行的单元格
int columnum = sheet.getColumns(); // 得到列数
int rownum = sheet.getRows(); // 得到行数
System.out.println(columnum);
System.out.println(rownum);
for ( int i = 0 ; i < rownum; i ++ ) // 循环进行读写
{
for ( int j = 0 ; j < columnum; j ++ ) {
Cell cell1 = sheet.getCell(j, i);
String result = cell1.getContents();
System.out.print(result);

摘自 windlaughing