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 static void addAutofitTestRows( ExcelFileWriter writer, string fitOrNot )
 {
     writer.DefaultWorksheet.AddHeaderToWorksheet( "This is the default worksheet - hopefully " + fitOrNot );
     writer.DefaultWorksheet.AddRowToWorksheet( "500", "5000", "5", "50", "500", "5000", "5", "5", "5", "5", "50" );
     writer.DefaultWorksheet.AddRowToWorksheet( "500", "5000", "5", "50", "500", "5000", "5", "5", "5", "5", "50" );
     writer.DefaultWorksheet.AddRowToWorksheet( "500", "5000", "5", "50", "500", "5000", "5", "5", "5", "5", "50" );
     writer.DefaultWorksheet.AddRowToWorksheet(
         "500",
         "5000",
         "5",
         "50",
         "500",
         "5000",
         "5",
         "Longer than expected cell value here oh yeah it is.  Super long in fact." );
 }
Exemplo n.º 3
0
        internal static ExcelFileWriter CreateExcelFileWriter( MergeRowTree rowTree, IEnumerable<string> fieldNames, bool useMsWordFieldNames )
        {
            var excelFile = new ExcelFileWriter();
            if( rowTree.Rows.Any() ) {
                foreach( var fieldName in fieldNames ) {
                    if( rowTree.Rows.First().Values.All( i => i.Name != fieldName ) ) {
                        // Use ApplicationException instead of MailMergingException because the field names can easily be validated before this method is called.
                        throw new ApplicationException( "Merge field " + fieldName + " is invalid." );
                    }
                }

                var sheet = excelFile.DefaultWorksheet;
                sheet.AddHeaderToWorksheet(
                    fieldNames.Select( fieldName => rowTree.Rows.First().Values.Single( i => i.Name == fieldName ) )
                        .Select( mergeValue => useMsWordFieldNames ? mergeValue.MsWordName : mergeValue.Name.CamelToEnglish() )
                        .ToArray() );
                sheet.FreezeHeaderRow();
                foreach( var row in rowTree.Rows ) {
                    sheet.AddRowToWorksheet(
                        fieldNames.Select( fieldName => row.Values.Single( i => i.Name == fieldName ) ).Select(
                            mergeValue => {
                                var mergeValueAsString = mergeValue as MergeValue<string>;
                                string value = null;
                                if( mergeValueAsString != null )
                                    value = mergeValueAsString.Evaluate( false );
                                if( value == null ) {
                                    // Use ApplicationException instead of MailMergingException because the field names can easily be validated before this method is called.
                                    throw new ApplicationException( "Merge field " + mergeValue.Name + " evaluates to an unsupported type." );
                                }

                                return value;
                            } ).ToArray() );
                }
            }
            return excelFile;
        }