NPOI编写XLS文件转换为Azure Blob

时间:2014-06-13 12:50:37

标签: excel azure azure-storage-blobs npoi

我正在尝试将使用NPOI的当前应用程序转换为在服务器上创建xls文档到Azure托管应用程序。我对NPOI和Azure几乎没有经验,因此在那里进行了2次攻击。我有应用程序上传xls到Blob容器,但它总是空白(9字节)。根据我的理解NPOI使用文件流写入文件,所以我只是改变它写入blob容器。

以下是我认为的相关部分:

   internal void GenerateExcel(DataSet ds, int QuoteID, string ReportFileName)
{
    string ExcelFileName = string.Format("{0}_{1}.xls",ReportFileName,QuoteID);       

    try
    {


        //these 2 strings will get deleted but left here for now to run side by side at the moment

        string ReportDirectoryPath = HttpContext.Current.Server.MapPath(".") + "\\Reports";
        if (!Directory.Exists(ReportDirectoryPath))
        {
            Directory.CreateDirectory(ReportDirectoryPath);
        }

        string ExcelReportFullPath = ReportDirectoryPath + "\\" + ExcelFileName;

        if (File.Exists(ExcelReportFullPath))
        {
            File.Delete(ExcelReportFullPath);
        }

        // Create a new workbook.
        var workbook = new HSSFWorkbook();

        //Rest of the NPOI XLS rows cells etc. etc. all works fine when writing to disk////////////////

        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to a container. 
        CloudBlobContainer container = blobClient.GetContainerReference("pricingappreports");
        // Create the container if it doesn't already exist.
        if (container.CreateIfNotExists())
        {
            container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        }
        // Retrieve reference to a blob with the same name.
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(ExcelFileName);



        // Write the output to a file on the server
        String file = ExcelReportFullPath;
        using (FileStream fs = new FileStream(file, FileMode.Create))
        {
            workbook.Write(fs);
            fs.Close();
        }
        // Write the output to a file on Azure Storage
        String Blobfile = ExcelFileName;
        using (FileStream fs = new FileStream(Blobfile, FileMode.Create))
        {
            workbook.Write(fs);
            blockBlob.UploadFromStream(fs);
            fs.Close();
        }                
    }

我上传到Blob并且文件存在,为什么数据不会写入xls? 任何帮助,将不胜感激。

更新:我想我发现了问题。看起来你不能写入Blob存储中的文件。发现这个博客几乎回答了我的问题:它不使用NPOI,但概念是相同的。 http://debugmode.net/2011/08/28/creating-and-updating-excel-file-in-windows-azure-web-role-using-open-xml-sdk/ 感谢

1 个答案:

答案 0 :(得分:0)

您可以安装fiddler并检查请求和响应数据包吗?您可能还需要在两次写入之间寻找0。所以这里正确的代码可能是在尝试将流写入blob之前添加以下内容。

workbook.Write(fs);
fs.Seek(0, SeekOrigin.Begin);
blockBlob.UploadFromStream(fs);
fs.Close();

我还注意到您使用String Blobfile = ExcelFileName而不是String Blobfile = ExcelReportFullPath。