# Issues Importing Excel Files with XLSX.jl Package

**URL:** <https://discourse.julialang.org/t/issues-importing-excel-files-with-xlsx-jl-package/114717>\
**Category:** Data\
**Tags:** question, data, xlsx, excel\
**Created:** [May 25, 2024, 12:18pm UTC](https://discourse.julialang.org/t/issues-importing-excel-files-with-xlsx-jl-package/114717 "2024-05-25T12:18:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil25](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@phil25](https://discourse.julialang.org/u/phil25)\
**Post date:** [May 25, 2024, 12:18pm UTC](https://discourse.julialang.org/t/issues-importing-excel-files-with-xlsx-jl-package/114717/1 "2024-05-25T12:18:34Z")

</div>

Dear all,

I’m currently using the `XLSX.jl` package in Julia to import several Excel files. Unfortunately, I don’t have access to the original sources of these files and have to work with the existing versions.

I’ve encountered an issue where I must open the files in Excel and save them again before I can import them into Julia. If I don’t, I receive the following error:

AssertionError: Couldn’t find xl/worksheets/sheet1.xml in not\_working.xlsx. (see full stacktrace below)

I suspect the files might be corrupted or not properly formatted. Here is a snippet of my code:

```julia
using XLSX

# Attempt to open the original file
f1 = XLSX.openxlsx("not_working.xlsx")

# Open and save the file in Excel, then try to open again
f2 = XLSX.openxlsx("working.xlsx") # This file was opened and saved again in Excel

```

I’ve uploaded the problematic Excel files here: [https://we.tl/t-agL1yFP38l](https://we.tl/t-agL1yFP38l)

Has anyone experienced similar issues or have any suggestions on how to handle these files without manually opening and saving them in Excel? Any insights or alternative approaches would be greatly appreciated.

Thank you!

> Blockquote  
> ERROR: AssertionError: Couldn’t find xl/worksheets/sheet1.xml in not\_working.xlsx.  
> Stacktrace:  
> [1] open\_internal\_file\_stream  
> @ C:\Users\phil25.julia\packages\XLSX\U2Bcm\src\stream.jl:46 [inlined]  
> [2] read\_worksheet\_dimension(xf::XLSX.XLSXFile, relationship\_id::String, name::String)  
> @ XLSX C:\Users\phil25.julia\packages\XLSX\U2Bcm\src\worksheet.jl:32  
> [3] XLSX.Worksheet(xf::XLSX.XLSXFile, sheet\_element::EzXML.Node)  
> @ XLSX C:\Users\phil25.julia\packages\XLSX\U2Bcm\src\worksheet.jl:8  
> [4] parse\_workbook!(xf::XLSX.XLSXFile)  
> @ XLSX C:\Users\phil25.julia\packages\XLSX\U2Bcm\src\read.jl:353  
> [5] open\_or\_read\_xlsx(source::String, read\_files::Bool, enable\_cache::Bool, read\_as\_template::Bool)  
> @ XLSX C:\Users\phil25.julia\packages\XLSX\U2Bcm\src\read.jl:236  
> [6] openxlsx(source::String; mode::String, enable\_cache::Bool)  
> @ XLSX C:\Users\phil25.julia\packages\XLSX\U2Bcm\src\read.jl:172  
> [7] openxlsx(source::String)  
> @ XLSX C:\Users\phil25.julia\packages\XLSX\U2Bcm\src\read.jl:164  
> [8] top-level scope  
> @ c:\Users\phil25\Gits\Research\dsa\julia\LoadResults.jl:44

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [May 26, 2024, 4:05am UTC](https://discourse.julialang.org/t/issues-importing-excel-files-with-xlsx-jl-package/114717/2 "2024-05-26T04:05:59Z")

</div>

Hello,

I think the issue is somehow “xl/worksheets/sheet1.xml” got renamed to “xl/worksheets/Sheet1.xml” with a capital S

```julia-repl
julia> using ZipArchives

julia> r = ZipReader(read("not_working.xlsx"))
6645 byte, 13 entry ZipReader{Vector{UInt8}}
total uncompressed size: 20787 bytes
  "[Content_Types].xml"
  "_rels/"
  "docProps/"
  "xl/"

julia> zip_names(r)
13-element Vector{String}:
 "[Content_Types].xml"
 "docProps/"
 "docProps/app.xml"
 "docProps/core.xml"
 "xl/"
 "xl/sharedStrings.xml"
 "xl/workbook.xml"
 "xl/worksheets/"
 "xl/worksheets/Sheet1.xml"
 "xl/_rels/"
 "xl/_rels/workbook.xml.rels"
 "_rels/"
 "_rels/.rels"

```

The following function seems to fix the issue, but I’m not sure if this is a robust solution.

```julia-repl
julia> function fix_broken_xlsx(broken_filename, new_filename)
           r = ZipReader(read(broken_filename))
           ZipWriter(new_filename) do w
               for name in zip_names(r)
                   zip_isdir(r, name) && continue
                   content = zip_readentry(r, name)
                   if name == "xl/worksheets/Sheet1.xml"
                       name = "xl/worksheets/sheet1.xml"
                   end
                   zip_writefile(w, name, content)
               end
           end
       end
fix_broken_xlsx (generic function with 1 method)

julia> fix_broken_xlsx("not_working.xlsx", "maybe_working.xlsx");

julia> f1 = XLSX.openxlsx("maybe_working.xlsx")
XLSXFile("maybe_working.xlsx") containing 1 Worksheet
            sheetname size range
-------------------------------------------------
                   de 4x103 A1:CY4
```

---

<div class="post-metadata">

**Author:** ![phil25](https://avatars.discourse-cdn.com/v4/letter/p/7cd45c/32.png) [@phil25](https://discourse.julialang.org/u/phil25)\
**Post date:** [May 27, 2024, 12:09pm UTC](https://discourse.julialang.org/t/issues-importing-excel-files-with-xlsx-jl-package/114717/3 "2024-05-27T12:09:29Z")

</div>

Thanks a lot, Nathan. I appreciate your solution. I did not manage to use ZipArchive but followed your approach like this

```julia
using ZipFile

function fix_broken_xlsx(broken_filename, new_filename)
    r = ZipFile.Reader(broken_filename)
    w = ZipFile.Writer(new_filename)
    
    for f in r.files
        # Read the content of the file
        content = read(f)
        
        # Check and rename if necessary
        if f.name == "xl/worksheets/Sheet1.xml"
            wf = ZipFile.addfile(w, "xl/worksheets/sheet1.xml")
            write(wf, content)
        else
            wf = ZipFile.addfile(w, f.name)
            write(wf, content)
        end
    end
    
    # Close the readers and writers
    close(r)
    close(w)
end
''
```
