在 Mulesoft

问题描述

我试图在 Mulesoft 中使用“新建或更新文件”节点将 .xslx 文件转换为 CSV

Input Mime Type 设置为 application/xlsx 并添加一个设置为 True 的参数“ignoreEmptyLine”。

但似乎在我的输出 CSV 文件中,我仍然得到空记录。

是否需要在输入节点设置另一个参数?

<flow name="excel2csvFlow" doc:id="0539d502-fef3-40c4-8aff-f35ce56304e8" >
        <file:listener doc:name="On New or Updated File" doc:id="2383c725-9ce0-460f-a10e-8a2707174650" config-ref="File_Config" directory="C:\Users\Darshan Vaswani\Desktop\Test\input" recursive="false" outputMimeType="application/xlsx; ignoreemptyline=true" movetoDirectory="C:\Users\Darshan Vaswani\Desktop\Test\archive">
            <scheduling-strategy >
                <fixed-frequency />
            </scheduling-strategy>
            <file:matcher />
        </file:listener>
        <ee:transform doc:name="Transform Message" doc:id="9cd75403-0552-412e-a22e-8d7d1215d1d0" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
                                        output application/csv ignoreEmptyLine=true
                                        ---
                                    payload."Daily report international"]]>
                </ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger" doc:id="decd4579-03a3-48ca-bb5a-895885b679a2" message="#[payload]"/>
        <file:write doc:name="Write" doc:id="55ecc67b-fd53-45af-98f5-4a08e663e208" config-ref="File_Config" path="C:\Users\Darshan Vaswani\Desktop\Test\output\test.csv"/>
</flow> 

输出

output of my code

解决方法

ignoreEmptyLine 的工作原理是从 Excel 文档中删除该行(如果完全为空)。如果一行中的任何单元格有空格,则它们不会被删除。您可以做的是在写入 CSV 之前在转换步骤中将它们过滤掉。如果你的 excel 文档太大,这可能会导致一些性能问题,但我认为如果是这种情况,你可以利用 Mule 4 的流功能。对于过滤逻辑,你可以按照以下操作:

%dw 2.0
output application/csv
import someEntry from dw::core::Objects
import isWhitespace from dw::core::Strings

--- 
payload."Daily report international" filter (
        $ someEntry (value,key) -> !(isWhitespace(value))
    )

请注意,我使用了 someEntry (https://docs.mulesoft.com/mule-runtime/4.3/dw-objects-functions-someentry),因此它在第一次出现任何非空白值时已经返回 true,并且不需要检查行中的其他条目。 isWhitespace (https://docs.mulesoft.com/mule-runtime/4.3/dw-strings-functions-iswhitespace) 用于检查该值是否包含除空格之外的其他字符。