0

I'm creating an excel file (xlsx) based on Office Open XML. This is how my code looks like.

        string basePath = Util.GetAppDirectoryPath ();
        string excelDirectory = Path.Combine (basePath, "temp", "xlsx");
        System.IO.Directory.CreateDirectory (excelDirectory);

        StreamWriter swContentType;
        using (swContentType = System.IO.File.CreateText (Path.Combine (excelDirectory, "[Content_Types].xml"))) {
            swContentType.Write (CONTENT_TYPES);
            swContentType.Flush ();
        }

        string docPropsDirectory = Path.Combine (excelDirectory, "docProps");
        System.IO.Directory.CreateDirectory (docPropsDirectory);

        StreamWriter swApp;
        using (swApp = System.IO.File.CreateText (Path.Combine (docPropsDirectory, "app.xml"))) {
            swApp.Write (APPXML);
            swApp.Flush ();
        }

        StreamWriter swCore;
        using (swCore = System.IO.File.CreateText (Path.Combine (docPropsDirectory, "core.xml"))) {
            swCore.Write (COREXML);
            swCore.Flush ();
        }

        string xlDirectory = Path.Combine (excelDirectory, "xl");
        System.IO.Directory.CreateDirectory (xlDirectory);

        FileStream fsSharedStrings;
        using (fsSharedStrings = System.IO.File.Create (Path.Combine (xlDirectory, "sharedStrings.xml"))) {
            WriteStringCache (fsSharedStrings);
        }

        StreamWriter swStyles;
        using (swStyles = System.IO.File.CreateText (Path.Combine (xlDirectory, "styles.xml"))) {
            WriteStyles (swStyles);
        }

        StreamWriter swWorkbook;
        using (swWorkbook = System.IO.File.CreateText (Path.Combine (xlDirectory, "workbook.xml"))) {
            WriteWorkbook (swWorkbook);
        }

        string xlThemeDirectory = Path.Combine (xlDirectory, "theme");
        System.IO.Directory.CreateDirectory (xlThemeDirectory);

        StreamWriter swTheme;
        using (swTheme = System.IO.File.CreateText (Path.Combine (xlThemeDirectory, "theme1.xml"))) {
            WriteTheme (swTheme);
        }

        string xlWorksheetsDirectory = Path.Combine (xlDirectory, "worksheets");
        System.IO.Directory.CreateDirectory (xlWorksheetsDirectory);

        FileStream fsWorksheets;
        if (_Sheets.Count == 0) {   // output an empty work sheet
            using (fsWorksheets = System.IO.File.Create (Path.Combine (xlWorksheetsDirectory, "sheet1.xml"))) {
                WriteEmptyWorksheet (fsWorksheets);
            }
        } else {// output the spreadsheets
            foreach (SheetInfo sinfo in _Sheets) {
                string sname = string.Format ("{0}.xml", sinfo.Name);
                using (fsWorksheets = System.IO.File.Create (Path.Combine (xlWorksheetsDirectory, sname))) {
                    WriteData (fsWorksheets, sinfo.Grid, sinfo.MergeCells); // here's where the meat of the data goes
                }

            }
        }

        string rels = Path.Combine (xlDirectory, "_rels");
        System.IO.Directory.CreateDirectory (rels);

        StreamWriter swWorkbookRels;
        using (swWorkbookRels = System.IO.File.CreateText (Path.Combine (rels, "workbook.xml.rels"))) {
            WriteWorkbookRels (swWorkbookRels);
        }

        string relsDirectory = Path.Combine (excelDirectory, "_rels");
        System.IO.Directory.CreateDirectory (relsDirectory);

        StreamWriter swRels;
        using (swRels = System.IO.File.CreateText (Path.Combine (relsDirectory, ".rels"))) {
            swRels.Write (RELS_RELS);
            swRels.Flush ();
        }

        ZipFile.CreateFromDirectory(excelDirectory, Path.Combine(basePath, "temp", "temp.xlsx"), CompressionLevel.Optimal, false);

I'm creating all the directories and write all the metadata that is neccesary. The Problem is that if I use ZipFile from System.IO.Compression to zip the created directory (excelDirectory) the file would be corrupted, but if I compress the directory using Arhive Manager or 7Zip the file would be good. What can be the problem ?

I'm using Linux Mint. Monodevelop. Mono.

  • What means "corrupted"? if you rename it to .zip can you open the file? and if yes, the structure created is exactly the same as the one created with 7zip? – Gusman Apr 04 '16 at 09:44
  • Corrupted means that LibreOffice or Microsoft Office can't open it. Yes, if I rename it to .zip I can extract it and the structure is the same as it was created. And if I compress again all the folders from extracted zip the file would be good. – George Cristian Apr 04 '16 at 09:48
  • If you rename it to zip and you can extract it, then it is not corrupted. What happens is that you're not following the spec that LO/MO may need. – knocte Apr 13 '16 at 08:17
  • Test it with MS.NET on Windows (i.e. Visual Studio Community), and if it works there, then it must be a Mono bug (in this case, upgrade your Mono version) – knocte Apr 13 '16 at 08:18

0 Answers0