java操作Excel之POI(2)

本文介绍了如何使用Java程序来设置Excel表格的各种样式,包括单元格的对齐方式、边框样式、填充色及合并单元格等操作。通过示例代码详细展示了不同属性的设置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 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

转载于:https://www.cnblogs.com/tenWood/p/6422214.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值