/// <summary> /// Creates an Excel workbook response. Automatically converts the specified file name to a safe file name. /// </summary> public static EwfResponse CreateExcelWorkbookResponse(Func <string> extensionlessFileNameCreator, Func <ExcelFileWriter> workbookCreator) { return(Create( ExcelFileWriter.ContentType, new EwfResponseBodyCreator(stream => workbookCreator().SaveToStream(stream)), fileNameCreator: () => ExcelFileWriter.GetSafeFileName(extensionlessFileNameCreator()))); }
public void TestGetSafeFileName() { /* First assert that the output's what's expected. */ Console.WriteLine("Making sure safe filename works."); var writer = new ExcelFileWriter(); const string fileName = "get safe filename testing :" + "_gibberish_here_*$#*&(#@)*(?|\\/@#_end_gibberish"; var safeFileName = ExcelFileWriter.GetSafeFileName(fileName); Assert.AreEqual(safeFileName, "GetSafeFilenameTesting_Gibberish_here_$#&(#@)(@#_end_gibberish.xlsx"); /* Then actually write it so we're sure the filesystem accepts it too. */ runTest(actualWriter => fileName); }
private void runTest(Func <ExcelFileWriter, string> code, bool includeDefaultWorksheet = true) { var start = DateTime.Now; var writer = includeDefaultWorksheet ? new ExcelFileWriter() : new ExcelFileWriter(false); var fileName = code(writer); var filePath = Path.Combine(outputFolderPath, ExcelFileWriter.GetSafeFileName(timestampPrefix + fileName)); var doneCreating = DateTime.Now; long size; using (var f = File.Create(filePath)) { writer.SaveToStream(f); size = f.Length; } var doneWriting = DateTime.Now; Console.WriteLine("To view this test file, open '{0}' with Excel.".FormatWith(filePath)); Console.WriteLine( "Finished creating {3}kb file in {0}ms, written in another {1}ms, for a total of {2}ms.".FormatWith( (doneCreating - start).TotalMilliseconds, (doneWriting - doneCreating).TotalMilliseconds, (doneWriting - start).TotalMilliseconds, (size / 1000))); }