2015-05-25 49 views
1

我想每天從表格中提取數據並將其保存在本地機器的Excel表格中。將excel表格數據保存到excel表單中的本地機器

+0

有很多方法。該流程需要多少自動化?產品是否需要採用Excel格式,或者僅僅是Excel打開並且格式可讀的東西? –

+0

產品需要以excel格式保存。 – Famy

+0

是的,但直接?即時創建CSV文件非常簡單。將CSV導入Excel並保存爲XLS(X)非常簡單。讓Oracle在沒有手動操作的情況下吐出一個XLS(X)不必太困難,但確切的路徑取決於您的需求。 Google'pl sql write to excel file' for some examples,並且考慮詢問一個更具體的問題。 –

回答

1

我想每天從表中提取數據

你可以使用DBMS_SCHEDULER創建工作時間表日常運行。

例如,下面的作業將每小時運行一次:

SQL> BEGIN 
    2 DBMS_SCHEDULER.DROP_JOB (JOB_NAME => 'test_full_job_definition'); 
    3 END; 
    4/

PL/SQL procedure successfully completed. 

SQL> 
SQL> BEGIN 
    2 DBMS_SCHEDULER.create_job (
    3  job_name  => 'test_full_job_definition', 
    4  job_type  => 'PLSQL_BLOCK', 
    5  job_action  => 'BEGIN my_job_procedure; END;', 
    6  start_date  => SYSTIMESTAMP, 
    7  repeat_interval => 'freq=hourly; byminute=0; bysecond=0;', 
    8  end_date  => NULL, 
    9  enabled   => TRUE, 
10  comments  => 'Job defined entirely by the CREATE JOB procedure.'); 
11 END; 
12/

PL/SQL procedure successfully completed. 

SQL> 
SQL> SELECT JOB_NAME, ENABLED FROM DBA_SCHEDULER_JOBS where job_name ='TEST_FULL_JOB_DEFINITION' 
    2/

JOB_NAME         ENABL 
---------------------------------------- ----- 
TEST_FULL_JOB_DEFINITION     TRUE 

SQL> 

將它保存在我的本地機器在Excel工作表

這是一個經常被問到的問題和人們經常在CSV文件和Microsoft Excel文件之間感到困惑。生成CSV文件可能很容易,因爲它使用SQL * Plus。要將輸出寫入Excel文件,您需要以編程方式執行

您可以使用下面的code開發的Anton Scheffer

CREATE OR REPLACE package as_xlsx 
is 
/********************************************** 
** 
** Author: Anton Scheffer 
** Date: 19-02-2011 
** Website: http://technology.amis.nl/blog 
** See also: http://technology.amis.nl/blog/?p=10995 
** 
** Changelog: 
** Date: 21-02-2011 
**  Added Aligment, horizontal, vertical, wrapText 
** Date: 06-03-2011 
**  Added Comments, MergeCells, fixed bug for dependency on NLS-settings 
** Date: 16-03-2011 
**  Added bold and italic fonts 
** Date: 22-03-2011 
**  Fixed issue with timezone's set to a region(name) instead of a offset 
** Date: 08-04-2011 
**  Fixed issue with XML-escaping from text 
** Date: 27-05-2011 
**  Added MIT-license 
** Date: 11-08-2011 
**  Fixed NLS-issue with column width 
** Date: 29-09-2011 
**  Added font color 
** Date: 16-10-2011 
**  fixed bug in add_string 
** Date: 26-04-2012 
**  Fixed set_autofilter (only one autofilter per sheet, added _xlnm._FilterDatabase) 
**  Added list_validation = drop-down 
** Date: 27-08-2013 
**  Added freeze_pane 
** 
****************************************************************************** 
****************************************************************************** 
Copyright (C) 2011, 2012 by Anton Scheffer 

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions: 

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE. 

****************************************************************************** 
******************************************** */ 
-- 
    type tp_alignment is record 
    (vertical varchar2(11) 
    , horizontal varchar2(16) 
    , wrapText boolean 
    ); 
-- 
    procedure clear_workbook; 
-- 
    procedure new_sheet(p_sheetname varchar2 := null); 
-- 
    function OraFmt2Excel(p_format varchar2 := null) 
    return varchar2; 
-- 
    function get_numFmt(p_format varchar2 := null) 
    return pls_integer; 
-- 
    function get_font 
    (p_name varchar2 
    , p_family pls_integer := 2 
    , p_fontsize number := 11 
    , p_theme pls_integer := 1 
    , p_underline boolean := false 
    , p_italic boolean := false 
    , p_bold boolean := false 
    , p_rgb varchar2 := null -- this is a hex ALPHA Red Green Blue value 
    ) 
    return pls_integer; 
-- 
    function get_fill 
    (p_patternType varchar2 
    , p_fgRGB varchar2 := null -- this is a hex ALPHA Red Green Blue value 
    ) 
    return pls_integer; 
-- 
    function get_border 
    (p_top varchar2 := 'thin' 
    , p_bottom varchar2 := 'thin' 
    , p_left varchar2 := 'thin' 
    , p_right varchar2 := 'thin' 
    ) 
/* 
none 
thin 
medium 
dashed 
dotted 
thick 
double 
hair 
mediumDashed 
dashDot 
mediumDashDot 
dashDotDot 
mediumDashDotDot 
slantDashDot 
*/ 
    return pls_integer; 
-- 
    function get_alignment 
    (p_vertical varchar2 := null 
    , p_horizontal varchar2 := null 
    , p_wrapText boolean := null 
    ) 
/* horizontal 
center 
centerContinuous 
distributed 
fill 
general 
justify 
left 
right 
*/ 
/* vertical 
bottom 
center 
distributed 
justify 
top 
*/ 
    return tp_alignment; 
-- 
    procedure cell 
    (p_col pls_integer 
    , p_row pls_integer 
    , p_value number 
    , p_numFmtId pls_integer := null 
    , p_fontId pls_integer := null 
    , p_fillId pls_integer := null 
    , p_borderId pls_integer := null 
    , p_alignment tp_alignment := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure cell 
    (p_col pls_integer 
    , p_row pls_integer 
    , p_value varchar2 
    , p_numFmtId pls_integer := null 
    , p_fontId pls_integer := null 
    , p_fillId pls_integer := null 
    , p_borderId pls_integer := null 
    , p_alignment tp_alignment := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure cell 
    (p_col pls_integer 
    , p_row pls_integer 
    , p_value date 
    , p_numFmtId pls_integer := null 
    , p_fontId pls_integer := null 
    , p_fillId pls_integer := null 
    , p_borderId pls_integer := null 
    , p_alignment tp_alignment := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure hyperlink 
    (p_col pls_integer 
    , p_row pls_integer 
    , p_url varchar2 
    , p_value varchar2 := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure comment 
    (p_col pls_integer 
    , p_row pls_integer 
    , p_text varchar2 
    , p_author varchar2 := null 
    , p_width pls_integer := 150 -- pixels 
    , p_height pls_integer := 100 -- pixels 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure mergecells 
    (p_tl_col pls_integer -- top left 
    , p_tl_row pls_integer 
    , p_br_col pls_integer -- bottom right 
    , p_br_row pls_integer 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure list_validation 
    (p_sqref_col pls_integer 
    , p_sqref_row pls_integer 
    , p_tl_col pls_integer -- top left 
    , p_tl_row pls_integer 
    , p_br_col pls_integer -- bottom right 
    , p_br_row pls_integer 
    , p_style varchar2 := 'stop' -- stop, warning, information 
    , p_title varchar2 := null 
    , p_prompt varchar := null 
    , p_show_error boolean := false 
    , p_error_title varchar2 := null 
    , p_error_txt varchar2 := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure list_validation 
    (p_sqref_col pls_integer 
    , p_sqref_row pls_integer 
    , p_defined_name varchar2 
    , p_style varchar2 := 'stop' -- stop, warning, information 
    , p_title varchar2 := null 
    , p_prompt varchar := null 
    , p_show_error boolean := false 
    , p_error_title varchar2 := null 
    , p_error_txt varchar2 := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure defined_name 
    (p_tl_col pls_integer -- top left 
    , p_tl_row pls_integer 
    , p_br_col pls_integer -- bottom right 
    , p_br_row pls_integer 
    , p_name varchar2 
    , p_sheet pls_integer := null 
    , p_localsheet pls_integer := null 
    ); 
-- 
    procedure set_column_width 
    (p_col pls_integer 
    , p_width number 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure set_column 
    (p_col pls_integer 
    , p_numFmtId pls_integer := null 
    , p_fontId pls_integer := null 
    , p_fillId pls_integer := null 
    , p_borderId pls_integer := null 
    , p_alignment tp_alignment := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure set_row 
    (p_row pls_integer 
    , p_numFmtId pls_integer := null 
    , p_fontId pls_integer := null 
    , p_fillId pls_integer := null 
    , p_borderId pls_integer := null 
    , p_alignment tp_alignment := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure freeze_rows 
    (p_nr_rows pls_integer := 1 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure freeze_cols 
    (p_nr_cols pls_integer := 1 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure freeze_pane 
    (p_col pls_integer 
    , p_row pls_integer 
    , p_sheet pls_integer := null 
    ); 
-- 
    procedure set_autofilter 
    (p_column_start pls_integer := null 
    , p_column_end pls_integer := null 
    , p_row_start pls_integer := null 
    , p_row_end pls_integer := null 
    , p_sheet pls_integer := null 
    ); 
-- 
    function finish 
    return blob; 
-- 
    procedure save 
    (p_directory varchar2 
    , p_filename varchar2 
    ); 
-- 
    procedure query2sheet 
    (p_sql varchar2 
    , p_column_headers boolean := true 
    , p_directory varchar2 := null 
    , p_filename varchar2 := null 
    , p_sheet pls_integer := null 
    ); 

注:它與代碼超過90K PL/SQL行龐大的代碼,所以我不能發佈整個代碼這裏由於限制了30k個字符。只發布重要的程序,功能和包裝細節。請按照代碼鏈接獲取完整的代碼。

+0

嘿Lalit,我知道關於dbms_scheduler部分。我的問題是我如何將桌面數據發送到桌面的C盤。你能否建議我一些簡短的方法。我很感謝你的努力,但是我必須在今天之前完成,上面的代碼將花費很多時間來分析和實施。希望很快聽到你的聲音。 – Famy

+0

@Famy我認爲實現上面的代碼不會浪費時間,因爲有示例提供瞭如何使用它。您只需要傳遞目錄名稱等。 –

0

最低要求的辦法是達利一些計劃作業

create table myexportable as (select sysdate exportdate, q.* from mytable q) 

RESP插入表中的數據爲一些臨時表。

insert into myexportable (select sysdate exportdate, q.* from mytable q) 

,然後利用SQL Plus中

set serveroutput on; 
set linesize 10000; 
set pagesize 10000; 
set heading off; 
set feed 0; 
set trimspool on; 
set colsep '|'; 

spool exptable.out; 

select * from myexportable where trunc(exportdate) = trunc(sysdate) 
/
quit; 

導出數據,並將它們至少進口管道分隔文本文件導入Excel。

+0

@Lalit Kumar B下面給出的代碼是在本地機器中將數據提取爲csv格式。但任何想法如何安排它? – Famy

+0

線軸C:\每日\ EXCEL \ EXCEL \ emp3.csv SET SERVEROUTPUT ON SIZE 100000 SET FEEDBACK OFF SET TERMOUT OFF SET PAGESIZE 0 SET LINESIZE 2000 DECLARE CURSOR c_emp是 從SELECT as_mst_doc_name swiftdba.as_doc_type $ T; 開始 for c_emp中的r_emp 循環 dbms_output.put_line(r_emp.as_mst_doc_name); end loop; end; / SPOOL OFF – Famy

相關問題