首页 > 编程语言 > 详细

java操作Excel之POI(2)

时间:2017-02-21 00:55:47      阅读:238      评论:0      收藏:0      [点我收藏+]

一、设置单元格对齐方式:

技术分享
 1 /**
 2      * 设置单元格对齐方式
 3      */
 4     public static void main(String[] args) throws Exception {
 5         Workbook wb = new HSSFWorkbook();    //定义一个新的工作簿
 6         Sheet sheet = wb.createSheet("第一个sheet页");
 7         Row row = sheet.createRow(2);                //创建第3行
 8         row.setHeightInPoints(30);                     //设置行高
 9         
10         createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);
11         createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);
12         createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);
13         createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);
14         
15         FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls");
16         wb.write(fileOut);
17         fileOut.close();
18     }
19     /**
20      * 创建一个单元格并为其设置指定的对齐方式
21      * @param wb    工作簿
22      * @param row   行
23      * @param column  列
24      * @param halign  水平方向对齐方式
25      * @param valign  垂直方向对齐方式
26      */
27     @SuppressWarnings("unused")
28     private static void createCell(Workbook wb, Row row, short column, short halign, short valign){
29         Cell cell = row.createCell(column);                           //创建单元格
30         cell.setCellValue(new HSSFRichTextString("Align It"));      //设置值
31         CellStyle cellStyle = wb.createCellStyle();                    //创建单元格样式
32         cellStyle.setAlignment(halign);                                //设置单元格水平方向对齐方式
33         cellStyle.setVerticalAlignment(valign);                        //设置单元格垂直方向对齐方式
34         cell.setCellStyle(cellStyle);
35     }
View Code

技术分享

 

二、设置单元格边框样式

技术分享
 1 /**
 2      * 设置单元格边框样式
 3      */
 4     public static void main(String[] args) throws Exception {
 5         Workbook wb = new HSSFWorkbook();    
 6         Sheet sheet = wb.createSheet("sheet1");
 7         Row row = sheet.createRow(1);                
 8         Cell cell = row.createCell(1);                
 9         cell.setCellValue(4);
10         
11         CellStyle cellStyle = wb.createCellStyle();
12         cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //底部边框
13         cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //底部边框颜色
14         
15         cellStyle.setBorderLeft(CellStyle.BORDER_THIN); //左边边框
16         cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); //左边边框颜色
17         
18         cellStyle.setBorderRight(CellStyle.BORDER_THIN); 
19         cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  
20         
21         cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); 
22         cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
23         
24         cell.setCellStyle(cellStyle);
25         FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls");
26         wb.write(fileOut);
27         fileOut.close();
28     }
View Code

技术分享

 

三、设置单元格填充色和颜色操作

技术分享
 1 /**
 2      * 设置单元格填充色和颜色操作
 3      */
 4     public static void main(String[] args) throws Exception {
 5         Workbook wb = new HSSFWorkbook();    
 6         Sheet sheet = wb.createSheet("sheet1");
 7         Row row = sheet.createRow(1);                
 8         
 9         Cell cell=row.createCell(1);
10         cell.setCellValue("XX");
11         CellStyle cellStyle=wb.createCellStyle();
12         cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
13         cellStyle.setFillPattern(CellStyle.BIG_SPOTS);  
14         cell.setCellStyle(cellStyle);
15         
16         
17         Cell cell2=row.createCell(2);
18         cell2.setCellValue("YYY");
19         CellStyle cellStyle2=wb.createCellStyle();
20         cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); //前景色
21         cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);  
22         cell2.setCellStyle(cellStyle2);
23         
24         FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls");
25         wb.write(fileOut);
26         fileOut.close();
27     }
View Code

技术分享

 

四、设置单元格合并

技术分享
 1     /**
 2      * 设置单元格合并
 3      */
 4     public static void main(String[] args) throws Exception {
 5         Workbook wb = new HSSFWorkbook();    
 6         Sheet sheet = wb.createSheet("sheet1");
 7         Row row = sheet.createRow(1);                
 8         
 9         Cell cell=row.createCell(1);
10         cell.setCellValue("单元格合并测试");
11         
12         sheet.addMergedRegion(new CellRangeAddress(
13                 1, // 起始行
14                 2, //结束行
15                 1, //起始列
16                 2  //结束列
17         ));
18         
19         FileOutputStream fileOut = new FileOutputStream("E:\\工作簿.xls");
20         wb.write(fileOut);
21         fileOut.close();
22     }
View Code

技术分享

java操作Excel之POI(2)

原文:http://www.cnblogs.com/tenWood/p/6422214.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!