packagepoi_test;importjava.io.FileOutputStream;importjava.io.IOException;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFFont;importorg.apache.poi.hssf.usermodel.HSSFPrintSetup;importorg.apache.poi.hssf.usermodel.HSSFRichTextString;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;public classExcelTest {public static void main(String[] args) throwsIOException {//新建文件输出流对象
FileOutputStream out = new FileOutputStream("F:/poitest.xls");//新建workbook
HSSFWorkbook wb = newHSSFWorkbook();//新建sheet
HSSFSheet sheet =wb.createSheet();//新建行
HSSFRow row = sheet.createRow(2);//设置行高
row.setHeightInPoints(20);//新建单元格
HSSFCell cell = row.createCell(2);//创建整个文本的字体对象,workbook创建
HSSFFont cnFont =wb.createFont();//设置字体行高,字体名字
cnFont.setFontHeightInPoints((short)10);
cnFont.setFontName("隶书");//将文本字面格式用到单元格上,新建单元格风格
HSSFCellStyle cnStyle =wb.createCellStyle();
cnStyle.setFont(cnFont);
cell.setCellStyle(cnStyle);//单元格内文本对象新建,HSSFRichTextString的应用
HSSFRichTextString richText = new HSSFRichTextString("中文字体测试");
cell.setCellValue(richText);//再建一个单元格,重复上面的设置
HSSFCell enCell = row.createCell(3);
HSSFFont enFont=wb.createFont();
enFont.setFontHeightInPoints((short) 10);
enFont.setFontName("Arial Black");
HSSFCellStyle enStyle=wb.createCellStyle();
enStyle.setFont(enFont);
enCell.setCellStyle(enStyle);
enCell.setCellValue(new HSSFRichTextString("English font test"));
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);//输出//设置边框
sheet.setDisplayGridlines(false);//设置打印的边框
sheet.setPrintGridlines(false);//设置打印对象
HSSFPrintSetup printSetup =sheet.getPrintSetup();//设置页边距
printSetup.setHeaderMargin((double) 0.44); //页眉
printSetup.setFooterMargin((double) 0.2);//页脚//设置页宽
printSetup.setFitWidth((short)1);
printSetup.setFitHeight((short)1000);//设置打印方向,横向就是true
printSetup.setLandscape(true);//设置A4纸
printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);//打印,关闭流
wb.write(out);
out.close();
System.out.println("ok");
}
}