static void Main(string[] args)
        {
            try
            {
                using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    IApplication application = excelEngine.Excel;
                    application.DefaultVersion = ExcelVersion.Excel2016;

                    //Preserve data types as per the value
                    application.PreserveCSVDataTypes = true;

                    //Read the CSV file
                    Stream csvStream = File.OpenRead(Path.GetFullPath(@"../../../TemplateSales.csv"));;

                    //Reads CSV stream as a workbook
                    IWorkbook  workbook = application.Workbooks.Open(csvStream);
                    IWorksheet sheet    = workbook.Worksheets[0];

                    //Formatting the CSV data as a Table
                    IListObject table = sheet.ListObjects.Create("SalesTable", sheet.UsedRange);
                    table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium6;
                    IRange location = table.Location;
                    location.AutofitColumns();

                    //Apply the proper latitude & longitude numerformat in the table
                    TryAndUpdateGeoLocation(table, "Latitude");
                    TryAndUpdateGeoLocation(table, "Longitude");

                    //Apply currency numberformat in the table column 'Price'
                    IRange columnRange = GetListObjectColumnRange(table, "Price");
                    if (columnRange != null)
                    {
                        columnRange.CellStyle.NumberFormat = "$#,##0.00";
                    }

                    //Apply Date time numberformat in the table column 'Transaction_date'
                    columnRange = GetListObjectColumnRange(table, "Transaction_date");
                    if (columnRange != null)
                    {
                        columnRange.CellStyle.NumberFormat = "m/d/yy h:mm AM/PM;@";
                    }

                    //Sort the data based on 'Products'
                    IDataSort  sorter    = table.AutoFilters.DataSorter;
                    ISortField sortField = sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
                    sorter.Sort();

                    //Save the file in the given path
                    Stream excelStream;
                    excelStream = File.Create(Path.GetFullPath(@"../../../Output.xlsx"));
                    workbook.SaveAs(excelStream);
                    excelStream.Dispose();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected Exception:" + e.Message);
            }
        }
Exemplo n.º 2
0
    public override bool TryHandleEnter(
        QueryableSortContext context,
        ISortField field,
        ISortEnumValue?sortEnumValue,
        EnumValueNode valueNode,
        [NotNullWhen(true)] out ISyntaxVisitorAction?action)
    {
        if (sortEnumValue is null)
        {
            context.ReportError(
                ErrorHelper.CreateNonNullError(field, valueNode, context));

            action = null !;
            return(false);
        }

        if (context.Instance.Peek() is QueryableFieldSelector fieldSelector)
        {
            context.Operations.Enqueue(
                HandleOperation(
                    context,
                    fieldSelector,
                    field,
                    sortEnumValue));

            action = SyntaxVisitor.Continue;
            return(true);
        }

        throw new InvalidOperationException(
                  DataResources.QueryableSortHandler_InvalidSelector);
    }
        private ActionResult SortColor(SortOn sortOn, int firstIndex, OrderBy orderBy)
        {
            ExcelEngine excelEngine = new ExcelEngine();
            IWorkbook   book        = excelEngine.Excel.Workbooks.Open(ResolveApplicationDataPath(@"SortingData.xlsx"), ExcelOpenType.Automatic);

            book.Version = ExcelVersion.Excel2016;
            IWorksheet sheet = book.Worksheets[1];
            IRange     range = sheet["A2:C50"];

            IDataSort sorter = book.CreateDataSorter();

            sorter.SortRange = range;

            ISortField field = sorter.SortFields.Add(2, sortOn, orderBy);

            field.Color = Color.Red;
            field       = sorter.SortFields.Add(2, sortOn, orderBy);
            field.Color = Color.Blue;
            sorter.Sort();
            book.Worksheets.Remove(0);

            try
            {
                return(excelEngine.SaveAsActionResult(book, "Output.xlsx", HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open));
            }
            catch (Exception)
            {
            }

            book.Close();
            excelEngine.Dispose();
            return(View());
        }
Exemplo n.º 4
0
        private void SortValues(string outFileName)
        {
#if NETCORE
            IWorkbook book = excelEngine.Excel.Workbooks.Open(@"..\..\..\..\..\..\..\Common\Data\XlsIO\SortingData.xlsx", ExcelOpenType.Automatic);
#else
            IWorkbook book = excelEngine.Excel.Workbooks.Open(@"..\..\..\..\..\..\Common\Data\XlsIO\SortingData.xlsx", ExcelOpenType.Automatic);
#endif
            IWorksheet sheet = book.Worksheets[0];
            IRange     range = sheet["A2:D51"];

            //Create the data sorter.
            IDataSort sorter = book.CreateDataSorter();
            //Specify the range to sort.
            sorter.SortRange = range;

            //Specify the sort field attributes (column index and sort order)
            ISortField field = sorter.SortFields.Add((int)numColumn.SelectedIndex, sortOn, orderBy);


            if (chkAddLevel.IsChecked.Value)
            {
                field = sorter.SortFields.Add((int)numSecCol.SelectedIndex, sortOn, orderBy);
            }
            if (checkBox1.IsChecked.Value)
            {
                field = sorter.SortFields.Add((int)numthirdCol.SelectedIndex, sortOn, orderBy);
            }

            //sort the data based on the sort field attributes.
            sorter.Sort();
            book.Worksheets.Remove(1);
            book.SaveAs(outFileName);
            book.Close();
        }
Exemplo n.º 5
0
        private void SortColor(string outFileName)
        {
#if NETCORE
            IWorkbook book = excelEngine.Excel.Workbooks.Open(@"..\..\..\..\..\..\..\Common\Data\XlsIO\SortingData.xlsx", ExcelOpenType.Automatic);
#else
            IWorkbook book = excelEngine.Excel.Workbooks.Open(@"..\..\..\..\..\..\Common\Data\XlsIO\SortingData.xlsx", ExcelOpenType.Automatic);
#endif
            IWorksheet sheet = book.Worksheets[1];
            IRange     range = sheet["A2:C50"];

            IDataSort sorter = book.CreateDataSorter();
            sorter.SortRange = range;

            ISortField field = sorter.SortFields.Add(2, sortOn, orderBy);
            field.Color = System.Drawing.Color.Red;

            field       = sorter.SortFields.Add(2, sortOn, orderBy);
            field.Color = System.Drawing.Color.Blue;


            sorter.Sort();
            book.Worksheets.Remove(0);
            book.SaveAs(outFileName);
            book.Close();
        }
Exemplo n.º 6
0
        private void SortColor(string outFileName)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Xlsx;

                string    inputPath = "syncfusion.xlsiodemos.winui.Assets.XlsIO.SortingData.xlsx";
                Assembly  assembly  = typeof(Sorting).GetTypeInfo().Assembly;
                Stream    input     = assembly.GetManifestResourceStream(inputPath);
                IWorkbook book      = application.Workbooks.Open(input);

                IWorksheet sheet = book.Worksheets[1];
                IRange     range = sheet["A2:C50"];

                IDataSort sorter = book.CreateDataSorter();
                sorter.SortRange = range;

                ISortField field = sorter.SortFields.Add(2, sortOn, orderBy);
                field.Color = Syncfusion.Drawing.Color.Red;

                field       = sorter.SortFields.Add(2, sortOn, orderBy);
                field.Color = Syncfusion.Drawing.Color.Blue;

                sorter.Sort();
                book.Worksheets.Remove(0);

                MemoryStream stream = new MemoryStream();
                book.SaveAs(stream);
                Save(stream, outFileName);

                stream.Dispose();
            }
        }
Exemplo n.º 7
0
        private void SortValues(string outFileName)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Xlsx;

                string    inputPath = "syncfusion.xlsiodemos.winui.Assets.XlsIO.SortingData.xlsx";
                Assembly  assembly  = typeof(Sorting).GetTypeInfo().Assembly;
                Stream    input     = assembly.GetManifestResourceStream(inputPath);
                IWorkbook book      = application.Workbooks.Open(input);

                IWorksheet sheet = book.Worksheets[0];
                IRange     range = sheet["A2:D51"];

                //Create the data sorter.
                IDataSort sorter = book.CreateDataSorter();
                //Specify the range to sort.
                sorter.SortRange = range;

                //Specify the sort field attributes (column index and sort order)
                ISortField field = sorter.SortFields.Add(0, sortOn, orderBy);

                //sort the data based on the sort field attributes.
                sorter.Sort();
                book.Worksheets.Remove(1);


                MemoryStream stream = new MemoryStream();
                book.SaveAs(stream);
                Save(stream, outFileName);

                stream.Dispose();
            }
        }
Exemplo n.º 8
0
        private void SortColor(string outFileName)
        {
            #region Workbook Initialize
            //Get the path of Input File
            string     inputPath = GetFullTemplatePath("SortingData.xlsx");
            IWorkbook  workbook  = excelEngine.Excel.Workbooks.Open(inputPath, ExcelOpenType.Automatic);
            IWorksheet worksheet = workbook.Worksheets[1];
            #endregion

            #region Sorting Based on Colors
            IRange    range  = worksheet["A2:C50"];
            IDataSort sorter = workbook.CreateDataSorter();
            sorter.SortRange = range;
            ISortField field = sorter.SortFields.Add(2, sortOn, orderBy);
            field.Color = Color.Red;
            field       = sorter.SortFields.Add(2, sortOn, orderBy);
            field.Color = Color.Blue;

            sorter.Sort();
            #endregion

            #region Worksheet Remove
            workbook.Worksheets.Remove(0);
            #endregion

            #region Workbook Save and Close
            workbook.SaveAs(outFileName);
            workbook.Close();
            #endregion
        }
Exemplo n.º 9
0
 protected override QueryableSortOperation HandleOperation(
     QueryableSortContext context,
     QueryableFieldSelector fieldSelector,
     ISortField field,
     ISortEnumValue?sortEnumValue)
 {
     return(DescendingSortOperation.From(fieldSelector));
 }
        public override bool TryHandleEnter(
            QueryableSortContext context,
            ISortField field,
            ObjectFieldNode node,
            [NotNullWhen(true)] out ISyntaxVisitorAction?action)
        {
            if (node.Value.IsNull())
            {
                context.ReportError(
                    ErrorHelper.CreateNonNullError(field, node.Value, context));

                action = SyntaxVisitor.Skip;
                return(true);
            }

            if (field.RuntimeType is null)
            {
                action = null;
                return(false);
            }

            if (!(context.GetInstance() is QueryableFieldSelector lastFieldSelector))
            {
                throw new InvalidOperationException();
            }

            Expression lastSelector = lastFieldSelector.Selector;


            Expression nextSelector;

            if (field.Member is PropertyInfo propertyInfo)
            {
                nextSelector = Expression.Property(lastSelector, propertyInfo);
            }
            else if (field.Member is MethodInfo methodInfo)
            {
                nextSelector = Expression.Call(lastSelector, methodInfo);
            }
            else
            {
                throw new InvalidOperationException();
            }

            if (context.InMemory)
            {
                nextSelector = SortExpressionBuilder.IfNullThenDefault(
                    lastSelector,
                    nextSelector,
                    Expression.Default(field.RuntimeType.Source));
            }

            context.PushInstance(lastFieldSelector.WithSelector(nextSelector));
            context.RuntimeTypes.Push(field.RuntimeType);
            action = SyntaxVisitor.Continue;
            return(true);
        }
Exemplo n.º 11
0
 /// <inheritdoc />
 public virtual bool TryHandleLeave(
     TContext context,
     ISortField field,
     ObjectFieldNode node,
     [NotNullWhen(true)] out ISyntaxVisitorAction?action)
 {
     action = null;
     return(false);
 }
Exemplo n.º 12
0
 public static IError SortingVisitor_ListValues(ISortField field, ListValueNode node) =>
 ErrorBuilder.New()
 .SetMessage(
     DataResources.SortingVisitor_ListInput_AreNotSuported,
     field.DeclaringType.Name,
     field.Name)
 .AddLocation(node)
 .SetExtension(nameof(field), field)
 .Build();
Exemplo n.º 13
0
 /// <inheritdoc />
 public override bool TryHandleLeave(
     Neo4JSortVisitorContext context,
     ISortField field,
     ObjectFieldNode node,
     [NotNullWhen(true)] out ISyntaxVisitorAction?action)
 {
     context.Path.Pop();
     action = SyntaxVisitor.Continue;
     return(true);
 }
 public virtual bool TryHandleEnter(
     TContext context,
     ISortField field,
     ISortEnumValue?sortValue,
     EnumValueNode node,
     [NotNullWhen(true)] out ISyntaxVisitorAction?action)
 {
     action = null;
     return(false);
 }
        public static string GetName(this ISortField field)
        {
            string fieldName = field.Name;

            if (field.Member is { } p)
            {
                fieldName = p.Name;
            }

            return(fieldName);
        }
        private ActionResult SortValues(int firstIndex, int secIndex, int thirdIndex, bool secondLevel, bool thirdLevel, OrderBy orderBy)
        {
            string basePath = _hostingEnvironment.WebRootPath;

            ExcelEngine excelEngine = new ExcelEngine();
            FileStream  inputStream = new FileStream(basePath + @"/XlsIO/SortingData.xlsx", FileMode.Open, FileAccess.Read);
            IWorkbook   book = excelEngine.Excel.Workbooks.Open(inputStream); IWorksheet sheet = book.Worksheets[0];

            IRange range = sheet["A2:D51"];

            //Create the data sorter.
            IDataSort sorter = book.CreateDataSorter();

            //Specify the range to sort.
            sorter.SortRange = range;


            //Specify the sort field attributes (column index and sort order)
            ISortField field = sorter.SortFields.Add(firstIndex, SortOn.Values, orderBy);

            //sort the data based on the sort field attributes.

            if (secondLevel)
            {
                field = sorter.SortFields.Add(secIndex, SortOn.Values, orderBy);
            }

            if (thirdLevel)
            {
                field = sorter.SortFields.Add(thirdIndex, SortOn.Values, orderBy);
            }
            sorter.Algorithm = SortingAlgorithms.QuickSort;
            sorter.Sort();
            book.Worksheets.Remove(1);
            try
            {
                book.Version = ExcelVersion.Excel2013;
                MemoryStream ms = new MemoryStream();
                book.SaveAs(ms);
                ms.Position = 0;

                return(File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Output.xlsx"));
            }
            catch (Exception)
            {
            }

            // Close the workbook
            book.Close();
            excelEngine.Dispose();
            return(View());
        }
Exemplo n.º 17
0
    public override bool TryHandleEnter(
        QueryableSortContext context,
        ISortField field,
        ObjectFieldNode node,
        [NotNullWhen(true)] out ISyntaxVisitorAction?action)
    {
        if (node.Value.IsNull())
        {
            context.ReportError(
                ErrorHelper.CreateNonNullError(field, node.Value, context));

            action = SyntaxVisitor.Skip;
            return(true);
        }

        if (field.RuntimeType is null)
        {
            action = null;
            return(false);
        }

        if (!(context.GetInstance() is QueryableFieldSelector lastFieldSelector))
        {
            throw ThrowHelper.Sorting_InvalidState_ParentIsNoFieldSelector(field);
        }

        Expression lastSelector = lastFieldSelector.Selector;


        Expression nextSelector = field.Member switch
        {
            PropertyInfo i => Expression.Property(lastSelector, i),
            MethodInfo i => Expression.Call(lastSelector, i),
            { } i => throw ThrowHelper.QueryableSorting_MemberInvalid(i, field),
                  null => throw ThrowHelper.QueryableSorting_NoMemberDeclared(field),
        };

        if (context.InMemory)
        {
            nextSelector = SortExpressionBuilder.IfNullThenDefault(
                lastSelector,
                nextSelector,
                Expression.Default(field.RuntimeType.Source));
        }

        context.PushInstance(lastFieldSelector.WithSelector(nextSelector));
        context.RuntimeTypes.Push(field.RuntimeType);
        action = SyntaxVisitor.Continue;
        return(true);
    }
Exemplo n.º 18
0
        public static Type GetReturnType(
            this ISortField field)
        {
            if (field.Member is PropertyInfo propertyInfo)
            {
                return(propertyInfo.PropertyType);
            }

            if (field.Member is MethodInfo methodInfo)
            {
                return(methodInfo.ReturnType);
            }

            throw new InvalidOperationException();
        }
Exemplo n.º 19
0
        public static IError CreateNonNullError <T>(
            ISortField field,
            IValueNode value,
            ISortVisitorContext <T> context)
        {
            ISortInputType sortType = context.Types.OfType <ISortInputType>().First();

            return(ErrorBuilder.New()
                   .SetMessage(
                       DataResources.ErrorHelper_CreateNonNullError,
                       context.Fields.Peek().Name,
                       sortType.Visualize())
                   .AddLocation(value)
                   .SetExtension("expectedType", new NonNullType(field.Type).Visualize())
                   .SetExtension("sortType", sortType.Visualize())
                   .Build());
        }
Exemplo n.º 20
0
        protected override ISyntaxVisitorAction OnFieldEnter(
            TContext context,
            ISortField field,
            ObjectFieldNode node)
        {
            if (field.Handler is ISortFieldHandler <TContext, T> handler &&
                handler.TryHandleEnter(
                    context,
                    field,
                    node,
                    out ISyntaxVisitorAction? action))
            {
                return(action);
            }

            return(SyntaxVisitor.SkipAndLeave);
        }
Exemplo n.º 21
0
        private void SortValues(string outFileName)
        {
            #region Workbook Initialize
            //Get the path of the input file
            string     inputPath = GetFullTemplatePath("SortingData.xlsx");
            IWorkbook  workbook  = excelEngine.Excel.Workbooks.Open(inputPath, ExcelOpenType.Automatic);
            IWorksheet worksheet = workbook.Worksheets[0];

            #endregion

            #region Sorting Based on Values
            IRange range = worksheet["A2:D51"];

            //Create the data sorter.
            IDataSort sorter = workbook.CreateDataSorter();
            //Specify the range to sort.
            sorter.SortRange = range;

            //Specify the sort field attributes (column index and sort order)
            ISortField field = sorter.SortFields.Add((int)cmbFirst.SelectedIndex, SortOn.Values, orderBy);


            if (chkAddSecondLevel.Checked)
            {
                field = sorter.SortFields.Add((int)cmbSecond.SelectedIndex, SortOn.Values, orderBy);
            }

            if (chkAddThirdLevel.Checked)
            {
                field = sorter.SortFields.Add((int)cmbThird.SelectedIndex, SortOn.Values, orderBy);
            }

            //sort the data based on the sort field attributes.
            sorter.Sort();
            #endregion

            #region Remove the Worksheet
            workbook.Worksheets.Remove(1);
            #endregion

            #region Workbook Save and Close
            workbook.SaveAs(outFileName);
            workbook.Close();
            #endregion
        }
        public override bool TryHandleLeave(
            QueryableSortContext context,
            ISortField field,
            ObjectFieldNode node,
            [NotNullWhen(true)] out ISyntaxVisitorAction?action)
        {
            if (field.RuntimeType is null)
            {
                action = null;
                return(false);
            }

            // Deque last
            context.PopInstance();
            context.RuntimeTypes.Pop();

            action = SyntaxVisitor.Continue;
            return(true);
        }
Exemplo n.º 23
0
        /// <inheritdoc />
        public override bool TryHandleEnter(
            Neo4JSortVisitorContext context,
            ISortField field,
            ObjectFieldNode node,
            [NotNullWhen(true)] out ISyntaxVisitorAction?action)
        {
            if (node.Value.IsNull())
            {
                context.ReportError(
                    ErrorHelper.CreateNonNullError(field, node.Value, context));

                action = SyntaxVisitor.Skip;
                return(true);
            }

            context.Path.Push(field.GetName());
            action = SyntaxVisitor.Continue;
            return(true);
        }
Exemplo n.º 24
0
        protected override ISyntaxVisitorAction OnOperationEnter(
            TContext context,
            ISortField field,
            ISortEnumValue?sortValue,
            EnumValueNode valueNode)
        {
            if (sortValue?.Handler is ISortOperationHandler <TContext, T> handler &&
                handler.TryHandleEnter(
                    context,
                    field,
                    sortValue,
                    valueNode,
                    out ISyntaxVisitorAction? action))
            {
                return(action);
            }

            return(SyntaxVisitor.Skip);
        }
        private ActionResult SortValues(int firstIndex, int secIndex, int thirdIndex, bool secondLevel, bool thirdLevel, OrderBy orderBy)
        {
            ExcelEngine excelEngine = new ExcelEngine();
            IWorkbook   book        = excelEngine.Excel.Workbooks.Open(ResolveApplicationDataPath(@"SortingData.xlsx"), ExcelOpenType.Automatic);
            IWorksheet  sheet       = book.Worksheets[0];
            IRange      range       = sheet["A2:D51"];

            //Create the data sorter.
            IDataSort sorter = book.CreateDataSorter();

            //Specify the range to sort.
            sorter.SortRange = range;


            //Specify the sort field attributes (column index and sort order)
            ISortField field = sorter.SortFields.Add(firstIndex, SortOn.Values, orderBy);

            //sort the data based on the sort field attributes.

            if (secondLevel)
            {
                field = sorter.SortFields.Add(secIndex, SortOn.Values, orderBy);
            }

            if (thirdLevel)
            {
                field = sorter.SortFields.Add(thirdIndex, SortOn.Values, orderBy);
            }
            sorter.Algorithm = SortingAlgorithms.QuickSort;
            sorter.Sort();
            book.Worksheets.Remove(1);
            try
            {
                return(excelEngine.SaveAsActionResult(book, "Output.xlsx", HttpContext.ApplicationInstance.Response, ExcelDownloadType.Open));
            }
            catch (Exception)
            {
            }

            book.Close();
            excelEngine.Dispose();
            return(View());
        }
        private ActionResult SortColor(SortOn sortOn, int firstIndex, OrderBy orderBy)
        {
            string basePath = _hostingEnvironment.WebRootPath;

            ExcelEngine excelEngine = new ExcelEngine();
            FileStream  inputStream = new FileStream(basePath + @"/XlsIO/SortingData.xlsx", FileMode.Open, FileAccess.Read);
            IWorkbook   book        = excelEngine.Excel.Workbooks.Open(inputStream);

            book.Version = ExcelVersion.Excel2016;
            IWorksheet sheet = book.Worksheets[1];
            IRange     range = sheet["A2:C50"];

            IDataSort sorter = book.CreateDataSorter();

            sorter.SortRange = range;

            ISortField field = sorter.SortFields.Add(2, sortOn, orderBy);

            field.Color = Color.Red;
            field       = sorter.SortFields.Add(2, sortOn, orderBy);
            field.Color = Color.Blue;
            sorter.Sort();
            book.Worksheets.Remove(0);

            try
            {
                book.Version = ExcelVersion.Excel2013;
                MemoryStream ms = new MemoryStream();
                book.SaveAs(ms);
                ms.Position = 0;

                return(File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Output.xlsx"));
            }
            catch (Exception)
            {
            }

            // Close the workbook
            book.Close();
            excelEngine.Dispose();
            return(View());
        }
        /// <inheritdoc/>
        public override bool TryHandleEnter(
            Neo4JSortVisitorContext context,
            ISortField field,
            ISortEnumValue?sortValue,
            EnumValueNode node,
            [NotNullWhen(true)] out ISyntaxVisitorAction?action)
        {
            if (sortValue is null)
            {
                context.ReportError(ErrorHelper.CreateNonNullError(field, node, context));

                action = null !;
                return(false);
            }

            context.Operations
            .Enqueue(new Neo4JSortDefinition(context.Path.Peek(), _sortDirection));

            action = SyntaxVisitor.Continue;

            return(true);
        }
 partial void SortType_SetCondition(ref ISortField instance, ref ISortType setValue);
Exemplo n.º 29
0
        private async void btnGenerateExcel_Click_2(object sender, RoutedEventArgs e)
        {
            #region Workbook initialization
            //New instance of XlsIO is created.[Equivalent to launching MS Excel with no workbooks open].
            //The instantiation process consists of two steps.

            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Excel2013;

            Assembly  assembly     = typeof(DataSort).GetTypeInfo().Assembly;
            string    resourcePath = "Syncfusion.SampleBrowser.UWP.XlsIO.XlsIO.Tutorials.Samples.Assets.Resources.Templates.SortingData.xlsx";
            Stream    fileStream   = assembly.GetManifestResourceStream(resourcePath);
            IWorkbook workbook     = await application.Workbooks.OpenAsync(fileStream);

            #endregion

            #region sorting the data
            workbook.Version = ExcelVersion.Excel2007;
            IWorksheet worksheet = workbook.Worksheets[0];
            IRange     range     = worksheet["A2:D51"];

            IDataSort sorter = workbook.CreateDataSorter();
            sorter.SortRange = range;

            OrderBy orderBy = this.Ascending.IsChecked == true ? OrderBy.Ascending : OrderBy.Descending;

            string sortOn      = comboBox1.SelectedValue as string;
            int    columnIndex = GetSelectedIndex(sortOn);

            //Specify the sort field attributes (column index and sort order)
            ISortField field = sorter.SortFields.Add(columnIndex, SortOn.Values, orderBy);

            sorter.Algorithm = SortingAlgorithms.QuickSort;
            sorter.Sort();
            worksheet.UsedRange.AutofitColumns();
            #endregion


            #region Save the Workbook
            StorageFile storageFile;
            if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
            {
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
                savePicker.SuggestedFileName      = "DataSortSample";
                savePicker.FileTypeChoices.Add("Excel Files", new List <string>()
                {
                    ".xlsx",
                });
                storageFile = await savePicker.PickSaveFileAsync();
            }
            else
            {
                StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
                storageFile = await local.CreateFileAsync("DataSortSample.xlsx", CreationCollisionOption.ReplaceExisting);
            }


            if (storageFile != null)
            {
                //Saving the workbook
                await workbook.SaveAsAsync(storageFile);

                workbook.Close();
                excelEngine.Dispose();

                MessageDialog msgDialog = new MessageDialog("Do you want to view the Document?", "File has been saved successfully.");

                UICommand yesCmd = new UICommand("Yes");
                msgDialog.Commands.Add(yesCmd);
                UICommand noCmd = new UICommand("No");
                msgDialog.Commands.Add(noCmd);
                IUICommand cmd = await msgDialog.ShowAsync();

                if (cmd == yesCmd)
                {
                    // Launch the saved file
                    bool success = await Windows.System.Launcher.LaunchFileAsync(storageFile);
                }
            }
            else
            {
                workbook.Close();
                excelEngine.Dispose();
            }
            #endregion
        }
Exemplo n.º 30
0
 protected abstract QueryableSortOperation HandleOperation(
     QueryableSortContext context,
     QueryableFieldSelector fieldSelector,
     ISortField field,
     ISortEnumValue?sortEnumValue);