#include"kutil.h" #include #include #include #include #include "libxl.h" #include #include #include #include #include #include #include #include #include #include #include #include "libxl.h" #include #include #include using namespace libxl; using namespace std::chrono_literals; using namespace std; #define debug true #define ITK_err 919012 string get_value(const map& value_map, const string& key) { auto iter = value_map.find(key); if (iter != value_map.end()) { return iter->second; } else { return ""; } } bool isSubstring(char* str1, char* str2) { int len1 = strlen(str1); int len2 = strlen(str2); int i = 0, j = 0; while (i < len1 && j < len2) { if (str1[i] == ',') { i++; continue; } if (str1[i] == str2[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } if (j == len2) { return true; } else { return false; } } int flag = 0; void insertDataToExcel2(char temp_file[SS_MAXPATHLEN], map dataList) { printf("----------------开始执行插入数据操作---------------------\n"); //将数据写入本地,使用Java插入数据 string json = "{"; for (auto it = dataList.begin(); it != dataList.end(); ++it) { json += "\"" + it->first + "\":\"" + it->second + "\","; } json.pop_back(); // 删除最后一个逗号 json += "}"; printf("json:%s", json.c_str()); // 将JSON格式的字符串写入文件 ofstream ofs("D:\\ldexcel\\output.txt"); ofs << json; ofs.close(); printf("开始执行cmd方法"); char cmd[1024] = ""; strcpy(cmd, "java -jar D:\\Siemens\\Teamcenter13\\bin\\writeAsExcel.jar"); strcat(cmd, " "); strcat(cmd, temp_file); strcat(cmd, " "); strcat(cmd, "D:\\ldexcel\\output.txt"); printf("\n%s\n", cmd); system(cmd); printf("cmd方法执行完毕"); for (auto it = dataList.begin(); it != dataList.end(); ++it) { cout << it->first << ": " << it->second << endl; } /*libxl::Book* book = xlCreateXMLBook(); printf("设置产品密匙\n"); book->setKey(L"Halil Kural", L"windows-2723210a07c4e90162b26966a8jcdboe");//设置密钥 if (book) { printf("开始\n"); printf("path%s\n", temp_file); int len = strlen(temp_file); int size = MultiByteToWideChar(CP_ACP, 0, temp_file, len, NULL, 0); // 获取所需空间大小 wstring wstr(size, 0); MultiByteToWideChar(CP_ACP, 0, temp_file, len, &wstr[0], size); // 转换为wchar_t数组 wprintf(L"path2%s", wstr.c_str()); // 使用wprintf输出 if (book->load(wstr.c_str())) { printf("加载Excel文件成功!\n"); printf("获取sheet页\n"); libxl::Sheet* sheet = book->getSheet(0); if (sheet) { map dataList; // 定义一个map对象 dataList["A1"] = "Hello"; dataList["B2"] = "world!"; for (auto& data : dataList) { // 遍历map中的数据 string cellName = data.first; // 获取单元格的名称 string cellValue = data.second; // 获取单元格的值 int row = sheet->nameToRow(cellName.c_str()); // 获取行号 int col = sheet->nameToColumn(cellName.c_str()); // 获取列号 sheet->writeStr(row, col, cellValue.c_str(), 0); // 在单元格中写入数据 } } } else { printf("加载Excel文件失败!错误码:%d\n", book->errorMessage()); } book->release(); }*/ } int LD_FormAttrToExcel(EPM_action_message_t msg) { printf("开始执行"); int att_cnt = 0; int doc_num = 0; tag_t rootTask_tag = NULLTAG; tag_t task = NULLTAG; tag_t *attachments = NULLTAG; tag_t *doc_tags = NULLTAG; string error; char *arg = NULL, *argflag = NULL, *argvalue = NULL; int arg_cnt = TC_number_of_arguments(msg.arguments); char *include_type = NULL, *relation = NULL, *formtype = NULL, *option = NULL; task = msg.task; ITKCALL(EPM_ask_root_task(task, &rootTask_tag)); if (arg_cnt > 0) { for (int i = 0; i < arg_cnt; i++) { arg = TC_next_argument(msg.arguments); ITKCALL(ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue)); if (strcmp(argflag, "include_type") == 0) { if (argvalue != NULL) { printf("获取的值%s:", argvalue); include_type = argvalue; } } if (strcmp(argflag, "relation") == 0) { if (argvalue != NULL) { printf("获取的值%s:", argvalue); relation = argvalue; } } if (strcmp(argflag, "formtype") == 0) { if (argvalue != NULL) { printf("获取的值%s:", argvalue); formtype = argvalue; } } if (strcmp(argflag, "Option") == 0) { if (argvalue != NULL) { printf("获取的值%s:", argvalue); option = argvalue; } } } //获取流程下目标关系下的对象 printf("获取流程下目标关系下的对象"); ITKCALL(EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &doc_num, &doc_tags)); for (int i = 0; i < doc_num; i++) { char *type = NULL; ITKCALL(AOM_ask_value_string(doc_tags[i], "object_type", &type)); if (strcmp(type, include_type) == 0) { //查找指定关系 printf("查找指定关系"); int gx_num; tag_t *gx_obj; map rowsvalue; ITKCALL(AOM_ask_value_tags(doc_tags[i], relation, &gx_num, &gx_obj)); for (int j = 0; j < gx_num; j++) { char *form = NULL; ITKCALL(AOM_ask_value_string(gx_obj[i], "object_type", &form)); printf("判断是否属于配置类型"); //判断是否属于配置类型 printf("formtype&s\n", formtype); printf("form&s\n", form); if (isSubstring(formtype, form)) { printf("获取表格属性"); //获取表格属性 int rownum; tag_t *tableRownum = NULL; ITKCALL(AOM_ask_table_rows(gx_obj[i], "ld6_ChangeTable", &rownum, &tableRownum)); printf("读取首选项的值"); int op_nums; char ** op_values = NULL; PREF_ask_char_values(option, &op_nums, &op_values); printf("获取表格数据"); printf("表格行数为%d\n", rownum); printf("首选项行数为%d\n", op_nums); for (int z = 0; z < rownum; z++) { printf("1"); string new_str; printf("2"); for (int a = 0; a < op_nums; a++) { printf("3"); char *getVal = op_values[a]; printf("4"); int serial_num; tag_t *tableRow = NULL; printf("5"); if (a == 0) { printf("6"); printf("表单中的序号属性,获取表单表格的序号"); vector ans; Split(getVal, ".", ans); if (ans.size() > 0) { tag_t rowTag = tableRownum[z]; char *xuhao; ITKCALL(AOM_ask_value_string(rowTag, ans[2].c_str(), &xuhao)); string pattern = "0(\\d)"; string replacement = "$1"; new_str = regex_replace(xuhao, regex(pattern), replacement); } } else { printf("后续行格式"); vector ans; Split(getVal, ".", ans); if (ans.size() > 1) { printf("将属性按照=分割"); vector getnas; Split(ans[2].c_str(), "=", getnas); tag_t rowTag = tableRownum[z]; char *value; if (strstr(getnas[0].c_str(), "Time")) { date_t getdate; ITKCALL(AOM_ask_value_date(rowTag, getnas[0].c_str(), &getdate)); char fileDate[128] = ""; sprintf_s(fileDate, "%04d-%02d-%02d 00:00", getdate.year, getdate.month + 1, getdate.day); value = fileDate; } else { ITKCALL(AOM_ask_value_string(rowTag, getnas[0].c_str(), &value)); } if (value == NULL) { value = ""; } if (strcmp(value, "0000-01-00 00:00") == 0) { value = ""; } string key = getnas[1] + new_str; std::string valueStr = value; std::size_t pos = valueStr.find('\n'); while (pos != std::string::npos) { valueStr.erase(pos, 1); pos = valueStr.find('\n', pos); } rowsvalue[key.c_str()] = valueStr; } else { //当外层循环走最后一遍的时候,获取表单上的属性 if (z == rownum - 1) { printf("获取表单属性"); vector getnas; Split(getVal, "=", getnas); char *value; ITKCALL(AOM_ask_value_string(gx_obj[i], getnas[0].c_str(), &value)); if (value == NULL) { value = ""; } std::string valueStr = value; std::size_t pos = valueStr.find('\n'); while (pos != std::string::npos) { valueStr.erase(pos, 1); pos = valueStr.find('\n', pos); } rowsvalue[getnas[1].c_str()] = valueStr; } } } } } printf("表格数据获取完毕。开始反填excel"); //表格数据获取完毕。开始反填excel printf("获取规范关系下的excel数据集"); int gf_num; tag_t *gf_obj; ITKCALL(AOM_ask_value_tags(doc_tags[i], "IMAN_specification", &gf_num, &gf_obj)); printf("获取到了%d个数据集", gf_num); for (int s = 0; s < gf_num; s++) { tag_t gettag = gf_obj[s]; char * objtype; char * objname; ITKCALL(AOM_ask_value_string(gettag, "object_type", &objtype)); ITKCALL(AOM_ask_value_string(gettag, "object_name", &objname)); printf("获取到了%s类型的数据集", objtype); if (strcmp(objtype, "MSExcelX") == 0) { printf("获取到了excel数据集"); tag_t ref_object = NULLTAG; printf("1"); AE_reference_type_t reference_type; printf("2"); char ref_name[WSO_name_size_c + 1] = "excel"; ITKCALL(AE_ask_dataset_named_ref2(gettag, ref_name, &reference_type, &ref_object)); printf("3"); //printf("reference_type%s", reference_type); if (reference_type == AE_PART_OF) { char *pathname = ""; ITKCALL(IMF_ask_file_pathname2(ref_object, SS_WNT_MACHINE, &pathname)); printf("4"); char *origin_file_name = ""; ITKCALL(IMF_ask_original_file_name2(ref_object, &origin_file_name)); printf("5"); char new_ds_name[WSO_name_size_c + 1] = ""; char *new_file_name = USER_new_file_name(new_ds_name, ref_name, "xlsx", 0); printf("6"); char *temp_dir = getenv("temp"); printf("7"); char temp_file[SS_MAXPATHLEN] = ""; strcpy(temp_file, "D:\\ldexcel"); strcat(temp_file, "\\"); strcat(temp_file, new_file_name); printf("temp_file%s", temp_file); ITKCALL(IMF_export_file(ref_object, temp_file)); printf("8"); printf("开始插入数据"); //string str(temp_file); insertDataToExcel2(temp_file, rowsvalue); //挂载数据集 printf("数据插入完成,开始挂载数据集"); ITKCALL(import_dataset_file_binary(gettag, temp_dir, "excel", "xlsx", temp_file, objname)); printf("执行结束"); } } } } } } } } return 0; }