0

我正在创建一个基于 Office Open XML 的 excel 文件 (xlsx)。这就是我的代码的样子。

        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);

我正在创建所有目录并编写所有必要的元数据。问题是,如果我使用 System.IO.Compression 中的 ZipFile 来压缩创建的目录 (excelDirectory),则该文件将被损坏,但如果我使用 Arhive Manager 或 7Zip 压缩该目录,则该文件会很好。可能是什么问题?

我正在使用 Linux Mint。单开发。单核细胞增多症。

4

0 回答 0