HSSFWorkBooK用法.docx
《HSSFWorkBooK用法.docx》由会员分享,可在线阅读,更多相关《HSSFWorkBooK用法.docx(8页珍藏版)》请在冰豆网上搜索。
HSSFWorkBooK用法
HSSFWorkBooK用法
publicActionResultexcelPrint(){
HSSFWorkbookworkbook=newHSSFWorkbook();//创建一个Excel文件
HSSFSheetsheet=workbook.createSheet();//创建一个Excel的Sheet
sheet.createFreezePane(1,3);//冻结
//设置列宽
sheet.setColumnWidth(0,1000);
sheet.setColumnWidth(1,3500);
sheet.setColumnWidth(2,3500);
sheet.setColumnWidth(3,6500);
sheet.setColumnWidth(4,6500);
sheet.setColumnWidth(5,6500);
sheet.setColumnWidth(6,6500);
sheet.setColumnWidth(7,2500);
//Sheet样式
HSSFCellStylesheetStyle=workbook.createCellStyle();
//背景色的设定
sheetStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
//前景色的设定
sheetStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
//填充模式
sheetStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
//设置列的样式
for(inti=0;i<=14;i++){
sheet.setDefaultColumnStyle((short)i,sheetStyle);
}
//设置字体
HSSFFontheadfont=workbook.createFont();
headfont.setFontName("黑体");
headfont.setFontHeightInPoints((short)22);//字体大小
headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗
//另一个样式
HSSFCellStyleheadstyle=workbook.createCellStyle();
headstyle.setFont(headfont);
headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
headstyle.setLocked(true);
headstyle.setWrapText(true);//自动换行
//另一个字体样式
HSSFFontcolumnHeadFont=workbook.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short)10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//列头的样式
HSSFCellStylecolumnHeadStyle=workbook.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);//左边框的颜色
columnHeadStyle.setBorderLeft((short)1);//边框的大小
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);//右边框的颜色
columnHeadStyle.setBorderRight((short)1);//边框的大小
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置单元格的边框为粗体
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);//设置单元格的边框颜色
//设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
HSSFFontfont=workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)10);
//普通单元格样式
HSSFCellStylestyle=workbook.createCellStyle();
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);//上下居中
style.setWrapText(true);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft((short)1);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderRight((short)1);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置单元格的边框为粗体
style.setBottomBorderColor(HSSFColor.BLACK.index);//设置单元格的边框颜色.
style.setFillForegroundColor(HSSFColor.WHITE.index);//设置单元格的背景颜色.
//另一个样式
HSSFCellStylecenterstyle=workbook.createCellStyle();
centerstyle.setFont(font);
centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
centerstyle.setWrapText(true);
centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderLeft((short)1);
centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderRight((short)1);
centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//设置单元格的边框为粗体
centerstyle.setBottomBorderColor(HSSFColor.BLACK.index);//设置单元格的边框颜色.
centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);//设置单元格的背景颜色.
try{
//创建第一行
HSSFRowrow0=sheet.createRow(0);
//设置行高
row0.setHeight((short)900);
//创建第一列
HSSFCellcell0=row0.createCell(0);
cell0.setCellValue(newHSSFRichTextString("中非发展基金投资项目调度会工作落实情况对照表"));
cell0.setCellStyle(headstyle);
/**
*合并单元格
*第一个参数:
第一个单元格的行数(从0开始)
*第二个参数:
第二个单元格的行数(从0开始)
*第三个参数:
第一个单元格的列数(从0开始)
*第四个参数:
第二个单元格的列数(从0开始)
*/
CellRangeAddressrange=newCellRangeAddress(0,0,0,7);
sheet.addMergedRegion(range);
//创建第二行
HSSFRowrow1=sheet.createRow
(1);
HSSFCellcell1=row1.createCell(0);
cell1.setCellValue(newHSSFRichTextString("本次会议时间:
2009年8月31日前次会议时间:
2009年8月24日"));
cell1.setCellStyle(centerstyle);
//合并单元格
range=newCellRangeAddress(1,2,0,7);
sheet.addMergedRegion(range);
//第三行
HSSFRowrow2=sheet.createRow(3);
row2.setHeight((short)750);
HSSFCellcell=row2.createCell(0);
cell.setCellValue(newHSSFRichTextString("责任者"));
cell.setCellStyle(columnHeadStyle);
cell=row2.createCell
(1);
cell.setCellValue(newHSSFRic