excel的样式

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 package *.*;

import java.util.ArrayList;
import java.util.Iterator;

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

public class FontCellStyle {
private static HSSFFont fontStyle = null;
private static HSSFCellStyle cellStyle = null;

/*设置字体格式*/
public static HSSFFont getHdrFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)20);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
return fontStyle;
}
public static HSSFFont getFtrFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)12);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
return fontStyle;
}
public static HSSFFont getContentFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("宋体");
fontStyle.setFontHeightInPoints((short)12);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
return fontStyle;
}
public static HSSFFont getMergeConflictFont(HSSFWorkbook wb) {
fontStyle = wb.createFont();
fontStyle.setFontName("Arial");
fontStyle.setFontHeightInPoints((short)12);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
return fontStyle;
}
/*设置Excel单元格格式,引用到字体格式*/
public static HSSFCellStyle getAnyCellStyle(HSSFWorkbook wb,HSSFFont font,short align,short valign,short indent,boolean wrapText) {
cellStyle =wb.createCellStyle();
if(font != null) cellStyle.setFont(font);
if(align > 0) cellStyle.setAlignment(align);
if(valign > 0) cellStyle.setVerticalAlignment(valign);
if(indent > 0) cellStyle.setIndention(indent);
cellStyle.setWrapText(wrapText);
return cellStyle;
}

/*设置Excel单元格行高、列宽*/
public static void setDefaultHighWidth(HSSFSheet sheet) {
sheet.setDefaultRowHeightInPoints(10);
sheet.setDefaultColumnWidth((short) 20);
}
public static void setDefaultCellHighWidthInRange(HSSFSheet sheet,short[] eachCellWidth,int high) {
//假定第一行和第一行所需的单元个已经建立好了,也就是说,在这之前已经调用了DesignXlsHeaderFooter.setXlsHeader
sheet.setDefaultRowHeightInPoints(high);//设置默认高
/*设置各列单元格宽度*/
for(int i = 0;i < eachCellWidth.length;i++) {
//System.out.print(""+i+"\t");
sheet.setColumnWidth((short) i,(short) ((eachCellWidth[i])*256));
}
//System.out.println();
/*
Iterator arrayItr = eachCellWidth.iterator();
short width;
short pos = 0;
while(arrayItr.hasNext()) {
width = Short.parseShort((String)arrayItr.next());
sheet.setColumnWidth(pos,width);
pos++;
}
*/
}//end_setDefaultCellHighWidthInRange
}


/*调用方式*/

/*设置整体excel单元格格式*/

FileOutputStream fos = null;
try {
fos = new FileOutputStream(rptRealPathAndName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//System.out.println("创建文件失败。。。");
log.info("In WriteRptByType.writeRptTypeFive(),create file failed!!!");
log.error(e.getMessage());
//e.printStackTrace();
return -1;
}
HSSFWorkbook workBook = new HSSFWorkbook();
HSSFSheet sheet = workBook.createSheet();;
workBook.setSheetName(0,"移动",HSSFWorkbook.ENCODING_UTF_16);
HSSFCellStyle cellStyleHdr = FontCellStyle.getAnyCellStyle(workBook,FontCellStyle.getHdrFont(workBook),HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, (short)-1, true);

HSSFRow curRow = sheet.createRow(0);
HSSFCell curCell= curRow.createCell((short)0);
curCell.setEncoding(HSSFCell.ENCODING_UTF_16);
curCell.setCellStyle(cellStyleHdr );
curCell.setCellValue("可以写入汉字,无乱码");

/*.............写入文件.............*/

try {
workBook.write(fos);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//System.out.println("写错误。。。");
succFlag = -1;
log.error("报表写错误:"+e.getMessage());
//e.printStackTrace(); 【转自www.bitsCN.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值