#Importing excel(.xlsx) file to access in loader

1 messages · Page 1 of 1 (latest)

noble folio
#

Hi
Im trying to load an excel file to use as a db from my loader.
this is my code right now, the data.xlsx file is in the app directory, I got the following error:
Ive tried to play with the name of the file a little but to no avail.
There is probably an appropriate way to do this hence my post.
Thanks in advance.

noble folio
#

Any help would be appreciated

flint agate
#

Maybe import the excel file as a resource (like remix does with other assets) and using the reference?

import excelFileUrl from "~/data.xlsx";
...
const f = XLSX.read(excelFileUrl); // or XLSX.readFile(excelFileUrl);
noble folio
#

I get the following error:

#

X [ERROR] No loader is configured for ".xlsx" files: app/data.xlsx

#

However if I'm importing a text file (still the same code as if it were a .xlsx file) it works.
I think the XLSX module does not preform a file type check and assumes its okay, and remix does not allow asset import for .xls or .xlsx files

flint agate
#

Of course, I'm stupid, assets a bundled, but you don't want your bundle to contain your datas, no?
Your data may evolve despite the build?
Than i can't see a better solution than to use an env variable that contains the path of your "data storage directory" and reconstruct access path from the env and your filename.
At least that's what I would do

noble folio
#

Excuse me but I don't quite get what you mean.
I understand you are saying it gets lost in bundling but what is your suggestion to proceed?

eternal knot
#

i often let browser read the xlsx file, then send json data to the server to do anything you want, then return your result

#

in the server i guess you can read file as string or base64 or binary then give that data to xlsx lib

sinful nova
#

The file path that readFile is looking for will be relative to the server build directory

#

If you copy your excel files into the build directory, that should fix it

wheat plaza
#

Ah right, you're using ~/data.xlsx which node doesn't actually understand, as its a typescript Alias

You'd need to do something like

const f = readFile("../data.xlsx")
wheat plaza
noble folio
#

In an attempt to cover everything I copied the xlsx file to the build directory too

#

I have also transferred the code to ex.server.ts to make things simple

#

the code:
`
import * as fs from "fs";
import { readFile, set_fs, read } from "xlsx";

export const ex = () => {
set_fs(fs);
const f = readFile("../data.xlsx")
console.log(f)
return f
};
`

wheat plaza
#

Because all the server stuff gets complied to index.js in the build folder

#

and data is next to it