如果想在工作表里指定列宽度的话,可以使用「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文件打开来看看吧。







