JAVA—咖啡馆

——欢迎访问rogerfan的博客,常来《JAVA——咖啡馆》坐坐,喝杯浓香的咖啡,彼此探讨一下JAVA技术,交流工作经验,分享JAVA带来的快乐!本网站部分转载文章,如果有版权问题请与我联系。

BlogJava 首页 新随笔 联系 聚合 管理
  447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
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((
short20); 
}
 
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)-1true); 

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(); 
posted on 2008-11-25 10:17 rogerfan 阅读(1219) 评论(1)  编辑  收藏 所属分类: 【Java知识】

Feedback

# re: 【转】poi操作excel-中文与单元格样式的方法 2013-01-23 10:03 poi3.8
楼主,你只玩了一个样式,没有试过多样式多字体,不知道那有多恶心。。。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: