簡體   English   中英

使用java生成excel並在excel中創建宏

[英]Generate a excel using java and create a macro in the excel

我有一個 java 代碼,它從 MongoDB 獲取數據,然后使用 Apache POI 創建一個包含此數據的 excel(.xls),但以格式化的方式。

我的最終要求是將 Excel 工作表中的最后一個工作表郵寄到一組郵件 ID。 我無法使用 Java 郵件 API 來執行此操作,因為不會向我提供郵箱的 SMTP 詳細信息。 截至目前,我計划在生成的 excel 中創建一個宏來發送數據。 我創建的用於發送郵件的宏是:

Sub Send_Selection_Or_ActiveSheet_with_MailEnvelope()
'Working in Excel 2002-2016
    Dim Sendrng As Range

    On Error GoTo StopMacro

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Note: if the selection is one cell it will send the whole worksheet
    Set Sendrng = Selection

    'Create the mail and send it
    With Sendrng

        ActiveWorkbook.EnvelopeVisible = True
        With .Parent.MailEnvelope

            ' Set the optional introduction field thats adds
            ' some header text to the email body.
            .Introduction = "This is a test mail."

            With .Item
                .To = "iamnithinprakash@gmail.com"
                .Subject = "My subject"
                .Send
            End With

        End With
    End With

StopMacro:
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    ActiveWorkbook.EnvelopeVisible = False

End Sub

但我不知道如何使用 java 創建這個宏。

OP 詢問如何使用包含宏的 Apache POI 庫創建 Excel 工作表。 不幸的是:這是不可能的。

引用 POI 限制

無法創建宏。 目前沒有計划支持宏。

幸運的是,它繼續:

但是,讀取和重寫包含宏的文件將安全地保留宏。

那么,什么可以解決:

  • 您使用 Excel“手動”創建一個空的Excel 工作表,其中包含您的宏
  • 您使用 POI數據添加到此類現有工作表中
  • 你把那個“一起”保存到一個新的工作表中

(或者它的一些變體,比如創建那個空模板,復制它,然后打開/更新其中一個副本)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM