public Stream GetZipStream() { MemoryStream ms = new MemoryStream(); ZipOutputStream os = new ZipOutputStream(ms); return os; }
internal void WriteZip(ZipOutputStream os) { byte[] b; if (SaveHandler == null) { b = GetStream().ToArray(); if (b.Length == 0) //Make sure the file isn't empty. DotNetZip streams does not seems to handle zero sized files. { return; } os.CompressionLevel = (OfficeOpenXml.Packaging.Ionic.Zlib.CompressionLevel)CompressionLevel; os.PutNextEntry(Uri.OriginalString); os.Write(b, 0, b.Length); } else { SaveHandler(os, (CompressionLevel)CompressionLevel, Uri.OriginalString); } if (_rels.Count > 0) { string f = Uri.OriginalString; var name = Path.GetFileName(f); _rels.WriteZip(os, (string.Format("{0}_rels/{1}.rels", f.Substring(0, f.Length - name.Length), name))); } b = null; }
private void SaveSharedStringHandler(ZipOutputStream stream, CompressionLevel compressionLevel, string fileName) { //Packaging.ZipPackagePart stringPart; //if (_package.Package.PartExists(SharedStringsUri)) //{ // stringPart=_package.Package.GetPart(SharedStringsUri); //} //else //{ // stringPart = _package.Package.CreatePart(SharedStringsUri, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml", _package.Compression); //Part.CreateRelationship(UriHelper.GetRelativeUri(WorkbookUri, SharedStringsUri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/sharedStrings"); //} //StreamWriter sw = new StreamWriter(stringPart.GetStream(FileMode.Create, FileAccess.Write)); //Init Zip stream.CompressionLevel = (OfficeOpenXml.Packaging.Ionic.Zlib.CompressionLevel)compressionLevel; stream.PutNextEntry(fileName); var cache = new StringBuilder(); var sw = new StreamWriter(stream); cache.AppendFormat("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"{0}\" uniqueCount=\"{0}\">", _sharedStrings.Count); foreach (string t in _sharedStrings.Keys) { SharedStringItem ssi = _sharedStrings[t]; if (ssi.isRichText) { cache.Append("<si>"); ConvertUtil.ExcelEncodeString(cache, t); cache.Append("</si>"); } else { if (t.Length>0 && (t[0] == ' ' || t[t.Length-1] == ' ' || t.Contains(" ") || t.Contains("\t"))) { cache.Append("<si><t xml:space=\"preserve\">"); } else { cache.Append("<si><t>"); } ConvertUtil.ExcelEncodeString(cache, ConvertUtil.ExcelEscapeString(t)); cache.Append("</t></si>"); } if (cache.Length > 0x600000) { sw.Write(cache.ToString()); cache = new StringBuilder(); } } cache.Append("</sst>"); sw.Write(cache.ToString()); sw.Flush(); Part.CreateRelationship(UriHelper.GetRelativeUri(WorkbookUri, SharedStringsUri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/sharedStrings"); }