设为首页 收藏本站
查看: 794|回复: 0

[经验分享] 一脸懵逼学习Java操作Excel之POI(Apache POI)

[复制链接]

尚未签到

发表于 2017-12-24 22:35:42 | 显示全部楼层 |阅读模式
1 package com.bie;  

  2  
  3 import java.io.FileOutputStream;
  
  4 import java.io.IOException;
  
  5
  
  6 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  
  7 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  
  8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  
  9 import org.apache.poi.ss.usermodel.Cell;
  
10 import org.apache.poi.ss.usermodel.CellStyle;
  
11 import org.apache.poi.ss.usermodel.IndexedColors;
  
12 import org.apache.poi.ss.usermodel.Row;
  
13 import org.apache.poi.ss.usermodel.Sheet;
  
14 import org.apache.poi.ss.usermodel.Workbook;
  
15 import org.apache.poi.ss.util.CellRangeAddress;
  
16
  
17 /**
  
18 * @author  Author:别先生
  
19 * @date Date:2017年9月9日 上午10:14:31
  
20 *
  
21 *
  
22 */

  
23 public>  
24
  
25     /***
  
26      * 创建一个单元格并为其设定指定的对齐方式
  
27      * @param wb 工作簿
  
28      * @param row 行
  
29      * @param column 列
  
30      * @param halign 水平对齐的方式
  
31      * @param valign 垂直对齐的方式
  
32      */
  
33     private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
  
34         //创建单元格
  
35         Cell cell = row.createCell(column);
  
36         //设置单元格的值
  
37         cell.setCellValue(new HSSFRichTextString("测试内容"));
  
38         //创建单元格的样式
  
39         CellStyle cellStyle = wb.createCellStyle();
  
40         //设置单元格水平对齐方式
  
41         cellStyle.setAlignment(halign);
  
42         //设置单元格垂直对齐方式
  
43         cellStyle.setVerticalAlignment(valign);
  
44         
  
45         //设置边框线和颜色
  
46         //底部边框
  
47         cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
  
48         //底部颜色
  
49         cellStyle.setBottomBorderColor(IndexedColors.BLUE.getIndex());
  
50         
  
51         //左边边框
  
52         cellStyle.setBorderLeft(CellStyle.BORDER_DASH_DOT);
  
53         //左边颜色
  
54         cellStyle.setBottomBorderColor(IndexedColors.RED.getIndex());
  
55         
  
56         //右边边框
  
57         cellStyle.setRightBorderColor(CellStyle.ALIGN_FILL);
  
58         //右边边框颜色
  
59         cellStyle.setBottomBorderColor(IndexedColors.DARK_YELLOW.getIndex());
  
60         
  
61         //顶部边框
  
62         cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);
  
63         //顶部颜色
  
64         cellStyle.setTopBorderColor(IndexedColors.AUTOMATIC.getIndex());
  
65         
  
66         //设置单元格填充色和颜色操作
  
67         //设置背景颜色
  
68         cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());
  
69         cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
  
70         //设置前景颜色
  
71         cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
  
72         cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
  
73         
  
74         //设置单元格的样式
  
75         cell.setCellStyle(cellStyle);
  
76     }
  
77     
  
78     public static void main(String[] args) throws IOException {
  
79         //定义一个工作簿
  
80         Workbook wb =new HSSFWorkbook();
  
81         //创建第一个sheet页
  
82         Sheet createSheet = wb.createSheet("第一个sheet");
  
83         //创建第一行
  
84         Row createRow = createSheet.createRow(0);
  
85         createRow.setHeightInPoints(30);
  
86         
  
87         //调用工具方法,创建单元格
  
88         //单元格的对齐方式的调用和使用
  
89         createCell(wb, createRow, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);;
  
90         createCell(wb, createRow, (short)1, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_CENTER);;
  
91         createCell(wb, createRow, (short)2, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);;
  
92         createCell(wb, createRow, (short)3, HSSFCellStyle.ALIGN_JUSTIFY, HSSFCellStyle.VERTICAL_JUSTIFY);;
  
93         
  
94         
  
95         //创建单元格
  
96         //创建第3行
  
97         Row createRow2 = createSheet.createRow(2);
  
98         createRow.setHeightInPoints(30);
  
99         //创建第一列和第二列
  
100         Cell createCell = createRow2.createCell(0);
  
101         createCell.setCellValue("单元格合并1");
  
102         
  
103         Cell createCell2 = createRow2.createCell(1);
  
104         createCell2.setCellValue("单元格合并2");
  
105         
  
106         //单元格合并,起始行,结束行,起始列,结束列
  
107         createSheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 3));
  
108         
  
109         
  
110         //创建输出流
  
111         FileOutputStream fos = new FileOutputStream("d:\\poi.xlsx");
  
112         //将内容写到excel文件中
  
113         wb.write(fos);
  
114         //关闭流
  
115         fos.close();
  
116     }
  
117     
  
118 }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-427655-1-1.html 上篇帖子: apache环境搭建(xampp跑不起来) 下篇帖子: java.lang.NoSuchMethodError: org.apache.spark.util.ThreadUtils$.newDae
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表