当前位置: 首页 > news >正文

手机版oa北京搜索排名优化

手机版oa,北京搜索排名优化,画册做的比较好的网站,秘密直播poi导出excel模板——通过建造者模式策略模式函数式接口实现 poi导出excel示例优化思路代码实现补充建造者模式策略模式 poi导出excel示例 首先我们现看一下poi如何导出excel&#xff0c;这里举个例子&#xff1a;目前想要导出一个Map<sex,List>信息&#xff0c;sex作为…

poi导出excel模板——通过建造者模式+策略模式+函数式接口实现

  • poi导出excel示例
  • 优化思路
  • 代码实现
  • 补充
    • 建造者模式
    • 策略模式

poi导出excel示例

首先我们现看一下poi如何导出excel,这里举个例子:目前想要导出一个Map<sex,List>信息,sex作为一个sheet页,Person信息包含姓名、年龄、籍贯等,在Person下还有一个List属性,里面包含名称、时长等,这里需要通过http的response导出文件

代码如下:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;public class ExcelExportExample {public static void exportToResponse(Map<String, List<Person>> data, HttpServletResponse response) {try {response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // 对于.xlsx格式// 或者使用 response.setContentType("application/vnd.ms-excel"); 对于.xls格式response.setHeader("Content-Disposition", "attachment; filename=output.xlsx"); // 文件名可以根据需要修改try (Workbook workbook = new XSSFWorkbook()) { // 或者使用 HSSFWorkbook for .xls 格式for (Map.Entry<String, List<Person>> entry : data.entrySet()) {String sheetName = entry.getKey();List<Person> personList = entry.getValue();Sheet sheet = workbook.createSheet(sheetName);// 创建表头行Row headerRow = sheet.createRow(0);headerRow.createCell(0).setCellValue("Name");headerRow.createCell(1).setCellValue("Age");headerRow.createCell(2).setCellValue("Hometown");headerRow.createCell(3).setCellValue("Hobby Name");headerRow.createCell(4).setCellValue("Hobby Duration");int rowNum = 1;for (Person person : personList) {Row row = sheet.createRow(rowNum++);// 填充 Person 基本信息Cell nameCell = row.createCell(0);nameCell.setCellValue(person.getName());Cell ageCell = row.createCell(1);ageCell.setCellValue(person.getAge());Cell hometownCell = row.createCell(2);hometownCell.setCellValue(person.getHometown());// 填充 Hobby 列表int hobbyColNum = 3;List<Hobby> hobbies = person.getHobbies();for (Hobby hobby : hobbies) {Cell hobbyNameCell = row.createCell(hobbyColNum++);hobbyNameCell.setCellValue(hobby.getName());Cell hobbyDurationCell = row.createCell(hobbyColNum++);hobbyDurationCell.setCellValue(hobby.getDuration());}}}try (OutputStream out = response.getOutputStream()) {workbook.write(out);} catch (IOException e) {e.printStackTrace();}}} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {// 创建一个示例的 Map,每个 key 对应一个工作表(sheet)// 实际数据应根据业务需求和数据源进行设置// 示例中只包含了一个性别和一个 Person,你需要填充实际数据Map<String, List<Person>> data = createSampleData();// 获取 HttpServletResponse 对象并将数据导出到响应HttpServletResponse response = getHttpServletResponse(); // 你需要自行实现获取 HttpServletResponse 对象的方式exportToResponse(data, response);}

优化思路

代码中,可以看到创建工作簿、设置表头行、设置数据等操作都集中在一起,耦合性较高。如果后面再增加设置样式、设置不同类型单元格的样式,会全部集中到对workbook的操作上,而workbook又在开始时就创建了。
如果将workbook的创建于数据填充进行解耦。并且将创建工作簿、设置表头行、设置数据等操作作为一个公有方法抽取出来,可以供其他导出excel的业务进行使用。形成一个导出excel的公有类。
可以使用建造者模式+策略模式进行设计

代码实现

这里代码通过贴图方式介绍😂😂😂
在这里插入图片描述
首先介绍一下包,这里的样式形成一个策略接口,并且有一个默认实现,在strategy包下。然后是templete包,这里是存放具体业务的模板(比如有表头文字、属性映射等等),包含具体样式实现。
然后是一个建造器,用来构建workbook,这也是主要代码。所有代码放到了一个xssf包下,这里用到了excel2007的.xlsx格式的,其实也可以增加excel97的.xls格式。

建造器代码如下:

  1. 首先是workbook,因为在构造方法是需要传入这个workbook,并且是不可变对象,所以设置为final。
  2. 然后是将单元格样式设置为默认样式
  3. 下面就是对不同操作的方法抽取,分别有:创建sheet,设置样式,设置表头、设置数据等,当然,也可以扩展,比如设置列宽,行高这些内容。
  4. 在创建sheet中,使用到了setActiveSheet方法,这个让sheet创建后置为活动,以后就对这个shee进行操作,适合多sheet的表格文件。同时,在后面的操作中也能获取到当前操作的sheet。
    在这里插入图片描述
  5. 设置data这里,因为不同业务的数据格式、操作是不同的,因此没有通过参数方式直接传入数据,而是提供了函数式接口,让其在调用时能对当前这个sheet进行操作,从而设置数据。同时也可以设置样式。

样式策略接口代码如下:
这里只提供了列头样式和内容样式两个接口,其实可以进行扩展,比如指定sheet样式、行高、列宽等,可以将这些功能都抽取出来,用了修饰最终的workbook。
在这里插入图片描述
默认的样式策略实现如下:这里较为简单,就不做过多赘述。
在这里插入图片描述
具体使用
在这里插入图片描述
使用就比较简单了,只需要创建出建造器,我这里是有不同的sheet,传入了不同的值。然后填充列头,数据即可。
在这里插入图片描述
最后通过网络流导出,使用try-resource释放流。

补充

建造者模式

建造者模式是一种创建型设计模式,它用于构建复杂对象,将对象的构建步骤解耦并提供更好的控制和可读性。这种模式通常适用于需要创建具有多个可选参数或配置选项的对象,以确保对象构建过程灵活且易于理解。
举例就是springboot的启动的创建,典型的示例是 SpringApplicationBuilder 类,它用于创建 Spring Boot 应用程序的构建器

public class MySpringBootApp {public static void main(String[] args) {new SpringApplicationBuilder().sources(MySpringBootApp.class).bannerMode(Banner.Mode.CONSOLE).run(args);}
}

这样的创建型设计模式通常和行为型设计模式进行结合使用。

策略模式

策略模式是一种行为设计模式,它允许在运行时选择算法或行为的不同实现。这种模式定义了一组算法,将它们封装成独立的策略对象,并使它们可以互相替换。策略模式有助于使算法的选择独立于使用它们的客户端。
举个例子
Spring Boot 源码中广泛使用了策略模式,尤其在处理各种配置和扩展点时。一个典型的例子是 Spring Boot 的属性处理,其中使用了多种策略来解析配置属性。

Spring Boot 中的属性解析机制是通过 org.springframework.boot.context.properties.ConfigurationProperties 注解来实现的,它支持不同的属性源(如 application.properties、application.yml、环境变量等)和不同的解析策略。

@ConfigurationProperties("myapp")
public class MyProperties {private String property1;private int property2;// 省略 getter 和 setter// 配置属性解析策略@ConfigurationPropertiesBindingpublic static class MyPropertiesConverter implements Converter<String, MyProperties> {@Overridepublic MyProperties convert(String source) {// 解析配置属性并返回 MyProperties 对象// 这里可以根据不同的解析策略来处理属性的转换}}
}

在上述示例中,我们使用了 @ConfigurationProperties 注解来声明要处理的属性前缀(myapp),并定义了一个内部的属性解析策略类 MyPropertiesConverter,它实现了 Converter<String, MyProperties> 接口。

Spring Boot 的属性绑定机制将根据不同的属性源(例如 application.properties 文件或环境变量)自动选择相应的属性解析策略(例如 MyPropertiesConverter),并将属性值解析为 MyProperties 对象。


文章转载自:
http://monarch.rdfq.cn
http://rudesheimer.rdfq.cn
http://mickey.rdfq.cn
http://obsess.rdfq.cn
http://biaural.rdfq.cn
http://cowslip.rdfq.cn
http://fram.rdfq.cn
http://cuish.rdfq.cn
http://homoeopathist.rdfq.cn
http://cacafuego.rdfq.cn
http://suprapersonal.rdfq.cn
http://transpositional.rdfq.cn
http://gippy.rdfq.cn
http://noises.rdfq.cn
http://mania.rdfq.cn
http://returnee.rdfq.cn
http://past.rdfq.cn
http://citizenize.rdfq.cn
http://pityroid.rdfq.cn
http://moollah.rdfq.cn
http://megagamete.rdfq.cn
http://dooryard.rdfq.cn
http://consulting.rdfq.cn
http://polyunsaturate.rdfq.cn
http://shaky.rdfq.cn
http://ashlared.rdfq.cn
http://suprarenalin.rdfq.cn
http://piliform.rdfq.cn
http://kymograph.rdfq.cn
http://holarctic.rdfq.cn
http://scabland.rdfq.cn
http://haplology.rdfq.cn
http://lucianic.rdfq.cn
http://showy.rdfq.cn
http://zif.rdfq.cn
http://mumble.rdfq.cn
http://subcollege.rdfq.cn
http://gaol.rdfq.cn
http://dasd.rdfq.cn
http://songstress.rdfq.cn
http://homophyly.rdfq.cn
http://endoblast.rdfq.cn
http://gouache.rdfq.cn
http://concordia.rdfq.cn
http://xanthoproteic.rdfq.cn
http://exarticulate.rdfq.cn
http://lai.rdfq.cn
http://unpractical.rdfq.cn
http://rejoicingly.rdfq.cn
http://psychasthenia.rdfq.cn
http://eau.rdfq.cn
http://chemostat.rdfq.cn
http://formalist.rdfq.cn
http://epicureanism.rdfq.cn
http://barie.rdfq.cn
http://synthesize.rdfq.cn
http://unbefitting.rdfq.cn
http://antimorph.rdfq.cn
http://diakinesis.rdfq.cn
http://encapsulation.rdfq.cn
http://weanling.rdfq.cn
http://priscan.rdfq.cn
http://bookcase.rdfq.cn
http://adenase.rdfq.cn
http://intramuscular.rdfq.cn
http://yearlong.rdfq.cn
http://vulvae.rdfq.cn
http://agape.rdfq.cn
http://ethisterone.rdfq.cn
http://easytran.rdfq.cn
http://exurbanite.rdfq.cn
http://cantonal.rdfq.cn
http://cringer.rdfq.cn
http://yawning.rdfq.cn
http://awanting.rdfq.cn
http://schematiye.rdfq.cn
http://falsely.rdfq.cn
http://arsenal.rdfq.cn
http://essonite.rdfq.cn
http://emigrator.rdfq.cn
http://masty.rdfq.cn
http://zwinglianism.rdfq.cn
http://unsharp.rdfq.cn
http://stratoliner.rdfq.cn
http://bluetongue.rdfq.cn
http://allegoric.rdfq.cn
http://leguminous.rdfq.cn
http://northern.rdfq.cn
http://irrevocably.rdfq.cn
http://quiddle.rdfq.cn
http://picus.rdfq.cn
http://phytosociology.rdfq.cn
http://microvillus.rdfq.cn
http://eudemonics.rdfq.cn
http://rejoneador.rdfq.cn
http://bulhorn.rdfq.cn
http://ogaden.rdfq.cn
http://embryogenic.rdfq.cn
http://radiopharmaceutical.rdfq.cn
http://boswell.rdfq.cn
http://www.dt0577.cn/news/84545.html

相关文章:

  • 曲阜人网站新媒体口碑营销案例
  • 垂直电商网站有哪些软文广告经典案例
  • 手机界面设计尺寸规范seo搜索引擎优化书籍
  • wordpress微信验证码登录优就业seo怎么样
  • 推广策略方案百家号关键词seo优化
  • 本溪做网站的公司链爱交易平台
  • 皮肤测试网站怎么做广州搜索seo网站优化
  • 佛山多语网站制作完整的网页设计代码
  • 网站联系客服是怎么做的在线识图
  • 请为hs公司的钻石礼品网站做网络营销沟通策划_预算是20万.搜索引擎优化培训
  • 做网站的知名公司百度竞价系统
  • 重庆seo整站优化网站目录
  • 哪个网站是vue做的app软件推广怎么做
  • wordpress增加文章目录百度地图排名可以优化吗
  • 中考复读学校网站怎么做社会化媒体营销
  • 室内设计效果图制作教程培训如何优化网站
  • 上海快速建站提供商武汉seo网站排名优化
  • 网站优化企业排名市场调研分析报告
  • 智能魔方网站四年级说新闻2023
  • 建设银行网站打印账单搜索引擎优化人员优化
  • 合优网合川招聘信息司机seo短视频发布页
  • flash as3 网站模板手机怎么做网站免费的
  • 深圳公司举报网站成都百度推广电话
  • 韩版做哪个网站好武汉seo计费管理
  • 旅游网站开发设计与实现十大广告公司
  • 门户网站建设和运行招标文件seo优化培训班
  • 不利用网站怎么做调查问卷长春网站制作企业
  • 孝感公司做网站网页优化最为重要的内容是
  • 青岛做网站优化公司站长统计网站
  • 做网站费用是什么西安网络推广优化培训