2009年10月19日星期一

POI设定列宽度

如果想在工作表里指定列宽度的话,可以使用「HSSFSheet」类的「setColumnWidth」方法。


setColumnWidth
public void setColumnWidth(short column, short width)
set the width (in units of 1/256th of a character width) 
 
Parameters:
  column - - the column to set (0-based)
  width - - the width in units of 1/256th of a character width

指定列的序列号和宽度。宽度如果指定1的话,那就是一个文字的1/256,4个文字的宽度是1024。


另外,要取得列宽度可以使用「HSSFSheet」类的「getColumnWidth」方法。


getColumnWidth
public short getColumnWidth(short column)
get the width (in units of 1/256th of a character width ) 
 
Parameters:
  column - - the column to set (0-based) 
Returns:
  width - the width in units of 1/256th of a character width

指定列的序列号


示例程序


自己动手做一下吧。仍然按照下图准备一个Excel文件,取得从第0列到第2列的列宽、然后把第2列的列宽指定给第0列和第1列。


指定列宽


import java.io.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
 
public class POISample{
  public static void main(String[] args){
    FileInputStream in = null;
    HSSFWorkbook workbook = null;
 
    try{
      in = new FileInputStream("sample.xls");
      POIFSFileSystem fs = new POIFSFileSystem(in);
      workbook = new HSSFWorkbook(fs);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try{
        in.close();
      }catch (IOException e){
        System.out.println(e.toString());
      }
    }
 
    HSSFSheet sheet = workbook.getSheetAt(0);
 
    short[] width = new short[3];
 
    for (int i = 0 ; i < 3 ; i++){
      width[i] = sheet.getColumnWidth((short)i);
      System.out.println(i + "列宽度:" + width[i]);
    }
 
    sheet.setColumnWidth((short)0, width[2]);
    sheet.setColumnWidth((short)1, width[2]);
 
    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample2.xls");
      workbook.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }
}

打开新生成的Excel文件看看。


0列宽度:1865
1列宽度:3986
2列宽度:2048

指定列宽


一个Excel文件新生成时,有自己默认的列宽度,当然我们也可以用POI来指定默认的列宽度。


指定默认的列宽度


指定默认的列宽度用「HSSFSheet」类的「setDefaultColumnWidth」方法。


setDefaultColumnWidth
public void setDefaultColumnWidth(short width)
set the default column width for the sheet (if the columns do not define 
their own width) in characters 
 
Parameters:
  width - default column width

这里要注意的就是,和刚才的方法不一样,这里的列宽度单位是1个文字,而不是刚才的一个文字的1/256。


要取得原来的默认列宽度,使用「getDefaultColumnWidth」方法。


getDefaultColumnWidth
public short getDefaultColumnWidth()
get the default column width for the sheet (if the columns do not define 
their own width) in characters 
 
Returns:
  default column width

这里的列宽度单位也是一个文字


示例程序


动手做做看吧。


import java.io.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
 
public class POISample{
  public static void main(String[] args){
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet();
 
    sheet.setDefaultColumnWidth((short)5);
 
    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample2.xls");
      workbook.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }
}

新生成的Excel文件打开来看看吧。


默认列宽度设定

POI合并单元格

现在再看看如果将指定的单元格进行合并操作。用POI进行合并操作,使用「HSSFSheet」类的「addMergedRegion」方法。


addMergedRegion
public int addMergedRegion(Region region)
adds a merged region of cells (hence those cells form one) 
 
Parameters:
  region - (rowfrom/colfrom-rowto/colto) to merge 
Returns:
  index of this region

合并范围必须使用「Region」类来指定,关于「Region」类的介绍如下。


「Region」类关系图


  • java.lang.Object
  • org.apache.poi.hssf.util.Region
  • public class Region extends java.lang.Object implements java.lang.Comparable

「Region」类的构造方法


Region
public Region(int rowFrom, short colFrom, int rowTo, short colTo)
--

指定范围时,从左上的单元格到右下的单元格指定,比方像下面这样。


Region(1, (short)1, 2, (short)3)

示例程序


动手做做看,还是使用原来的Excel文件。




把上图选中的单元格进行合并操作时,看下面的程序。


import java.io.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.util.Region;
 
public class POISample{
  public static void main(String[] args){
    FileInputStream in = null;
    HSSFWorkbook workbook = null;
 
    try{
      in = new FileInputStream("sample.xls");
      POIFSFileSystem fs = new POIFSFileSystem(in);
      workbook = new HSSFWorkbook(fs);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try{
        in.close();
      }catch (IOException e){
        System.out.println(e.toString());
      }
    }
 
    HSSFSheet sheet = workbook.getSheetAt(0);
 
    sheet.addMergedRegion(new Region(1, (short)1, 2, (short)3));
 
    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample2.xls");
      workbook.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }
}

把刚作成的新的Excel文件打开看看吧


POI调整工作表的显示比例

有时我们可能会手动去调整工作表Sheet的显示比例,用POI同样也能做到这一点。这时我们可以使用「HSSFSheet」类的「setZoom」方法。

setZoom
public void setZoom(int numerator, int denominator)
Sets the zoom magnication for the sheet. The zoom is expressed as a 
fraction. For example to express a zoom of 75% use 3 for the 
numerator and 4 for the denominator. 
 
Parameters:
  numerator - The numerator for the zoom magnification.
  denominator - The denominator for the zoom magnification.

这样就可以指定显示比例了,指定方法是用"numerator"÷"denominator",比方说,「setZoom(2, 1)」就是设定为200%的比例,「setZoom(3, 4)」就是设定为75%的比例。

示例程序

动手做一下吧。


import java.io.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
 
public class POISample{
  public static void main(String[] args){
    FileInputStream in = null;
    HSSFWorkbook workbook = null;
 
    try{
      in = new FileInputStream("sample.xls");
      POIFSFileSystem fs = new POIFSFileSystem(in);
      workbook = new HSSFWorkbook(fs);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try{
        in.close();
      }catch (IOException e){
        System.out.println(e.toString());
      }
    }
 
    HSSFSheet sheet = workbook.getSheetAt(0);
 
    sheet.setZoom(2, 1);
 
    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample2.xls");
      workbook.write(out);
    }catch(IOException e){
      System.out.println(e.toString());
    }finally{
      try {
        out.close();
      }catch(IOException e){
        System.out.println(e.toString());
      }
    }
  }
}

新生成的Excel文件如下图所示。