Exemplo n.º 1
0
    private static async ValueTask WriteAsync(
        Stream stream,
        SpreadsheetBuffer buffer,
        List <Style> styles,
        CancellationToken token)
    {
        buffer.Advance(Utf8Helper.GetBytes(Header, buffer.GetSpan()));

        var fontLookup = await WriteFontsAsync(stream, buffer, styles, token).ConfigureAwait(false);

        var fillLookup = await WriteFillsAsync(stream, buffer, styles, token).ConfigureAwait(false);

        await buffer.WriteAsciiStringAsync(XmlPart1, stream, token).ConfigureAwait(false);

        var styleCount = styles.Count + 1;

        if (styleCount.GetNumberOfDigits() > buffer.FreeCapacity)
        {
            await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
        }

        var sb = new StringBuilder();

        sb.Append(styleCount);

        // The default style must be the first one (index 0)
        sb.Append("\"><xf numFmtId=\"0\" fontId=\"0\" fillId=\"0\"/>");
        await buffer.WriteAsciiStringAsync(sb.ToString(), stream, token).ConfigureAwait(false);

        sb.Clear();

        for (var i = 0; i < styles.Count; ++i)
        {
            var style     = styles[i];
            var fontIndex = fontLookup[style.Font];
            var fillIndex = fillLookup[style.Fill];

            sb.Clear();
            sb.Append("<xf numFmtId=\"0\"");
            sb.Append(" fontId=\"").Append(fontIndex).Append('"');
            if (fontIndex > 0)
            {
                sb.Append(" applyFont=\"1\"");
            }

            sb.Append(" fillId=\"").Append(fillIndex).Append('"');
            if (fillIndex > 1)
            {
                sb.Append(" applyFill=\"1\"");
            }

            sb.Append(" xfId=\"0\"/>");
            await buffer.WriteAsciiStringAsync(sb.ToString(), stream, token).ConfigureAwait(false);
        }

        await buffer.WriteAsciiStringAsync(XmlPart2, stream, token).ConfigureAwait(false);

        await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
    }
Exemplo n.º 2
0
    private static async ValueTask <Dictionary <Fill, int> > WriteFillsAsync(
        Stream stream,
        SpreadsheetBuffer buffer,
        List <Style> styles,
        CancellationToken token)
    {
        var       defaultFill  = new Fill();
        const int defaultCount = 2;

        var uniqueFills = new Dictionary <Fill, int> {
            { defaultFill, 0 }
        };

        for (var i = 0; i < styles.Count; ++i)
        {
            var fill = styles[i].Fill;
            uniqueFills[fill] = 0;
        }

        var sb         = new StringBuilder();
        var totalCount = uniqueFills.Count + defaultCount - 1;

        sb.Append("<fills count=\"").Append(totalCount).Append("\">");

        // The 2 default fills must come first
        sb.Append("<fill><patternFill patternType=\"none\"/></fill>");
        sb.Append("<fill><patternFill patternType=\"gray125\"/></fill>");
        await buffer.WriteAsciiStringAsync(sb.ToString(), stream, token).ConfigureAwait(false);

        var fillIndex = defaultCount;

        foreach (var fill in uniqueFills.Keys.ToArray())
        {
            if (fill.Equals(defaultFill))
            {
                continue;
            }

            sb.Clear();
            sb.AppendFill(fill);
            await buffer.WriteAsciiStringAsync(sb.ToString(), stream, token).ConfigureAwait(false);

            uniqueFills[fill] = fillIndex;
            ++fillIndex;
        }

        await buffer.WriteAsciiStringAsync("</fills>", stream, token).ConfigureAwait(false);

        return(uniqueFills);
    }
Exemplo n.º 3
0
    private static async ValueTask <Dictionary <Font, int> > WriteFontsAsync(
        Stream stream,
        SpreadsheetBuffer buffer,
        List <Style> styles,
        CancellationToken token)
    {
        var       defaultFont  = new Font();
        const int defaultCount = 1;

        var uniqueFonts = new Dictionary <Font, int> {
            { defaultFont, 0 }
        };

        for (var i = 0; i < styles.Count; ++i)
        {
            var font = styles[i].Font;
            uniqueFonts[font] = 0;
        }

        var sb         = new StringBuilder();
        var totalCount = uniqueFonts.Count + defaultCount - 1;

        sb.Append("<fonts count=\"").Append(totalCount).Append("\">");

        // The default font must be the first one (index 0)
        sb.Append("<font><sz val=\"11\"/><name val=\"Calibri\"/></font>");
        await buffer.WriteAsciiStringAsync(sb.ToString(), stream, token).ConfigureAwait(false);

        var fontIndex = defaultCount;

        foreach (var font in uniqueFonts.Keys.ToArray())
        {
            if (font.Equals(defaultFont))
            {
                continue;
            }

            sb.Clear();
            sb.AppendFont(font);
            await buffer.WriteAsciiStringAsync(sb.ToString(), stream, token).ConfigureAwait(false);

            uniqueFonts[font] = fontIndex;
            ++fontIndex;
        }

        await buffer.WriteAsciiStringAsync("</fonts>", stream, token).ConfigureAwait(false);

        return(uniqueFonts);
    }
Exemplo n.º 4
0
    public async ValueTask WriteHeadAsync(WorksheetOptions?options, CancellationToken token)
    {
        _buffer.Advance(Utf8Helper.GetBytes(SheetHeader, _buffer.GetSpan()));
        if (options is null)
        {
            _buffer.Advance(Utf8Helper.GetBytes(SheetDataBegin, _buffer.GetSpan()));
            return;
        }

        var sb = new StringBuilder();

        if (options.FrozenColumns is not null || options.FrozenRows is not null)
        {
            await _buffer.FlushToStreamAsync(_stream, token).ConfigureAwait(false);

            WriteSheetViewsXml(sb, options);
            await _buffer.WriteAsciiStringAsync(sb.ToString(), _stream, token).ConfigureAwait(false);

            sb.Clear();
        }

        await WriteColsXmlAsync(sb, options, token).ConfigureAwait(false);

        _buffer.Advance(Utf8Helper.GetBytes(SheetDataBegin, _buffer.GetSpan()));
    }
Exemplo n.º 5
0
    public static async ValueTask WriteAsync(
        ZipArchive archive,
        CompressionLevel compressionLevel,
        SpreadsheetBuffer buffer,
        List <string> worksheetPaths,
        bool hasStylesXml,
        CancellationToken token)
    {
        var stream = archive.CreateEntry("[Content_Types].xml", compressionLevel).Open();

#if NETSTANDARD2_0
        using (stream)
#else
        await using (stream.ConfigureAwait(false))
#endif
        {
            buffer.Advance(Utf8Helper.GetBytes(Header, buffer.GetSpan()));

            if (hasStylesXml)
            {
                await buffer.WriteAsciiStringAsync(Styles, stream, token).ConfigureAwait(false);
            }

            for (var i = 0; i < worksheetPaths.Count; ++i)
            {
                var path = worksheetPaths[i];
                var sheetElementLength = GetSheetElementByteCount(path);

                if (sheetElementLength > buffer.FreeCapacity)
                {
                    await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
                }

                buffer.Advance(GetSheetElementBytes(path, buffer.GetSpan()));
            }

            await buffer.WriteAsciiStringAsync(Footer, stream, token).ConfigureAwait(false);

            await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
        }
    }
Exemplo n.º 6
0
    public static async ValueTask WriteAsync(
        ZipArchive archive,
        CompressionLevel compressionLevel,
        SpreadsheetBuffer buffer,
        List <string> worksheetNames,
        CancellationToken token)
    {
        var stream = archive.CreateEntry("xl/workbook.xml", compressionLevel).Open();

#if NETSTANDARD2_0
        using (stream)
#else
        await using (stream.ConfigureAwait(false))
#endif
        {
            buffer.Advance(Utf8Helper.GetBytes(Header, buffer.GetSpan()));

            for (var i = 0; i < worksheetNames.Count; ++i)
            {
                var sheetId            = i + 1;
                var name               = WebUtility.HtmlEncode(worksheetNames[i]);
                var sheetElementLength = GetSheetElementByteCount(name, sheetId);

                if (sheetElementLength > buffer.FreeCapacity)
                {
                    await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
                }

                buffer.Advance(GetSheetElementBytes(name, sheetId, buffer.GetSpan()));
            }

            await buffer.WriteAsciiStringAsync(Footer, stream, token).ConfigureAwait(false);

            await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
        }
    }