Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

J'essaye de lire dans un fichier Excel en utilisant EPPLUS pour enregistrer les valeurs par la suite en base.

Je n'ai aucun problème en local mais ça ne fonctionne pas sur le serveur IIS.
En ajoutant des conditions et des logs je me suis aperçu que package.Workbook.Worksheets est null (lors de l'exécution sur le serveur) et je n'arrive pas à comprendre pourquoi.

Any idea please


Translation:


I try to read in an Excel file using EPPLUS to save the values afterwards in base.

I have no problems locally but it doesn't work on the IIS server.
By adding conditions and logs I realized that package. Workbook.Worksheets is null (when running on the server) and I can't figure out why.

What I have tried:

C#
protected void btnUpload_Click(object sender, EventArgs e)
        {
            string ext = FileUpload1.FileName.ToLower();
            string path = FileUpload1.PostedFile.FileName;
            
            
            FileInfo fileInfo = new FileInfo(path);
            if (fileInfo == null)
            {
                throw new Exception(" file info is null");
            }
            //ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
            ExcelPackage package = new ExcelPackage(fileInfo);
            ExcelWorksheet worksheet;
            if (package.Workbook != null) { 
            worksheet = package.Workbook.Worksheets["Liquidité BAIL"];
            }
            

            if (null != worksheet)
            {
Posted
Updated 16-Aug-21 0:26am
v2
Comments
PIEBALDconsult 13-Aug-21 11:12am    
I haven't used EPPLUS for a while... does it require that Excel be installed on the system? Servers generally do not have Office installed.
Richard Deeming 13-Aug-21 11:33am    
EPPlus doesn't require or use Excel. :)
Wajdi Boulifa 16-Aug-21 6:19am    
Exact:)
Richard Deeming 13-Aug-21 11:34am    
Probably best to ask the developers directly - especially since they're now charging for commercial use:
Issues · EPPlusSoftware/EPPlus · GitHub[^]

Quote:
The file is selected by the user; so it necessarily exists when I had done my test (I selected it myself) .. FileUpload1.FileName.ToLower ();
NB: FileUpload1.FileName refers to the path - or often just the name - of the file on the user's computer. Your code is running on your server. Code running on the server has no access to files stored on the user's computer.

Your code appears to work when you run it locally. But that's only because, in that specific case, the server and client are the same computer. When you deploy your code to a real server, your code cannot access the files stored on the client.

As an analogy, if I told you my scissors are in the third drawer down in the kitchen, that would be correct for my house. But if you went to the third drawer down in your kitchen, you probably wouldn't find any scissors. And even if you did, they wouldn't be the same scissors I was referring to.

When the user uploads a file, you have to either save that file to a folder on your server, or process the InputStream in-memory. You can't simply open the FileName and expect it to work.
 
Share this answer
 
v2
Nothing in your code actually verifies that a file exists (only that you can create one); and the rest will just run on defaults.

https://docs.microsoft.com/en-us/dotnet/api/system.io.file.exists?view=net-5.0
 
Share this answer
 
Comments
Wajdi Boulifa 16-Aug-21 6:18am    
Bonjour et merci pour ton retour :)
Le fichier est sélectionné par l'utilisateur; donc il existe forcement lorsque j'avais fait mon test( je l'ai sélectionné moi même) .. FileUpload1.FileName.ToLower();
certes je vais ajouter des tests après pour vérifier que le fichier a été bien sélectionné, mais ce n'est pas le problème pour le moment

Translation:
Hello and thank you for your feedback :)
The file is selected by the user; so it necessarily exists when I had done my test (I selected it myself) .. FileUpload1.FileName.ToLower ();
of course I will add tests after to verify that the file has been correctly selected, but that's not the problem for the moment
Richard Deeming 16-Aug-21 6:25am    
En anglais s'il vous plait. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900