'How to Download a System.IO.Stream Object
I am using ClosedXML and OpenXML to create a an Excel spreadsheet. This is part of a .Net Core project where a user will click a button to download a file. I was able to save the file to my hard drive directly from ClosedXML by using the following syntax:
wb.SaveAs("D:\\Our_Working_Folder\\TestNewData.xlsx");
I needed to add in gradient fills, and followed what was in the following link: How to use a gradient fill (GradientFill) with ClosedXML
I go thru the following code, but it does not download a file.
System.IO.Stream spreadsheetStream = new System.IO.MemoryStream();
wb.SaveAs(spreadsheetStream);
SpreadsheetDocument package = SpreadsheetDocument.Open(spreadsheetStream, true);
WorkbookPart wbPart = package.GetPartsOfType<WorkbookPart>().FirstOrDefault();
WorkbookStylesPart wbStylePart = wbPart.GetPartsOfType<WorkbookStylesPart>().FirstOrDefault();
Stylesheet stylesheet = wbStylePart.Stylesheet; // all three are not null - check if you want
//Yellow to Red
OpenXmlElement oldFill = stylesheet.Fills.FirstOrDefault(f => f.OuterXml.Contains("C034EB")); // find the fill that uses your unique color
if (oldFill != null) // maybe you generate the .xlsx and the "gradient fill" is not always present
{
GradientFill gradientFill = new GradientFill() { Degree = 0 };
gradientFill.Append(new GradientStop() { Position = 0D, Color = new Color() { Rgb = "F2E95DFF" } });
gradientFill.Append(new GradientStop() { Position = 1D, Color = new Color() { Rgb = "F25D64FF" } });
oldFill.ReplaceChild(gradientFill, oldFill.FirstChild); // inside the fill replace the patternFill with your gradientFill
}
//yellow to green
oldFill = stylesheet.Fills.FirstOrDefault(f => f.OuterXml.Contains("EB34D3")); // find the fill that uses your unique color
if (oldFill != null) // maybe you generate the .xlsx and the "gradient fill" is not always present
{
GradientFill gradientFill = new GradientFill() { Degree = 0 };
gradientFill.Append(new GradientStop() { Position = 0D, Color = new Color() { Rgb = "F2E95DFF" } });
gradientFill.Append(new GradientStop() { Position = 1D, Color = new Color() { Rgb = "86F25DFF" } });
oldFill.ReplaceChild(gradientFill, oldFill.FirstChild); // inside the fill replace the patternFill with your gradientFill
}
//green to yellow
oldFill = stylesheet.Fills.FirstOrDefault(f => f.OuterXml.Contains("5E34EB")); // find the fill that uses your unique color
if (oldFill != null) // maybe you generate the .xlsx and the "gradient fill" is not always present
{
GradientFill gradientFill = new GradientFill() { Degree = 0 };
gradientFill.Append(new GradientStop() { Position = 0D, Color = new Color() { Rgb = "86F25DFF" } });
gradientFill.Append(new GradientStop() { Position = 1D, Color = new Color() { Rgb = "F2E95DFF" } });
oldFill.ReplaceChild(gradientFill, oldFill.FirstChild); // inside the fill replace the patternFill with your gradientFill
}
//green to red
oldFill = stylesheet.Fills.FirstOrDefault(f => f.OuterXml.Contains("3446EB")); // find the fill that uses your unique color
if (oldFill != null) // maybe you generate the .xlsx and the "gradient fill" is not always present
{
GradientFill gradientFill = new GradientFill() { Degree = 0 };
gradientFill.Append(new GradientStop() { Position = 0D, Color = new Color() { Rgb = "86F25DFF" } });
gradientFill.Append(new GradientStop() { Position = 1D, Color = new Color() { Rgb = "F25D64FF" } });
oldFill.ReplaceChild(gradientFill, oldFill.FirstChild); // inside the fill replace the patternFill with your gradientFill
}
//red to green
oldFill = stylesheet.Fills.FirstOrDefault(f => f.OuterXml.Contains("1C877C")); // find the fill that uses your unique color
if (oldFill != null) // maybe you generate the .xlsx and the "gradient fill" is not always present
{
GradientFill gradientFill = new GradientFill() { Degree = 0 };
gradientFill.Append(new GradientStop() { Position = 0D, Color = new Color() { Rgb = "F25D64FF" } });
gradientFill.Append(new GradientStop() { Position = 1D, Color = new Color() { Rgb = "86F25DFF" } });
oldFill.ReplaceChild(gradientFill, oldFill.FirstChild); // inside the fill replace the patternFill with your gradientFill
}
//red to yellow
oldFill = stylesheet.Fills.FirstOrDefault(f => f.OuterXml.Contains("34EBE2")); // find the fill that uses your unique color
if (oldFill != null) // maybe you generate the .xlsx and the "gradient fill" is not always present
{
GradientFill gradientFill = new GradientFill() { Degree = 0 };
gradientFill.Append(new GradientStop() { Position = 0D, Color = new Color() { Rgb = "F25D64FF" } });
gradientFill.Append(new GradientStop() { Position = 1D, Color = new Color() { Rgb = "F2E95DFF" } });
oldFill.ReplaceChild(gradientFill, oldFill.FirstChild); // inside the fill replace the patternFill with your gradientFill
}
package.Close();
spreadsheetStream.Position = 0;
return new FileStreamResult(spreadsheetStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { FileDownloadName = "TestNewData.xlsx" };
I start off by saving the workbook created by ClosedXML as a Stream. I then manipulate the stream through OpenXML. When it passes the FileStreamResult, nothing is downloaded, and no errors are generated.
How can I download this file?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
