想通过 pl/sql 程序将 excel 作为邮件附件发送

Want to send excel as mail attachment through pl/sql procedure

当我通过使用 clob 创建数据的过程发送带有附件的邮件时。 对于小数据,它工作正常。但对于大数据,它的行为不当(对齐方式改变)。你能帮忙看看是否需要对代码做些什么吗?

 create or replace PROCEDURE RPT AS 
  l_clob clob; 
  l_bfile   bfile;
    l_fhandle utl_file.file_type;
  l_buffer  VARCHAR2(8192);
v_count number;
 a_count number;
   k_count number;
     ka_count number;
      currentdate DATE;
     non_work_days_count number;
    BEGIN
   --preparing header
         dbms_lob.createtemporary (l_clob, TRUE); 
 l_clob := l_clob 
      || 'Column1'|| ','
      || 'Column2' || ','
      || 'Column3' || ','
      || 'Column4' || ','
      || UTL_TCP.crlf;

 for crq in (select col1,col2,col3,col4 from table where id=1 ) loop
   /* Prepare Details data using Clob */   
 l_clob := l_clob 
      || to_clob(crq.COl1)|| ','
      || to_clob(crq.COL2) || ','
      || to_clob(crq.COL3) || ','
      || to_clob(crq.COL4) || ','
      || UTL_TCP.crlf;
      end loop; 

      for crq in (select col1,col2,col3,col4 from table where id=2 ) loop
    /* Prepare Details data using Clob */   
     l_clob := l_clob 
      || to_clob(crq.COl1)|| ','
      || to_clob(crq.COL2) || ','
      || to_clob(crq.COL3) || ','
      || to_clob(crq.COL4) || ','
      || UTL_TCP.crlf;
         end loop; 
    dbms_output.put_line('Sending mail with attachment ');
          ATTACHMENT_SEND(p_to=> 'req@Email.com',
            p_from=> 'req@Email.com',
            p_subject=> ' Report ',
            p_text_msg=>'Hi All',   
             p_attach_name =>'report_'||sysdate||'.csv',
            p_attach_mime =>'text/plain',
            p_attach_clob =>l_clob,
            p_smtp_host=>'host.com');


   dbms_lob.freetemporary(l_clob); 


      END RPT;

我还观察到当字符长度达到 32000 时它会中断(留下剩余数据)。

创建的 excel 附件中的数据存在对齐问题

|| 在 Oracle SQL 中连接最多限制为 4000,而在 PL/SQL 中限制为 32000。您可以尝试 [DBMS_LOB.APPEND][1] 过程来连接长 CLOB。因此,您可以尝试将代码转换为 -

CREATE OR REPLACE PROCEDURE RPT AS
l_clob              clob; 
l_bfile             bfile;
l_fhandle           utl_file.file_type;
l_buffer            VARCHAR2(8192);
v_count             number;
a_count             number;
k_count             number;
ka_count            number;
currentdate         DATE;
non_work_days_count number;
BEGIN
   --preparing header
      dbms_lob.createtemporary (l_clob, TRUE); 
      l_clob := l_clob 
      || 'Column1'|| ','
      || 'Column2' || ','
      || 'Column3' || ','
      || 'Column4' || ','
      || UTL_TCP.crlf;

for crq in (select col1,col2,col3,col4 from table where id=1 ) loop
   /* Prepare Details data using Clob */   
l_clob := DBMS_LOB.APPEND(
              DBMS_LOB.APPEND(
                  DBMS_LOB.APPEND(
                      DBMS_LOB.APPEND(l_clob
                                     ,to_clob(crq.COl1) || ',')
                     ,to_clob(crq.COL2) || ',')
                 ,to_clob(crq.COL3) || ',')
             ,to_clob(crq.COL4) || ',' || UTL_TCP.crlf);
      end loop; 

for crq in (select col1,col2,col3,col4 from table where id=2 ) loop
    /* Prepare Details data using Clob */   
l_clob := DBMS_LOB.APPEND(
              DBMS_LOB.APPEND(
                  DBMS_LOB.APPEND(
                      DBMS_LOB.APPEND(l_clob
                                     ,to_clob(crq.COl1) || ',')
                     ,to_clob(crq.COL2) || ',')
                 ,to_clob(crq.COL3) || ',')
             ,to_clob(crq.COL4) || ',' || UTL_TCP.crlf);
      end loop; 
dbms_output.put_line('Sending mail with attachment ');

ATTACHMENT_SEND(p_to          => 'req@Email.com',
                p_from        => 'req@Email.com',
                p_subject     => ' Report ',
                p_text_msg    =>'Hi All',   
                p_attach_name =>'report_'||sysdate||'.csv',
                p_attach_mime =>'text/plain',
                p_attach_clob =>l_clob,
                p_smtp_host   =>'host.com');

dbms_lob.freetemporary(l_clob); 

END RPT;

[1]: https://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lob.htm#i997432

经过大量搜索后,我发现了一种不同的方法,它对我来说是一种 gem(可以处理数十万条记录)。我想有一天它会对某人有所帮助。

步骤:

1) 创建了一个目录(其中存储了预期的 excel)。 2)将select语句的输出写入上述文件。 3)在上面的位置选择文件并在邮件附件中发送。

注意:如果有人需要 SMTP 代码部分或任何帮助,请告诉我