private void CreateFile(ExcelFile Xls)
    {
        //Create a new file. We could also open an existing file with Xls.Open
        Xls.NewFile(1, TExcelFileFormat.v2019);
        //Set some cell values.
        Xls.SetCellValue(1, 1, "Hello to everybody");
        Xls.SetCellValue(2, 1, 3);
        Xls.SetCellValue(3, 1, 2.1);
        Xls.SetCellValue(4, 1, new TFormula("=Sum(A2,A3)"));

        //Load an image from disk.
        string AssemblyPath = HttpContext.Current.Request.PhysicalApplicationPath;

        using (System.Drawing.Image Img = System.Drawing.Image.FromFile(Path.Combine(Path.Combine(AssemblyPath, "images"), "Test.bmp")))
        {
            //Add a new image on cell E5
            Xls.AddImage(2, 6, Img);
            //Add a new image with custom properties at cell F6
            Xls.AddImage(Img, new TImageProperties(new TClientAnchor(TFlxAnchorType.DontMoveAndDontResize, 2, 10, 6, 10, 100, 100, Xls), ""));
            //Swap the order of the images. it is not really necessary here, we could have loaded them on the inverse order.
            Xls.BringToFront(1);
        }

        //Add a comment on cell a2
        Xls.SetComment(2, 1, "This is 3");

        //Custom Format cells a2 and a3
        TFlxFormat f = Xls.GetDefaultFormat;

        f.Font.Name           = "Times New Roman";
        f.Font.Color          = Color.Red;
        f.FillPattern.Pattern = TFlxPatternStyle.LightDown;
        f.FillPattern.FgColor = Color.Blue;
        f.FillPattern.BgColor = Color.White;

        int XF = Xls.AddFormat(f);

        Xls.SetCellFormat(2, 1, XF);
        Xls.SetCellFormat(3, 1, XF);

        f.Rotation            = 45;
        f.FillPattern.Pattern = TFlxPatternStyle.Solid;
        int XF2 = Xls.AddFormat(f);

        //Apply a custom format to all the row.
        Xls.SetRowFormat(1, XF2);

        //Merge cells
        Xls.MergeCells(5, 1, 10, 6);
        //Note how this one merges with the previous range, creating a final range (5,1,15,6)
        Xls.MergeCells(10, 6, 15, 6);

        //Make sure rows are autofitted for pdf export.
        Xls.AutofitRowsOnWorkbook(false, true, 1);
    }
Esempio n. 2
0
        private void AddData(ExcelFile Xls)
        {
            //Fill the file with the contents of this c# file, many times so we can see many page breaks.
            int Row = 3;

            DumpFile(Xls, ref Row);

            Xls.AutofitRowsOnWorkbook(false, true, 1);
            Xls.AutoPageBreaks(50, 100); // we will use a 100% of page scale since we are printing to pdf.
                                         //If this was to create an Excel file, pagescale should be lower to
                                         //compensate the differences between page sizes in diiferent printers in Excel

            //Export the file to PDF so we can see the page breaks.
            using (FlexCelPdfExport pdf = new FlexCelPdfExport(Xls, true))
            {
                pdf.Export(saveFileDialog1.FileName);
            }
        }
Esempio n. 3
0
        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);
        }