---
title: "数据库输出HTML格式报表的测试"
author: "justforyou"
date: "2011-05-29T19:01:14Z"
tags: ["Apache", "Excel", "HTML", "SQL", "单元测试"]
categories: ["数据库"]
slug: "html"
draft: false
lastmod: "2011-05-29T19:01:14Z"
id: 1063908
url: "https://justforyou.iteye.com/blog/1063908"
---

当需要在数据库中开发输出HTML报表的时候, 我们可以在过程中直接使用htp包来实现html代码和对数据处理后的输出. 并输出为需要的格式(HTML标记语言来控制) . 以下提供一个直接从数据库生成HTML报表的测试例子, 需要根据自己的需要来进行修改或增加. 在使用前需要配置apache的连接, 这样在调用过程的时候, 系统直接将生成的html输出到WEB; --配置apache连接: 如安装在D:\\system\\https 下, D:\\system\\https\\Apache\\modplsql\\conf\\dads.conf文件. 设置为 : SetHandler pls\_handler Order deny,allow Allow from all AllowOverride None PlsqlDatabaseUsername dinya PlsqlDatabasePassword dinya PlsqlDatabaseConnectString dinya:1521:ora10g PlsqlAuthenticationMode Basic 当然对数据库的连接必须有效, 具体请见相关的设置文档. 重起Apache服务. --创建测试包: create or replace package cux\_html\_report as function t(p\_size in number, p\_content in varchar2) return varchar2 ; type c\_sql is ref cursor; procedure main; procedure html\_header(p\_title in varchar2); procedure html\_body(p\_sql in varchar2,p\_column\_count in number,p\_subtitle in varchar2,p\_align in varchar2,p\_black in varchar2); procedure html\_tail; procedure table\_h(p\_width in number,p\_border in number,p\_align in varchar2); procedure table\_t; procedure tr\_(p\_col\_count in number,p\_column in varchar2,p\_align in varchar2,p\_black in varchar2); procedure tr\_h; procedure tr\_t; procedure td\_(p\_col\_count in number,p\_content in varchar2,p\_align in varchar2,p\_black in varchar2); procedure report\_header(p\_title in varchar2); procedure report\_subtitle(p\_col\_count in number,p\_content in varchar2,p\_align in varchar2,p\_black in varchar2); procedure report\_tail(p\_content in varchar2); end ; / create or replace package body cux\_html\_report as function t(p\_size in number, p\_content in varchar2) return varchar2 as i number:=0; v\_out varchar2(300):=''; p\_cont varchar2(1000):=p\_content; begin for ii in 1..p\_size loop select substr(p\_cont,0,decode(instr(p\_cont,';'),0,length(p\_cont),instr(p\_cont,';')-1)) into v\_out from dual ; p\_cont:=substr(p\_cont,instr(p\_cont,';')+1); i:=i+1; if i=p\_size then return v\_out; end if; end loop; end ; /\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** 主程序 需要处理分页的问题 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/ procedure main as begin html\_header('我的测试报表'); report\_header('我的标题'); html\_body('select t.owner||'';''||t.object\_name||'';''||t.object\_id||'';''||t.object\_type||'';''||t.owner from all\_objects t where t.owner=''DINYA'' and rownum&lt;=100',5,'序号;对象名称;ID;类型','center',''); report\_tail('这里是报表尾'); html\_tail; end; --页头,固定格式 procedure html\_header(p\_title in varchar2) as begin htp.p(' '||nvl(trim(p\_title),'无标题文档')||' '); end ; --页体, 根据SQL程序和字段数,展示数据 procedure html\_body(p\_sql in varchar2,p\_column\_count in number,p\_subtitle in varchar2,p\_align in varchar2,p\_black in varchar2) as i number:=0; c c\_sql; v\_column varchar2(4000); begin table\_h(90,1,'center'); report\_subtitle(p\_column\_count,p\_subtitle,p\_align,p\_black); open c for p\_sql; loop fetch c into v\_column; exit when c%notfound; i:=i+1; tr\_(p\_column\_count,v\_column,'left',''); end loop; table\_t; end ; --页尾, 固定内容 procedure html\_tail as begin htp.p(' '); end ; -------------------------------------------------------------------------------- --表头, 表的属性 procedure table\_h(p\_width in number,p\_border in number,p\_align in varchar2) as begin htp.p(''); end ; --表尾 procedure table\_t as begin htp.p('

'); end ; -------------------------------------------------------------------------------- --表格行 -------------------------------------------------------------------------------- procedure tr\_(p\_col\_count in number,p\_column in varchar2,p\_align in varchar2,p\_black in varchar2) as begin htp.p(''); td\_(p\_col\_count,p\_column,p\_align,p\_black); htp.p(''); end; procedure tr\_h as begin htp.p(''); end ; procedure tr\_t as begin htp.p(''); end ; ------------------------------------------------------------------------------- --表单元格 ------------------------------------------------------------------------------- procedure td\_(p\_col\_count in number,p\_content in varchar2,p\_align in varchar2,p\_black in varchar2) as v\_cont varchar2(300):=p\_content; v\_out varchar2(300):=''; begin for i in 1..p\_col\_count loop select substr(v\_cont,0,decode(instr(v\_cont,';'),0,length(v\_cont),instr(v\_cont,';')-1)) into v\_out from dual ; htp.p(''||substr(p\_black,1,3)||v\_out||substr(p\_black,4)||''); v\_cont:=substr(v\_cont,instr(v\_cont,';')+1); end loop; end ; ------------------------------------------------------------------------------ --报表内容部分 ----------------------------------------------------------------------------- procedure report\_header(p\_title in varchar2) as begin table\_h(90,0,'center'); tr\_(1,p\_title,'center',''); table\_t; end ; procedure report\_subtitle(p\_col\_count in number,p\_content in varchar2,p\_align in varchar2,p\_black in varchar2) as begin tr\_h; td\_(p\_col\_count,p\_content,p\_align,p\_black); tr\_t; end ; procedure report\_tail(p\_content in varchar2) as begin table\_h(90,0,'center'); tr\_(1,p\_content,'left',''); table\_t; end; ---------------------------------------------------------------------------------- end ; / SQL&gt; @d:\\cux\_html\_report.pck Package created. Package body created. SQL&gt; 编译没有错误后, 使用http://dinya:7777/ss/cux\_html\_report.main 即可查看报表. 如果在main过程中使用 owa\_util.mime\_header(ccontent\_type =&gt; 'application/vnd.ms-excel'); 可以直接将数据生成到Excel中. 如: procedure main as begin owa\_util.mime\_header(ccontent\_type =&gt; 'application/vnd.ms-excel'); html\_header('我的测试报表'); 需要使用的话, 可以根据需要修改上面的程序.