使用接口适配模式做报表导出
来源:互联网 发布:Java 记录日志 demo 编辑:程序博客网 时间:2024/06/11 03:25
目录
生产中时常会遇到做报表导出的功能,让我们一步一步的来分析怎么做一个报表,
cell+row+sheet
1。 cell : 报表的 最小单位值是 cell : 有值,类型,样式,生产中我们可以实现在 resource中 放入已经调好的样式,于是我们要做的就是向excel中添值,为了更好的添值,我们为 每个cell 添加一个 索引 属性
public class Cell { private Object value; private int colIndex; //显示的列索引 private String dataType; //int, string, date, double}
- 由cell 构成的是一个row行,每一个row 又有 cell,显不显示
所以
public class Row { private String indicator; private Cell[] cells = null; //行值 private boolean formular = false; }
- 由row又会构成sheet,可能会有很多sheet ,当然 也会有 一年 有12个月,于是一个 excel文件中含有 12 个sheet,于是我们理所当然要 配置 每一个sheet 的 code + 名字+ 是否复制
public class Sheet { private String code; //编码 private String name; //Sheet名称 private boolean clone = false; //是否复制}
导出的逐步分解
- 首先 对导出动作做一个抽象,导出的 是 sheet,sheet 都需要 数据集
public interface IExcelReportSource { * 获取Sheet定义 肯定是共有的 public Sheet[] getSheets(Map<String, Object> context); * 获取当前Sheet需要的数据集 这个肯定是 都不相同 public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet);}
这时 ,我有些excel 可能只需要sheet,有些可能具体的数据,所以我的实现类,只需要接口中的某些方法的实现,所以 我需要写一个 接口适配器,
public abstract class AbstractReportSourceService implements IExcelReportSource { @Override public Sheet[] getSheets(Map<String, Object> context) { // 这里可以是 你从 其他地方搞来的 数据组装的 sheet}}
这个抽象类 重写的getsheets() 可以公用,具体的实现类看可以有很多个
@Component("LSHROSWeekReportSource")public class souce1 extends AbstractReportSourceService { @Override public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) { }}
或者
@Component("SaleMonthlyReportSource")public class source2 extends AbstractReportSourceService { @Override public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) { }}
2。 导出的报表ExcelReportContext 需要
模板名字+文件名+导出动作+起止cell
public class ExcelReportContext { private String template; // 模板名字 private String fileName; // 文件名字 private Map<String, Object> params = new HashMap<>();// 这就是 参数 private IExcelReportSource source; private boolean staticSheet; private int clonableSheet = 0; private int startCellIndex = 0; private int endCellIndex = 0; private boolean autoRowExtended = false; public ExcelReportContext addParam(String name, Object value) { params.put(name, value); return this; } }
3。 导出的service做一个抽象,
public interface IExcelReportService { * 导出Excel到outputStream 本地 导出 public void export(ExcelReportContext context, OutputStream os) throws Exception; * 导出Excel到Response 导出到 页面 public void export(ExcelReportContext context, HttpServletResponse response) throws Exception;}
实现类
@Component("ExcelReportService")public class ExcelReportServiceImpl implements IExcelReportService { @Override public void export(ExcelReportContext context, OutputStream os) throws Exception { XSSFWorkbook xWorkbook = this.getWorkbook(context); xWorkbook.write(os); xWorkbook = null; }
实际使用
根据 注入的 resource ,确定 数据集从哪里来,
@Service@Transactionalpublic class ReportServiceImpl implements ReportService { private Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class); @Autowired private IExcelReportService excelReportService; @Resource(name = "SaleMonthlyReportSource") private IExcelReportSource source1; @Resource(name = "LSHROSWeekReportSource") private IExcelReportSource source2; public void getSalesManagerReport(String username, String year, HttpServletResponse response) { try { ExcelReportContext context = new ExcelReportContext(); context.setTemplate("templates/销售管理报表模板.xlsx") .setFileName(year + "年销售管理报表.xlsx") .setSource(saleMonthlyReportSource) .setStaticSheet(false) //.setClonableSheet(2) 读取第三个 sheet(不写默认为第一个sheet) .addParam("username", username).addParam("year", year); excelReportService.export(context, response); } catch (Exception exc) { logger.error("报表[销售管理报表]导出异常。", exc); } }
阅读全文
0 0
- 使用接口适配模式做报表导出
- 使用jexcel导出报表
- 报表导出 使用POI
- 百灵报表(BIRT)接口学习:使用Data Extraction Task导出数据
- Java 使用 iReport 导出报表
- FusionChartsV3 做报表使用小结
- 使用highChart做报表统计
- 使用POI来做报表
- 水晶报表使用按钮导出EXCEL
- 导出报表CSV,卸载迅雷,使用IE
- Java使用IText PDF 导出报表
- java pageoffice使用,生成导出excel报表
- 导出报表
- 报表导出
- 报表导出
- 导出报表
- 第一次使用rdlc做报表心得
- 一步一步使用POI做java报表
- 八年Java开发的学习经历自述,希望新人少走弯路
- html框架
- 机器学习基石 作业1 程序题(15-20)
- 看到有一个人问前驱结点用英文怎么说
- 解决kindeidtor与struts2框架交互WARN OgnlValueStack:68
- 使用接口适配模式做报表导出
- 关于PostgreSQL的一些限制
- STUN协议数据格式及抓包分析
- JS零散点
- Linphone 被叫方如何解析来电SIP消息中的自定义头消息
- 动脑学院安卓视频资料分享
- SOCK5代理服务器配置
- Vue之跨域上传excel表格
- Unity Shader入门精要 阅读笔记五