找回密码
 立即注册

QQ登录

只需一步,快速开始

骑士归来

初级会员

36

主题

84

帖子

414

积分

初级会员

积分
414
骑士归来
初级会员   /  发表于:2024-6-3 16:49  /   查看:267  /  回复:3
100金币
本帖最后由 骑士归来 于 2024-6-4 10:31 编辑
  1. package org.example;

  2. import com.grapecity.forguncy.commands.ICommandExecutableInServerSide;
  3. import com.grapecity.forguncy.commands.IServerCommandExecuteContext;
  4. import com.grapecity.forguncy.commands.annotation.ResultToProperty;
  5. import com.grapecity.forguncy.commands.entity.Command;
  6. import com.grapecity.forguncy.commands.entity.ExecuteResult;
  7. import com.grapecity.forguncy.plugincommon.common.annotation.DisplayName;
  8. import com.grapecity.forguncy.plugincommon.common.annotation.FormulaProperty;
  9. import com.grapecity.forguncy.plugincommon.common.annotation.Icon;
  10. import com.grapecity.forguncy.plugincommon.common.annotation.Required;
  11. import lombok.Data;
  12. import org.apache.poi.ss.usermodel.*;
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  14. import com.fasterxml.jackson.databind.ObjectMapper;
  15. import org.json.JSONArray;
  16. import org.json.JSONException;

  17. import java.io.FileInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;

  24. @Data
  25. @Icon("resources/Icon.png")
  26. public class MyPlugin11 extends Command implements ICommandExecutableInServerSide {

  27.     @Required
  28.     @FormulaProperty
  29.     @DisplayName("Excel路径")
  30.     private Object addJsonArray;

  31.     @ResultToProperty
  32.     @FormulaProperty
  33.     @DisplayName("JSON结果")
  34.     private String resultTo = "结果";

  35.     @Override
  36.     public ExecuteResult execute(IServerCommandExecuteContext context) {
  37.         ObjectMapper objectMapper = new ObjectMapper();
  38.         List<Map<String, Object>> resultList = new ArrayList<>();

  39.         try {
  40.             // Get the Excel file path from the input parameter
  41.             String excelFilePath = addJsonArray.toString();

  42.             // Read the Excel file
  43.             InputStream excelFile = new FileInputStream(excelFilePath);
  44.             Workbook workbook = new XSSFWorkbook(excelFile);
  45.             Sheet sheet = workbook.getSheetAt(0);

  46.             // Iterate through each row in the Excel file
  47.             for (Row row : sheet) {
  48.                 Map<String, Object> rowMap = new HashMap<>();
  49.                 for (Cell cell : row) {
  50.                     // Get the cell value and add it to the row map
  51.                     String cellValue = cell.getStringCellValue();
  52.                     rowMap.put(cell.getColumnIndex() + "", cellValue);
  53.                 }
  54.                 resultList.add(rowMap);
  55.             }

  56.             // Convert the result list to JSON
  57.             String jsonResult = objectMapper.writeValueAsString(resultList);

  58.             // Set the formatted JSON result to the resultTo property
  59.             setResultTo(jsonResult);
  60.             return new ExecuteResult(1, "Success");
  61.         } catch (IOException | JSONException e) {
  62.             e.printStackTrace();
  63.             return new ExecuteResult(0, "Error occurred while processing the Excel file");
  64.         }
  65.     }


  66.     @Override
  67.     public String toString() {
  68.         return "Excel转JSON";
  69.     }
  70. }
复制代码
有没有会的帮我分析下咋回事

  1. 电商订单导入 用时: 323 毫秒
  2. forguncy.js?v=638490375880000000:31 创建变量 结果 = 结果
  3. forguncy.js?v=638490375880000000:31 18:04 执行'电商订单导入'命令, 请求方式:POST, 用户:Administrator
  4. forguncy.js?v=638490375880000000:31 参数:
  5. forguncy.js?v=638490375880000000:31 文件 = "47872513-6be6-453b-aa55-f23b1fe3ee28_测试文件.xlsx"
  6. forguncy.js?v=638490375880000000:31 获取数据库附件中的文件路径 用时:6毫秒。
  7. forguncy.js?v=638490375880000000:31 创建变量 文件路径 = ["C:\\ProgramData\\Forguncy\\ChestNut\\3433\\WebSite\\Upload\\Temp\\47872513-6be6-453b-aa55-f23b1fe3ee28_测试文件.xlsx"]
  8. forguncy.js?v=638490375880000000:31 Excel转JSON 用时:100毫秒。
  9. forguncy.js?v=638490375880000000:31 返回命令 用时:3毫秒。
  10. forguncy.js?v=638490375880000000:31 错误码: 0
  11. forguncy.js?v=638490375880000000:31 信息: 结果
  12. forguncy.js?v=638490375880000000:31 '电商订单导入'命令执行结束, 用时:304毫秒。
复制代码
excel里面只有一列数据




前端截图如下:

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册

3 个回复

倒序浏览
骑士归来
初级会员   /  发表于:2024-6-3 16:56:07
沙发

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
Ben.C
注册会员   /  发表于:2024-6-4 09:23:02
板凳
直接debug挂java代码

Remote 这里显示的端口号就行了

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
Nathan.guo活字格认证 Wyn认证
超级版主   /  发表于:2024-6-5 09:02:01
地板
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部