【问题标题】:Stream multiple Excel files as one file将多个 Excel 文件作为一个文件流式传输
【发布时间】:2017-02-16 12:00:37
【问题描述】:

我想使用 web 服务或 httphandler 交付大型 Excel 文件。

由于 Excel 文件可能非常大,我想将它们拆分成更小的文件,以减少内存占用。

所以我将有一个包含列标题和数据的主 excel 文件。 以及仅包含数据的其他文件。

在下载过程中,我想先流式传输主 excel 文件,然后将所有其他相关的 excel 文件附加为一个下载流。 我不想压缩它们!最后应该是一个文件

这可能吗?


带有标题的主 excel 文件:

所有其他文件将如下所示(没有标题):

这确实会返回废话:

void Main()
{
    CombineMultipleFilesIntoSingleFile();
}

// Define other methods and classes here

    private static void CombineMultipleFilesIntoSingleFile(string outputFilePath= @"C:\exceltest\main.xlsx", string inputDirectoryPath = @"C:\exceltest", string inputFileNamePattern="*.xlsx")
    {
        string[] inputFilePaths = Directory.GetFiles(inputDirectoryPath, inputFileNamePattern);
        Console.WriteLine("Number of files: {0}.", inputFilePaths.Length);
        using (var outputStream = File.Create(outputFilePath))
        {
            foreach (var inputFilePath in inputFilePaths)
            {
                using (var inputStream = File.OpenRead(inputFilePath))
                {
                    // Buffer size can be passed as the second argument.
                    inputStream.CopyTo(outputStream);
                }
                Console.WriteLine("The file {0} has been processed.", inputFilePath);
            }
        }
    }

【问题讨论】:

  • 您可能需要为它构建一些自定义的东西,但我相信这是可能的。
  • 为什么要投反对票,请解释一下?!

标签: asp.net-mvc web-services filestream httphandler


【解决方案1】:

当您请求文件时,不要在第一次请求时下载它。

  1. 请求在 AJAX 请求中下载文件名。
  2. 对于收到的每个文件名,准备其到服务器的路径。
  3. 为每个文件路径创建隐藏 iFrame,并指定 src 作为每个要下载文件的文件路径。

iFrame的src属性设置后,会导航到文件路径,每个iFrame会下载单个文件,所以多个iFrame会下载多个文件。

您不能在一个请求中下载多个文件。就像你要追加多个文件的流一样,它会创建一个垃圾文件,一个垃圾文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    相关资源
    最近更新 更多