コード例 #1
0
        public override TFlxPartialFormat Evaluate(ExcelFile workbook, TXlsCellRange rangeToFormat, object[] parameters)
        {
            if (parameters == null || parameters.Length != 1)
            {
                throw new ArgumentException("Bad parameter count in call to ZipCode() user-defined format");
            }

            int color;

            //If the zip code is not valid, don't modify the format.
            if (parameters[0] == null || !int.TryParse(Convert.ToString(parameters[0]), out color))
            {
                return(new TFlxPartialFormat(null, null, false));
            }

            //This code is not supposed to make sense. We will convert the zip code to a color based in the numeric value.
            TFlxFormat fmt = workbook.GetDefaultFormat;

            fmt.FillPattern.Pattern = TFlxPatternStyle.Solid;
            fmt.FillPattern.FgColor = TExcelColor.FromArgb(color);
            fmt.FillPattern.BgColor = TExcelColor.Automatic;

            fmt.Font.Color = TExcelColor.FromArgb(~color);

            TFlxApplyFormat apply = new TFlxApplyFormat();

            apply.FillPattern.SetAllMembers(true);
            apply.Font.Color = true;
            return(new TFlxPartialFormat(fmt, apply, false));
        }
コード例 #2
0
        public override TFlxPartialFormat Evaluate(ExcelFile workbook, TXlsCellRange rangeToFormat, object[] parameters)
        {
            //Again, this example is not supposed to make sense, only to show how you can code a complex rule.
            //This method will format the rows with a color that depends in the length of the first parameter,
            //and if the second parameter starts with "B" it will make the text red.

            if (parameters == null || parameters.Length != 2)
            {
                throw new ArgumentException("Bad parameter count in call to ShipFormat() user-defined format");
            }

            int    len     = Convert.ToString(parameters[0]).Length;
            string country = Convert.ToString(parameters[1]);

            Int32      color = 0xFFFFFF - len * 100;
            TFlxFormat fmt   = workbook.GetDefaultFormat;

            fmt.FillPattern.Pattern = TFlxPatternStyle.Solid;
            fmt.FillPattern.FgColor = TExcelColor.FromArgb(color);
            fmt.FillPattern.BgColor = TExcelColor.Automatic;

            TFlxApplyFormat apply = new TFlxApplyFormat();

            apply.FillPattern.SetAllMembers(true);

            if (country.StartsWith("B"))
            {
                fmt.Font.Color   = Colors.OrangeRed;
                apply.Font.Color = true;
            }

            return(new TFlxPartialFormat(fmt, apply, false));
        }
コード例 #3
0
        /// <summary>Modifies a format.(For example "Font-Name").</summary>
        internal static void ApplyFormatTag(string tag, string fullExpression, TFlxApplyFormat fmt, ref bool exteriorBorders)
        {
            bool add = true;

            if (tag.StartsWith(FormatDelete, StringComparison.InvariantCultureIgnoreCase))
            {
                tag = tag.Remove(0, FormatDelete.Length);
                add = false;
            }

            if (tag.StartsWith(FormatAdd, StringComparison.InvariantCultureIgnoreCase))
            {
                tag = tag.Remove(0, FormatAdd.Length);
            }

            ApplyFormatTagEnum r;

            if (!ApplyFormatTags.TryGetValue(tag, out r))
            {
                FlxMessages.ThrowException(FlxErr.ErrInvalidApplyTag, tag, fullExpression);
            }

            switch (r)
            {
            case ApplyFormatTagEnum.All:
                fmt.SetAllMembers(add);
                break;

            case ApplyFormatTagEnum.Border:
                fmt.Borders.SetAllMembers(add);
                break;

            case ApplyFormatTagEnum.BorderLeft:
                fmt.Borders.Left = add;
                break;

            case ApplyFormatTagEnum.BorderRight:
                fmt.Borders.Right = add;
                break;

            case ApplyFormatTagEnum.BorderTop:
                fmt.Borders.Top = add;
                break;

            case ApplyFormatTagEnum.BorderBottom:
                fmt.Borders.Bottom = add;
                break;

            case ApplyFormatTagEnum.BorderExterior:
                exteriorBorders = add;
                break;

            case ApplyFormatTagEnum.Font:
                fmt.Font.SetAllMembers(add);
                break;

            case ApplyFormatTagEnum.FontFamily:
                fmt.Font.Name = add;
                break;

            case ApplyFormatTagEnum.FontSize:
                fmt.Font.Size20 = add;
                break;

            case ApplyFormatTagEnum.FontColor:
                fmt.Font.Color = add;
                break;

            case ApplyFormatTagEnum.FontStyle:
                fmt.Font.Style = add;
                break;

            case ApplyFormatTagEnum.FontUnderline:
                fmt.Font.Underline = add;
                break;

            case ApplyFormatTagEnum.NumericFormat:
                fmt.Format = add;
                break;

            case ApplyFormatTagEnum.Background:
                fmt.FillPattern.SetAllMembers(add);
                break;

            case ApplyFormatTagEnum.BackgroundPattern:
                fmt.FillPattern.Pattern = add;
                break;

            case ApplyFormatTagEnum.BackgroundColor:
                fmt.FillPattern.FgColor  = add;
                fmt.FillPattern.BgColor  = add;
                fmt.FillPattern.Gradient = add;
                break;

            case ApplyFormatTagEnum.TextAlign:
                fmt.HAlignment = add;
                fmt.VAlignment = add;
                break;

            case ApplyFormatTagEnum.TextAlignHoriz:
                fmt.HAlignment = add;
                break;

            case ApplyFormatTagEnum.TextAlignVert:
                fmt.VAlignment = add;
                break;

            case ApplyFormatTagEnum.Locked:
                fmt.Locked = add;
                break;

            case ApplyFormatTagEnum.Hidden:
                fmt.Hidden = add;
                break;

            case ApplyFormatTagEnum.TextWrap:
                fmt.WrapText = add;
                break;

            case ApplyFormatTagEnum.ShrinkToFit:
                fmt.ShrinkToFit = add;
                break;

            case ApplyFormatTagEnum.Rotation:
                fmt.Rotation = add;
                break;

            case ApplyFormatTagEnum.TextIndent:
                fmt.Indent = add;
                break;

            default:
                FlxMessages.ThrowException(FlxErr.ErrUndefinedId, tag, "TagNames.resx");
                break;
            }
        }
コード例 #4
0
ファイル: Form1.cs プロジェクト: trnka/TMS-FlexCel.NET-demos
        private void AddData(ExcelFile Xls)
        {
            string TemplateFile = "template.xls";

            if (cbXlsxTemplate.Checked)
            {
                if (!XlsFile.SupportsXlsx)
                {
                    throw new Exception("Xlsx files are not supported in this version of the .NET framework");
                }
                TemplateFile = "template.xlsm";
            }

            // Open an existing file to be used as template. In this example this file has
            // little data, in a real situation it should have as much as possible. (Or even better, be a report)
            Xls.Open(Path.Combine(PathToExe, TemplateFile));

            //Find the cell where we want to fill the data. In this case, we have created a named range "data" so the address
            //is not hardcoded here.
            TXlsNamedRange DataCell = Xls.GetNamedRange("Data", -1);

            //Add a chart with totals
            AddChart(DataCell, Xls);
            //Note that "DataCell" will change because we inserted rows above it when creating the chart. But we will keep using the old one.

            //Add the captions. This should probably go into the template, but in a dynamic environment it might go here.
            Xls.SetCellValue(DataCell.Top - 1, DataCell.Left, "Country");
            Xls.SetCellValue(DataCell.Top - 1, DataCell.Left + 1, "Quantity");

            //Add a rectangle around the cells
            TFlxApplyFormat ApplyFormat = new TFlxApplyFormat();

            ApplyFormat.SetAllMembers(false);
            ApplyFormat.Borders.SetAllMembers(true);  //We will only apply the borders to the existing cell formats
            TFlxFormat fmt = Xls.GetDefaultFormat;

            fmt.Borders.Left.Style   = TFlxBorderStyle.Double;
            fmt.Borders.Left.Color   = Color.Black;
            fmt.Borders.Right.Style  = TFlxBorderStyle.Double;
            fmt.Borders.Right.Color  = Color.Black;
            fmt.Borders.Top.Style    = TFlxBorderStyle.Double;
            fmt.Borders.Top.Color    = Color.Black;
            fmt.Borders.Bottom.Style = TFlxBorderStyle.Double;
            fmt.Borders.Bottom.Color = Color.Black;
            Xls.SetCellFormat(DataCell.Top - 1, DataCell.Left, DataCell.Top, DataCell.Left + 1, fmt, ApplyFormat, true);  //Set last parameter to true so it draws a box.

            //Freeze panes
            Xls.FreezePanes(new TCellAddress(DataCell.Top, 1));


            Random Rnd = new Random();

            //Fill the data
            int z            = 0;
            int OutlineLevel = 0;

            for (int r = 0; r <= DataRows; r++)
            {
                //Fill the values.
                Xls.SetCellValue(DataCell.Top + r, DataCell.Left, Country[z % Country.Length]);  //For non C# users, "%" means "mod" or modulus in other languages. It is the rest of the integer division.
                Xls.SetCellValue(DataCell.Top + r, DataCell.Left + 1, Rnd.Next(1000));

                //Add the country to the outline
                Xls.SetRowOutlineLevel(DataCell.Top + r, OutlineLevel);
                //increment the country randomly
                if (Rnd.Next(3) == 0)
                {
                    z++;
                    OutlineLevel = 0;  //Break the group and create a new one.
                }
                else
                {
                    OutlineLevel = 1;
                }
            }

            //Make the "+" signs of the outline appear at the top.
            Xls.OutlineSummaryRowsBelowDetail = false;

            //Collapse the outline to the first level.
            Xls.CollapseOutlineRows(1, TCollapseChildrenMode.Collapsed);

            //Add Data Validation for the first column, it must be a country.
            TDataValidationInfo dv = new TDataValidationInfo(
                TDataValidationDataType.List,         //We will use a built in list.
                TDataValidationConditionType.Between, //This parameter does not matter since it is a list. It will not be used.
                "=\"" + GetCountryList() + "\"",      //We could have used a range of cells here with the values (like "=C1..C4") Instead, we directly entered the list in the formula.
                null,                                 //no need for a second formula, not used in List
                false,
                true,
                true,  //Note that as we entered the data directly in FirstFormula, we need to set this to true
                true,
                "Unknown country",
                "Please make sure that the country is in the list",
                false, //We will not use an input box, so this is false and the 2 next entries are null
                null,
                null,
                TDataValidationIcon.Stop);  //We will use the stop icon so no invalid input is permitted.

            Xls.AddDataValidation(new TXlsCellRange(DataCell.Top, DataCell.Left, DataCell.Top + DataRows, DataCell.Left), dv);

            //Add Data Validation for the second column, it must be an integer between 0 and 1000.
            dv = new TDataValidationInfo(
                TDataValidationDataType.WholeNumber, //We will request a number.
                TDataValidationConditionType.Between,
                "=0",                                //First formula marks the first part of the "between" condition.
                "=1000",                             //Second formula is the second part.
                false,
                false,
                false,
                true,
                "Invalid Quantity",
                null, //We will leave the default error message.
                true,
                "Quantity:",
                "Please enter a quantity between 0 and 1000",
                TDataValidationIcon.Stop);  //We will use the stop icon so no invalid input is permitted.
            Xls.AddDataValidation(new TXlsCellRange(DataCell.Top, DataCell.Left + 1, DataCell.Top + DataRows, DataCell.Left + 1), dv);


            //Search country "Unknown" and replace it by "no".
            //This does not make any sense here (we could just have entered "no" to begin)
            //but it shows how to do it when modifying an existing file
            Xls.Replace("Unknown", "no", TXlsCellRange.FullRange(), true, false, true);

            //Autofit the rows. As we keep the row height automatic this will not show when opening in Excel, but will work when directly printing from FlexCel.
            Xls.AutofitRowsOnWorkbook(false, true, 1);

            Xls.Recalc(); //Calculate the SUMIF formulas so we can sort by them. Note that FlexCel automatically recalculates before saving,
                          //but in this case we haven't saved yet, so the sheet is not recalculated. You do not normally need to call Recalc directly.

            //Sort the data. As in the case with replace, this does not make much sense. We could have entered the data sorted to begin
            //But it shows how you can use the feature.

            //Find the cell where the chart goes.
            TXlsNamedRange ChartRange = Xls.GetNamedRange("ChartData", -1);

            Xls.Sort(new TXlsCellRange(ChartRange.Top, ChartRange.Left, ChartRange.Top + Country.Length, ChartRange.Left + 1),
                     true, new int[] { 2 }, new TSortOrder[] { TSortOrder.Descending }, null);



            //Protect the Sheet
            TSheetProtectionOptions Sp = new TSheetProtectionOptions(false); //Create default protection options that allows everything.

            Sp.InsertColumns = false;                                        //Restrict inserting columns.
            Xls.Protection.SetSheetProtection("flexcel", Sp);
            //Set a modify password. Note that this does *not* encrypt the file.
            Xls.Protection.SetModifyPassword("flexcel", true, "flexcel");

            Xls.Protection.OpenPassword = "******";  //OpenPasword is the only password that will actually encrypt the file, so you will not be able to open it with flexcel if you do not know the password.

            //Select cell A1
            Xls.SelectCell(1, 1, true);
        }
コード例 #5
0
ファイル: UReportCollections.cs プロジェクト: mwilian/demos
 internal TConfigFormat(int aXF, TFlxApplyFormat aApplyFmt, bool aExteriorBorders)
 {
     XF              = aXF;
     ApplyFmt        = aApplyFmt;
     ExteriorBorders = aExteriorBorders;
 }