欢迎来到天天文库
浏览记录
ID:19759473
大小:33.00 KB
页数:7页
时间:2018-10-05
《poi设置excel格式 字体样式 美化》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、POI设置Excel的格式字体样式美化importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFFont;importorg.apache.poi.hssf.usermodel.HSSFRichTextString;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.userm
2、odel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.hssf.util.HSSFColor;/****@authorhadeslee*/publicclassTest2{/**CreatesanewinstanceofTest2*/publicTest2(){}publicstaticvoidmain(String[]args)throwsException{//声明一个工作薄HSSFWorkbookwb=newHSSFWorkbook();//生成一个表
3、格HSSFSheetsheet=wb.createSheet("表格1");//生成一个列HSSFRowrow=sheet.createRow(0);//生成一个样式HSSFCellStylestyle=wb.createCellStyle();//设置这些样式style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style.setBorderBottom(HSSFCellStyle.BORDER_THIN);s
4、tyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);style.setBorderTop(HSSFCellStyle.BORDER_THIN);style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//生成一个字体HSSFFontfont=wb.createFont();font.setColor(HSSFColor.VIOLET.index);font.setFontHeightInPoints(
5、(short)16);font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//把字体应用到当前的样式style.setFont(font);//填充单元格for(shorti=0;i<5;i++){//声明一个单元格HSSFCellcell=row.createCell(i);//设置单元格的字符值cell.setCellValue(newHSSFRichTextString("第"+i+"列"));//设置单元格的样式cell.setCellStyle(style);}FileOutputStreamfout=newFileOutp
6、utStream("我的第一个EXCEL.xls");//输出到文件wb.write(fout);fout.close();}}publicstaticvoidmain(String[]args){try{HSSFWorkbookwb=newHSSFWorkbook();HSSFSheetsheet=wb.createSheet();HSSFRowrow=sheet.createRow(0);row.setHeight((short)25);//目的是想把行高设置成25pxFileOutputStreamfileOut=newFileOutputStream("c:\a
7、.xls");wb.write(fileOut);fileOut.close();}catch(Exceptione){e.printStackTrace();}}打开a.xls发现结果不是我想要的,第一行的高度都没有,没有报错说明代码有问题,为什么回没有高度呢?是不是单位不一样呢?我把row.setHeight((short)25);改成了row.setHeight((short)250);结果发现第一行出来了,但是这是怎么一个换算关系呢?我查看了一下导出的Excel第一行高是16像素,换算一下得出row.setHeight
此文档下载收益归作者所有