JAVA插件开发,Excel文件转JSON失败问题?
本帖最后由 骑士归来 于 2024-6-4 10:31 编辑package org.example;
import com.grapecity.forguncy.commands.ICommandExecutableInServerSide;
import com.grapecity.forguncy.commands.IServerCommandExecuteContext;
import com.grapecity.forguncy.commands.annotation.ResultToProperty;
import com.grapecity.forguncy.commands.entity.Command;
import com.grapecity.forguncy.commands.entity.ExecuteResult;
import com.grapecity.forguncy.plugincommon.common.annotation.DisplayName;
import com.grapecity.forguncy.plugincommon.common.annotation.FormulaProperty;
import com.grapecity.forguncy.plugincommon.common.annotation.Icon;
import com.grapecity.forguncy.plugincommon.common.annotation.Required;
import lombok.Data;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@Icon("resources/Icon.png")
public class MyPlugin11 extends Command implements ICommandExecutableInServerSide {
@Required
@FormulaProperty
@DisplayName("Excel路径")
private Object addJsonArray;
@ResultToProperty
@FormulaProperty
@DisplayName("JSON结果")
private String resultTo = "结果";
@Override
public ExecuteResult execute(IServerCommandExecuteContext context) {
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String, Object>> resultList = new ArrayList<>();
try {
// Get the Excel file path from the input parameter
String excelFilePath = addJsonArray.toString();
// Read the Excel file
InputStream excelFile = new FileInputStream(excelFilePath);
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet sheet = workbook.getSheetAt(0);
// Iterate through each row in the Excel file
for (Row row : sheet) {
Map<String, Object> rowMap = new HashMap<>();
for (Cell cell : row) {
// Get the cell value and add it to the row map
String cellValue = cell.getStringCellValue();
rowMap.put(cell.getColumnIndex() + "", cellValue);
}
resultList.add(rowMap);
}
// Convert the result list to JSON
String jsonResult = objectMapper.writeValueAsString(resultList);
// Set the formatted JSON result to the resultTo property
setResultTo(jsonResult);
return new ExecuteResult(1, "Success");
} catch (IOException | JSONException e) {
e.printStackTrace();
return new ExecuteResult(0, "Error occurred while processing the Excel file");
}
}
@Override
public String toString() {
return "Excel转JSON";
}
}
有没有会的帮我分析下咋回事
电商订单导入 用时: 323 毫秒
forguncy.js?v=638490375880000000:31 创建变量 结果 = 结果
forguncy.js?v=638490375880000000:31 18:04 执行'电商订单导入'命令, 请求方式:POST, 用户:Administrator
forguncy.js?v=638490375880000000:31 参数:
forguncy.js?v=638490375880000000:31 文件 = "47872513-6be6-453b-aa55-f23b1fe3ee28_测试文件.xlsx"
forguncy.js?v=638490375880000000:31 获取数据库附件中的文件路径 用时:6毫秒。
forguncy.js?v=638490375880000000:31 创建变量 文件路径 = ["C:\\ProgramData\\Forguncy\\ChestNut\\3433\\WebSite\\Upload\\Temp\\47872513-6be6-453b-aa55-f23b1fe3ee28_测试文件.xlsx"]
forguncy.js?v=638490375880000000:31 Excel转JSON 用时:100毫秒。
forguncy.js?v=638490375880000000:31 返回命令 用时:3毫秒。
forguncy.js?v=638490375880000000:31 错误码: 0
forguncy.js?v=638490375880000000:31 信息: 结果
forguncy.js?v=638490375880000000:31 '电商订单导入'命令执行结束, 用时:304毫秒。excel里面只有一列数据
前端截图如下:
直接debug挂java代码
Remote 这里显示的端口号就行了 :hjyzw:
页:
[1]