日期:2014-05-20  浏览次数:20795 次

请教各位高手用poi操作excel如何设置单元格大小?
如题!

------解决方案--------------------
5、设置列宽、行高 
sheet.setColumnWidth((short)column,(short)width);
row.setHeight((short)height);
render_code();
------解决方案--------------------
添加区域,合并单元格 
Region region = new Region((short)rowFrom,(short)columnFrom,(short)rowTo,(short)columnTo);
sheet.addMergedRegion(region);
//得到所有区域
sheet.getNumMergedRegions()
render_code();
------解决方案--------------------
常用单元格边框格式 
虚线HSSFCellStyle.BORDER_DOTTED 
实线HSSFCellStyle.BORDER_THIN 
代码public static HSSFCellStyle getCellStyle(short type)
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(type);//下边框
style.setBorderLeft(type);//左边框
style.setBorderRight(type);//右边框
style.setBorderTop(type);//上边框
return style;
}
render_code(); 
设置字体和内容位置 
代码HSSFFont f = wb.createFont();
f.setFontHeightInPoints((short) 11);//字号
f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
style.setRotation(short rotation);//单元格内容的旋转的角度
HSSFDataFormat df = wb.createDataFormat();
style1.setDataFormat(df.getFormat("0.00%"));//设置单元格数据格式
cell.setCellFormula(string);//给单元格设公式
style.setRotation(short rotation);//单元格内容的旋转的角度
cell.setCellStyle(style);
------解决方案--------------------
ls 给的已经够用了

lz 可以去下个api,仔细查阅一下,你会对这个框架有更好的理解。
------解决方案--------------------
this.sheet.setColumnWidth(column, width);


------解决方案--------------------
this.sheet.setColumnWidth(column, width * 256); 

------解决方案--------------------
[code=Java][/code]package com.baidu.poitest;

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
* @author lixiaoqing
*
*/
public class SimpleDemo {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);

// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);


// Create a row and put some cells in it. Rows are 0 based.
row = sheet.createRow((short)1);
// Create a cell and put a value in it.
cell = row.createCell((short)0);
cell.setCellValue(1);

// Or do it on one line.
row.createCell((short)1).setCellValue(2.4);
row.createCell((short)2).setCellValue("This is another string");
row.createCell((short)3).setCellValue(false);