question

Amanda Prado avatar image
0 Likes"
Amanda Prado asked Amanda Prado commented

Flexscript to import a Excel File

Hello All,

Could anyone help with a Script walkthrough how can I import data from Excel to a Global Table?

I would like my script read a excel table and generate a Global Table as per my excel.

I was trying with this following code, but it´s not working.



/**Custom Code*/ {

// Definir o diretrio inicial para buscar o arquivo Excel

string directory = modeldir();

if (stringlen(directory) < 3) directory = documentsdir();


// Abrir um dilogo para o usurio selecionar um arquivo Excel

string filename = filebrowse("*.xls*","Excel Files",directory);


// Verificar se o usurio cancelou a seleo

if (stringlen(filename) == 0) {

return 0; // Termina o script se nenhum arquivo foi selecionado

}


// Abrir o arquivo Excel

excelopen(filename);

excelsetsheet("Sheet1"); // Definir a planilha ativa (ajuste conforme necessrio)


// Criar uma tabela global

treenode table = applicationcommand("addglobaltable");

table.name = "Imported Data"; // Nome da tabela global

Table myTable = Table("Imported Data");


// Ler os cabealhos da primeira linha do Excel

int colCount = 0; // Contador de colunas

string header;


while (colCount < 256) { // Limitar a leitura a 256 colunas

header = excelreadstr(1, colCount + 1); // Ler cabealho da coluna

if (header == "") { // Se no houver mais cabealhos, sair do loop

break;

}

myTable.addCol(colCount + 1); // Adicionar coluna tabela

myTable.setColHeader(colCount + 1, header); // Definir cabealho da coluna

colCount++; // Incrementar contador de colunas

}


// Ler os dados do Excel e preencher a tabela

int StationRow = 1; // Iniciar na primeira linha da tabela global


for (int row = 2; row <= 1000; row++) { // Começar na linha 2 para ignorar cabealhos

string value1 = excelreadstr(row, 1); // Ler valor da primeira coluna

if (value1 == "") { // Se o valor estiver vazio, interromper a leitura

break;

}


// Adicionar uma nova linha tabela global

myTable.addRow(StationRow);

myTable.setRowHeader(StationRow, value1); // Definir cabealho da linha


// Ler e armazenar valores das colunas

for (int col = 1; col <= colCount; col++) { // Usar o contador de colunas

string value = excelreadstr(row, col);

if (stringlen(value) < 1) {

myTable[StationRow][col] = 0; // Se vazio, definir como 0

} else {

// Tentar converter o valor para nmero, se possvel

}

}

StationRow++; // Avanar para a prxima linha da tabela global

}


// Fechar o arquivo Excel

excelclose(0);


// Mensagem de concluso

msg("Excel Import", "Importao concluda com sucesso!", 1);

}

FlexSim 24.2.1
global tableflexscriptscriptexcel
· 5
5 |100000

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

Joerg Vogel avatar image Joerg Vogel commented ·

@Amanda Prado, do you need more data, than you can import by Excel import tool

0 Likes 0 ·
Amanda Prado avatar image Amanda Prado Joerg Vogel commented ·

But If want import by script?

0 Likes 0 ·
Amanda Prado avatar image Amanda Prado Amanda Prado commented ·

Could you tell me where I'm going wrong in my code to import the excel file into a global table?1731346867177.png

Thank you very much !

0 Likes 0 ·
1731346867177.png (50.2 KiB)
Felipe Capalbo avatar image Felipe Capalbo commented ·

Hello, Amanda.

The best approach is to configure the Excel Import/Export interface within FlexSim to ensure proper communication with the desired file and sheet. Once the setup is complete, you can use the excelmultitableimport(); command in the "On Model Reset" trigger to automate the process.

excelmultitableimport();

This command executes the Excel import based on the configuration you’ve already established. Let me know if you need further assistance!

You can see how to setup the Excel Interface here:
https://docs.flexsim.com/en/23.2/Reference/Tools/ExcelInterface/ExcelInterface.html

0 Likes 0 ·
Amanda Prado avatar image Amanda Prado Felipe Capalbo commented ·

Hello Felipe,

Thanks for your support, but unfortunately I have not understood.

Have you saw the code that I have sent here?

Could you tell me if I should just add this "excelmultitableimport();" command to the end of the code I sent?

Because I tried and it's not working.

Thank you very much !

0 Likes 0 ·

0 Answers