question

Rahul R avatar image
0 Likes"
Rahul R asked Kavika F answered

Creating excel file at a specific location to copy global table using flexscript

I am trying to export global tables into an excel file (created at a specific location). excelsave with modeldir() does not seem to be working.

excelcreateworkbook();
excelsetsheet("Sheet1");
excelwritenum(1,1,1);
excelsave(modeldir() + "NewWorkBook.xlsx");
excelclose(1);
FlexSim 22.2.0
excel export
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Kavika F avatar image
0 Likes"
Kavika F answered

Hey @Rahul R, excelsave(...) will assume the directory you want to save in is your Documents directory. If you want to save your excel file in another folder (i.e., your model's directory), you have to use the relative path from your Documents directory to your desired folder. Here's an example block of code.

excelcreateworkbook();
excelsetsheet("Sheet1");
excelwritenum(1,1,1);
string pathToDir = "C:/Users/[user]/Pictures/test_folder/";

string key = "Pictures";
pathToDir = "../" + pathToDir.slice(pathToDir.indexOf(key));

excelsave(pathToDir + "TEST_BOOK.xlsx");
excelclose(0);

This makes a new workbook, writes '1' to the first cell, and then saves it to a file called "TEST_BOOK.xlsx". I want to save it in a test_folder in my Pictures directory. I know that this directory is on the same level as my Documents directory, so I prepend a "../" to my path, which tells the engine to "go up" one level; so I go up from "C:/Users/[user]/Documents" to "C:/Users/[user]/". I have to slice off the first part of the desired path (C:/Users/[user]/) because that's the same path to my Documents directory. Now I can add the rest of the path ("Pictures/test_folder/") and then add the file name (TEST_BOOK.xlsx).

If your path to the model's directory is within the Documents folder, you can simply slice off the first part (C:/Users/[user]/Documents/) and prepend the second part to the file name.

excelcreateworkbook();
excelsetsheet("Sheet1");
excelwritenum(1,1,1);
string pathToDir = modeldir();
pathToDir = pathToDir.slice(pathToDir.indexOf("Documents")+10);
excelsave(pathToDir + "TEST_BOOK.xlsx");
excelclose(0);
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.