Oracle中数据导出成HTML的操作实践

2023-05-20 14:23:07 浏览数 (1)

spool是Oracle中将数据到成文件常用的一种工具,但它的强大,不仅仅是数据的导出,在格式和内容上,还可以自定义,甚至生成像AWR一样的统计报告。

参考《SQL*Plus® User's Guide and Reference》中第7章"Generating HTML Reports from SQL*Plus"讲解的内容,可以有所了解。

P.S. 链接:

https://docs.oracle.com/cd/B19306_01/server.102/b14357/ch7.htm#CHDCECJG

提到了生成HTML格式的操作,

In addition to plain text output, the SQL*Plus command-line interface enables you to generate either a complete web page, or HTML output which can be embedded in a web page. You can use SQLPLUS -MARKUP "HTML ON" or SET MARKUP HTML ON SPOOL ON to produce complete HTML pages automatically encapsulated with and  tags. By default, data retrieved with MARKUP HTML ON is output in HTML, though you can optionally direct output to the HTML tag so that it displays in a web browser exactly as it appears in SQL*Plus. See the SQLPLUS MARKUP Options and the SET MARKUP command for more information about these commands. SQLPLUS -MARKUP "HTML ON" is useful when embedding SQLPlus in program scripts. On starting, it outputs the HTML and BODY tags before executing any commands. All subsequent output is in HTML until SQLPlus terminates. The -SILENT and -RESTRICT command-line options may be effectively used with -MARKUP to suppress the display of SQL*Plus prompt and banner information, and to restrict the use of some commands. SET MARKUP HTML ON SPOOL ON generates an HTML page for each subsequently spooled file. The HTML tags in a spool file are closed when SPOOL OFF is executed or SQL*Plus exits. You can use SET MARKUP HTML ON SPOOL OFF to generate HTML output suitable for embedding in an existing web page. HTML output generated this way has no  or  tags.

通过这个例子,可以看到,通过嵌入HTML的标签,以及自定义内容,结合SQL语句,生成了HTML的文件,

代码语言:javascript复制
SQL> SET MARKUP HTML ON SPOOL ON PREFORMAT OFF ENTMAP ON HEAD "<TITLE>Department Report</TITLE> <STYLE type='text/css'> <!-- BODY {background: #FFFFC6} --> </STYLE>" BODY "TEXT='#FF00Ff'" TABLE "WIDTH='90%' BORDER='5'"
SQL&gt; COLUMN DEPARTMENT_NAME HEADING 'DEPARTMENT' ENTMAP OFF
COLUMN CITY HEADING 'CITY'<br>
SQL&gt;
<br>
SQL&gt; SPOOL report.html
<br>
SQL&gt; SELECT '<A HREF="http://oracle.com/'||DEPARTMENT_NAME||'.html">'||DEPARTMENT_NAME||'</A>' DEPARTMENT_NAME, CITY
FROM EMP_DETAILS_VIEW
WHERE SALARY>12000; <br>
  2  <br>
  3
<br>
<p>
<table WIDTH='90%' BORDER='5'>
<tr>
<th scope="col">
DEPARTMENT
</th>
<th scope="col">
CITY
</th>
</tr>
<tr>
<td>
<A HREF="http://oracle.com/Executive.html">Executive</A>
</td>
<td>
Seattle
</td>
</tr>
<tr>
<td>
<A HREF="http://oracle.com/Executive.html">Executive</A>
</td>
<td>
Seattle
</td>
</tr>
<tr>
<td>
<A HREF="http://oracle.com/Executive.html">Executive</A>
</td>
<td>
Seattle
</td>
</tr>
<tr>
<td>
<A HREF="http://oracle.com/Finance.html">Finance</A>
</td>
<td>
Seattle
</td>
</tr>
<tr>
<td>
<A HREF="http://oracle.com/Sales.html">Sales</A>
</td>
<td>
Oxford
</td>
</tr>
<tr>
<td>
<A HREF="http://oracle.com/Sales.html">Sales</A>
</td>
<td>
Oxford
</td>
</tr>
<tr>
<td>
<A HREF="http://oracle.com/Marketing.html">Marketing</A>
</td>
<td>
Toronto
</td>
</tr>
<tr>
<td>
<A HREF="http://oracle.com/Accounting.html">Accounting</A>
</td>
<td>
Seattle
</td>
</tr>
</table>
<p>
 
 
8 rows selected.<br>
<br>
 
 
 
 
SQL&gt; SPOOL OFF
<br>

下载到Windows机器上,用浏览器打开,能看到这个界面,

通过这个spool小工具,我们可以实现很多的需求,成本很低,因此,像这些功能都是我们国产数据库值得借鉴学习的,毕竟数据库是让用户使用的,提供用户不同个性化、简洁的、易用的功能,就可以吸引用户,至少能让用户更加顺畅地用这款产品,所谓从客户出发,为客户着想,受益的可能不仅仅是客户自己了。

0 人点赞