- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.CellStyle;
- import org.apache.poi.ss.usermodel.Font;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- /**
- * @author 刘毅
- * @date 2010-1-12
- * @param
- */
- public class CreateExcelForWorkingFontsSize {
- /***
- * 创建空的xls格式的文档,设置字体大小
- */
- public void createXlsForFontsSize(){
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("new sheet");
- Row row = sheet.createRow(1);
- // 创建字体并改变它
- Font font = wb.createFont();
- //设置长度
- font.setFontHeightInPoints((short)24);
- font.setFontName("Courier New");
- //斜体
- font.setItalic(true);
- //设置字体杠杠
- font.setStrikeout(true);
- CellStyle style = wb.createCellStyle();
- style.setFont(font);
- Cell cell = row.createCell(1);
- cell.setCellValue("This is a test of fonts");
- cell.setCellStyle(style);
- FileOutputStream fileOut = null;
- try {
- fileOut = new FileOutputStream("src/workbook.xls");
- wb.write(fileOut);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- if(null !=fileOut){
- fileOut.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- CreateExcelForWorkingFontsSize create = new CreateExcelForWorkingFontsSize();
- create.createXlsForFontsSize();
- }
- }