# officer **Repository Path**: net_yc60/officer ## Basic Information - **Project Name**: officer - **Description**: 这是一个基于java poi开发的office(excel,pdf,ppt等等)的处理库 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-05-13 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # officer #### 介绍 这是一个基于java poi开发的office(excel,pdf,ppt等等)的处理库 #### 软件架构 这是一个基于POI的简单封装项目,由于时间有限暂时仅开放EXCEL部分处理, 主要是简化导入导出的问题 #### 安装教程 1. 本项目必须依赖POI 2. 依赖于本人的特定工具包 https://gitee.com/net_yc60/support_util 3. 上面的工具包可以基于JAVA11,也可以基于java8,只有HttpAPIUtil是基于JAVA11的,如果只为使用本项目依赖,可以把HttpAPIUtil.java删除,或者自行适配 #### 使用说明 1. 使用ExcelAnnotationScanner.init("");方法指定注解扫描的文件夹 2. 使用@ExcelClass注解声明为Excel处理类 3. 使用@ExcelField指定各个字段的相关信息 4. 可以和lombok注解一同使用 就可以进行简单的使用了 5. 除了以上提到的@ExcelClass和@ExcelField注解外,其他注解均为功能性扩展注解 #### A 简单导出实例 ``` package cn.temp; @ExcelClass @Data public class Book { //headerName用于指定表头名,order用于指定顺序 @ExcelField(headerName = "价格",order=1) private Double price; @ExcelField(headerName = "书名",order=2) private String name; @ExcelField(headerName = "库存",order=3) private Integer count; } --------------------------------------------------------- @Test public void test() throws FileNotFoundException { ExcelAnnotationScanner.init("cn.temp"); Book book1 = new Book(); book1.setPrice(1.2); book1.setName("西游记"); book1.setCount(5000); Book book2 = new Book(); book2.setPrice(1.5); book2.setName("三国演义"); book2.setCount(560); // IExcelWriter writer = ExcelUtils.get03ExcelWriter(); IExcelWriter writer = ExcelUtils.get07ExcelWriter(); writer.write(book1,book2); writer.flush(new FileOutputStream(new File("C:\\***\\Desktop\\test.xlsx"))); } ``` ![demo01](https://images.gitee.com/uploads/images/2020/0108/085257_87d5835f_1505707.png) #### B 功能类导出实例 ``` @ExcelClass @Data public class Book { //headerName用于指定表头名,order用于指定顺序 @ExcelField(headerName = "价格",condition = @FieldCondition(order = 1),style = @ExcelStyle(columnWidth = 12)) //可以调节单元格宽度 private Double price; @ExcelField(headerName = "书名",condition = @FieldCondition(order = 2), style = @ExcelStyle(fontName = "宋体",wrapText = true,bold = true,italic = true)) //可以调整为指定样式 private String name; @ExcelField(headerName = "库存",condition = @FieldCondition(order = 3)) private Integer count; @ExcelDateFormat("yyyy-MM-dd") //使用这个注解可以处理日期格式问题 @ExcelComment("这是出产日期") //可以在表头带有comment @ExcelField(headerName = "生产日期", condition = @FieldCondition(order = 4)) private Date date; @ExcelField(headerName = "图样", condition = @FieldCondition(order = 5),IMAGE = @ExcelImage(imageType = Workbook.PICTURE_TYPE_JPEG)) //可以声明为图片显示 private String img; //图片类型支持链接(本地和httpurl)和byte[]类型 } 上面的代码中使用了 @ExcelField中的IMAGE属性,该属性可以处理图片,但是只能在导出时使用 中的condition属性,可以设置顺序 中的style属性,可以设置指定的样式 @ExcelDateFormat可以对Date类型进行格式化写入,在读取时同样可以生效 @ExcelComment可以设置改列数据的顶部注释,就是下图蓝色框部分, 当鼠标置于该框上方时,注释文字就会显示出来 ================================================================================================= @Test public void test() throws FileNotFoundException { ExcelAnnotationScanner.init("cn.temp"); Book book1 = new Book(); book1.setPrice(1.2); book1.setName("西游记"); book1.setCount(5000); book1.setDate(new Date()); book1.setImg("http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01dfad59462f58a8012193a3b63e9c.jpg%402o.jpg&thumburl=http%3A%2F%2Fimg0.imgtn.bdimg.com%2Fit%2Fu%3D1959255714%2C1379024244%26fm%3D26%26gp%3D0.jpg"); Book book2 = new Book(); book2.setPrice(1.5); book2.setName("三国演义"); book2.setCount(560); book2.setDate(new Date()); book2.setImg("https://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fwww.jianbihua.cc%2Fuploads%2Fallimg%2F160628%2F285-16062Q55252.jpg&thumburl=https%3A%2F%2Fss3.bdstatic.com%2F70cFv8Sh_Q1YnxGkpoWK1HF6hhy%2Fit%2Fu%3D448486059%2C1319747046%26fm%3D26%26gp%3D0.jpg"); // IExcelWriter writer = ExcelUtils.get03ExcelWriter(); IExcelWriter writer = ExcelUtils.get07ExcelWriter(); writer.write(book1,book2); writer.flush(new FileOutputStream(new File("C:\\****\\test.xlsx"))); } ``` ![demo02](https://images.gitee.com/uploads/images/2020/0108/085440_66e7d315_1505707.png) #### C 特殊类导出实例 ``` @ExcelClass @ExcelTopHeaders(headers = { @ExcelTopHeader(topHeaderWidth = 7,topHeader="这是顶部菜单") //顶部菜单 }) @Data public class Book { //headerName用于指定表头名,order用于指定顺序 @ExcelField(headerName = "价格",condition = @FieldCondition(order = 1),style = @ExcelStyle(columnWidth = 12)) //可以调节单元格宽度 private Double price; @ExcelField(headerName = "书名",condition = @FieldCondition(order = 2), style = @ExcelStyle(fontName = "宋体",wrapText = true,bold = true,italic = true)) //可以调整为指定样式 private String name; @StyleConditions(targetConditions={@StyleCondition(config = StyleConditionForCount.class,initArgs = {"1000"})}) @ExcelField(headerName = "库存",condition = @FieldCondition(order = 3)) private Integer count; @ExcelDateFormat("yyyy-MM-dd") //使用这个注解可以处理日期格式问题 @ExcelComment("这是出产日期") //可以在表头带有comment @ExcelField(headerName = "生产日期", condition = @FieldCondition(order = 4)) private Date date; @ExcelField(headerName = "图样", condition = @FieldCondition(order = 5),IMAGE = @ExcelImage(imageType = Workbook.PICTURE_TYPE_JPEG)) //可以声明为图片显示 private String img; //图片类型支持链接和byte[]类型 @ExcelCondition(BOOLEAN_CHECK = @BooleanCheck(BOOLEAN_MARKS={@BooleanMark(trueOf = "在售", falseOf = "停售")},booleanCheckForRead = true,booleanCheckForWrite = true)) //boolean转文字描述 @ExcelField(headerName = "是否在售", condition = @FieldCondition(order = 6)) private Boolean isSell; @ExcelCondition(INT_CHECK = @IntCheck(INT_MARK={@IntMark(key=0,value = "全部人群"),@IntMark(key = 1,value = "儿童")},checkForRead = true,checkForWrite = true)) //int转文字描述 @ExcelField(headerName = "适合人群", condition = @FieldCondition(order = 7)) private Integer fitType; } ================================================================================================================== 以上代码较之前的特殊, 1.使用了@ExcelTopHeaders这个注解来处理顶部菜单,这个顶部菜单可以设置横向合并单元格, 同时为其设置需要的样式 2.使用了@ExcelCondition注解中的BOOLEAN_CHECK和INT_CHECK对 boolean类型和Integer类型进行文字转换描述,在读取时同样有效 3.使用了@StyleConditions注解,这个注解在实际工作中可能会有需要,它可以设置该属性数据 在指定情况下,使用指定的样式,它的唯一属性targetConditions是一个@StyleCondition类型数组, 也就是支持多种情况设定. @StyleCondition有两个属性,config和initArgs config指向StyleConditionConfig的一个实现类,initArgs是该实现类的构造方法, 不是无参构造器的情况下,请使用@StyleConditionConstructor声明所使用的构造器 如果为无参构造,请传空数组 以下是代码中的 StyleConditionForCount实现类代码 ================================================================================================================= /** * 库存的特殊样式处理器 */ public class StyleConditionForCount implements StyleConditionConfig { private Integer warnCount; //当少于这个库存数量时,使用指定样式 @StyleConditionConstructor //这个注解非常重要,在不使用默认无参构造方法时,必须使用该注解指定默认的构造器 public StyleConditionForCount(Integer warnCount) { //构造方法 //实例中的注解 @StyleConditions(targetConditions={@StyleCondition(config = StyleConditionForCount.class,initArgs = {"1000"})}) this.warnCount = warnCount; } @Override public boolean when(Integer data) { return data (){} ``` #### 有部分功能仍未上传,暂时只有读写,对spring的支持包也没上传,需要使用的请执行想办法!