What we were trying to do
We needed to let users download ATM transaction data as Excel and CSV files. The catch is that this is sensitive financial data, so everything has to be PCI compliant, meaning secure. This is how we went from the old way to a modern one in about 1.5 hours.
The old way and the new way
The old VB-style approach went like this:
' This is how it used to be done:
1. Make a temporary file on the server
2. Save the data to that file
3. Read the file back
4. Send it to the user
5. Try to delete the temporary file
6. Hope nothing went wrong!
The temporary file is the problem. It can contain sensitive data, it might not get deleted properly, and so there are security risks at every step, plus a lot of cleanup code to write and trust.
Our new C# way:
// Modern approach - everything stays in memory!
using var package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("ATM Data");
worksheet.Cells["A1"].Value = "Report ID";
// Add more data...
// Send directly to user - no files saved anywhere!
return File(package.GetAsByteArray(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"atm_data.xlsx");
No temporary file is created, the data never touches the disk, it cleans up after itself, and it’s much more secure.
How it works, in plain English
- We create a virtual Excel file in the computer’s memory (RAM)
- We add our data to this virtual file
- We stream it directly to the user’s browser
- Everything disappears from memory when it’s done
It’s like writing on a magic whiteboard that erases itself after showing the content to the user.
The important parts
First, setting up the structure:
// This tells other developers what our code will do
public interface IDataAccess
{
// Get the data we want to export
Task<IEnumerable<ExportRow>> GetExportData(int fiId, ExportParameters parameters);
// Keep track of who downloaded what
Task LogExport(ExportLog log);
}
Then the special cases:
// Make sure numbers like '0123' don't lose their leading zeros
worksheet.Cells["B:B"].Style.Numberformat.Format = "@";
Why this is PCI compliant
Nothing is ever saved to disk, and the data only sits in memory for as long as it’s needed. The download goes over HTTPS, we track who downloads what, and the cleanup happens automatically, so nobody has to remember to do it.
Future plans
Right now we’re using EPPlus, which needs a commercial license. We could switch to ClosedXML (free), NPOI (free), or our own CSV generator.
What we learned
- Modern C# makes security easier
- Less code often means more secure code
- Memory-only operations are faster and safer
- You don’t need temporary files anymore
Next we’re planning to add a Windows Service that watches for new files and processes them automatically, but that’s a story for another day.
Whether you come from VB or you’re learning C#, the modern approach can make your code both simpler and more secure, and sometimes the easiest solution really is the best one.
Have a .NET modernization project?
Free 30-minute call. No pitch, we just look at your migration together.
Get in touch