我有一个像
这样的数据的ExcelName Roll_No Place
Mahesh 112 Hyd
Manish 118 Pune
Somesh 119 Lon
Abc 110 xyz
现在,如果我有一个基于滚动号码通过骡子获取学生的名字。 mule中的flowVar将具有类似于112的Roll_No,因此相应的名称将是Mahesh。
我在最新的mule版本中尝试使用DataWeave。下面是一段代码。
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<flow name="excelcheckFlow">
<file:inbound-endpoint path="C:\Users\rmishra\Desktop\PoCs" responseTimeout="10000" doc:name="File"/>
<logger message="-----------Read the content------#[payload]" level="INFO" doc:name="Logger"/>
<dw:transform-message metadata:id="74fc2359-be73-475f-a800-71ab941497ee" doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload map ((payload01 , indexOfPayload01) -> {
Name: payload01.Name,
Roll_No: payload01.Roll_No,
Place: payload01.Place
})]]></dw:set-payload>
</dw:transform-message>
<logger message="-----------#[payload.get(0).Name]-----#[payload.get(1).Name]---------" level="INFO" doc:name="Logger"/>
</flow>
</mule>
它给我一个错误。
答案 0 :(得分:1)
Dataweave还不支持从Excel(xls)直接转换。 所以,你有一些选择:
https://docs.mulesoft.com/mule-user-guide/v/3.7/using-dataweave-in-studio#configuring-the-csv-reader
如果您无法做到这一点,请在文件连接器使用Java自定义转换器将数据(使用POI库)转换为Java数组后再使用Dataweave。 (相当复杂的解决方案)
使用DataMapper进行转换(考虑到在未来版本的Mule中将完全弃用DataMapper,因此,当Dataweave在未来版本中具有此功能时,您将不得不迁移此转换)
答案 1 :(得分:0)
问题可能在于您的记录器。您是否尝试在Datawieve和Logger之间进行“Object-String”转换?
答案 2 :(得分:0)
我按照Davo的建议创建自定义变换器,并成功将excel转换为ArrayList,然后我必须将ArrayList转换为json,然后再进入dataweave进行转换。
我是Java新手,我将非常感谢任何评论和建议以改善我的答案。
@Override
public Object transformMessage(MuleMessage muleMessage, String outputEncoding) throws TransformerException
{
Map<String,String> keys = new HashMap<>();
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String,ArrayList<Map<String, String>>> outList = new HashMap<>();
int i = 0, j = 0;
try {
InputStream inp = (InputStream) muleMessage.getPayload();
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
i++;
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
if (i == 1)
{
j = 0;
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
keys.put("f"+j, cell.getStringCellValue());
j++;
}
}
else
{
Map<String,String> map = new HashMap<>();
j = 0;
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
String k = keys.get("f"+j);
String v = cell.getStringCellValue();
map.put(k, v);
j++;
}
list.add(map);
}
}
inp.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
outList.put("root",list);
return outList;
}
答案 3 :(得分:0)
对DataWeave(数据转换处理器)的Excel(XLSX文件)支持已经发布了新的Anypoint Studio 6.1.2版本w / 3.8.2运行时。
请在此处查看如何使用它:Excel format in DataWeave