Esempio n. 1
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()));
    }
Esempio n. 2
0
    public static async ValueTask WriteAsync(
        ZipArchive archive,
        CompressionLevel compressionLevel,
        SpreadsheetBuffer buffer,
        List <string> worksheetPaths,
        bool hasStylesXml,
        CancellationToken token)
    {
        var stream = archive.CreateEntry("xl/_rels/workbook.xml.rels", 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 < worksheetPaths.Count; ++i)
            {
                var path               = worksheetPaths[i];
                var sheetId            = i + 1;
                var sheetElementLength = GetSheetElementByteCount(path, sheetId);

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

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

            var bufferNeeded = Footer.Length + (hasStylesXml ? MaxStylesXmlElementByteCount : 0);
            if (bufferNeeded > buffer.FreeCapacity)
            {
                await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
            }

            if (hasStylesXml)
            {
                buffer.Advance(GetStylesXmlElementBytes(worksheetPaths.Count + 1, buffer.GetSpan()));
            }

            buffer.Advance(Utf8Helper.GetBytes(Footer, buffer.GetSpan()));
            await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
        }
    }
Esempio n. 3
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);
    }
Esempio n. 4
0
    private static bool GetBytes(StyleId styleId, SpreadsheetBuffer buffer)
    {
        var bytes        = buffer.GetSpan();
        var bytesWritten = SpanHelper.GetBytes(StyledCellHelper.BeginStyledNumberCell, bytes);

        bytesWritten += Utf8Helper.GetBytes(styleId.Id, bytes.Slice(bytesWritten));
        bytesWritten += SpanHelper.GetBytes(StyledCellHelper.EndStyleNullValue, bytes.Slice(bytesWritten));
        buffer.Advance(bytesWritten);
        return(true);
    }
Esempio n. 5
0
    private bool GetBytes(StyleId styleId, SpreadsheetBuffer buffer)
    {
        var bytes        = buffer.GetSpan();
        var bytesWritten = SpanHelper.GetBytes(StyledCellHelper.BeginStyledBooleanCell, bytes);

        bytesWritten += Utf8Helper.GetBytes(styleId.Id, bytes.Slice(bytesWritten));
        bytesWritten += SpanHelper.GetBytes(EndStyleValueBytes(), bytes.Slice(bytesWritten));
        buffer.Advance(bytesWritten);
        return(true);
    }
Esempio n. 6
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);
        }
    }
Esempio n. 7
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);
        }
    }
    public static async ValueTask WriteAsync(
        ZipArchive archive,
        CompressionLevel compressionLevel,
        SpreadsheetBuffer buffer,
        CancellationToken token)
    {
        var stream = archive.CreateEntry("_rels/.rels", compressionLevel).Open();

#if NETSTANDARD2_0
        using (stream)
#else
        await using (stream.ConfigureAwait(false))
#endif
        {
            buffer.Advance(Utf8Helper.GetBytes(Content, buffer.GetSpan()));
            await buffer.FlushToStreamAsync(stream, token).ConfigureAwait(false);
        }
    }
Esempio n. 9
0
 private static bool GetBytes(SpreadsheetBuffer buffer)
 {
     buffer.Advance(SpanHelper.GetBytes(DataCellHelper.NullCell, buffer.GetSpan()));
     return(true);
 }
Esempio n. 10
0
 private bool GetBytes(SpreadsheetBuffer buffer)
 {
     buffer.Advance(SpanHelper.GetBytes(DataCellBytes(), buffer.GetSpan()));
     return(true);
 }