将多个Excel文件作为一个文件流

时间:2016-10-07 13:06:20

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

我想使用webservice或httphandler提供大型Excel文件。

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

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

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

这可能吗?

包含标题的主excel文件: enter image description here

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

这确实会回归废话:

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

1 个答案:

答案 0 :(得分:2)

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

  1. 请求在AJAX请求中下载文件名。
  2. 对于收到的每个文件名,请准备其到服务器的路径。
  3. 为每个文件路径创建隐藏的iFrame,并为每个要下载的文件指定src作为文件路径。
  4. 当设置了iFrame的src属性时,它将导航到文件路径,每个iFrame将下载单个文件,因此多个iFrame会下载多个文件。

    您无法在单个请求中下载多个文件。好像你将附加多个文件的流,它将创建一个垃圾文件,一个垃圾文件。