1

我在 lightswitch web 应用程序上使用 c# 和 VS2012,

我希望将我的数据导出为 CSV(在搜索屏幕上!),但无法访问任何 POC,

据我了解,有两个主要问题 - savefiledialog 必须直接由用户按钮引起,并且必须在主调度程序中发生,

我使用了这段代码:

        partial void mySearchScreen_Created()
        {
            var CSVButton = this.FindControl("ExportToCSV");
            CSVButton.ControlAvailable += ExportCSV_ControlAvailable;

        }
        private void ExportCSV_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            this.FindControl("ExportToCSV").ControlAvailable -= ExportCSV_ControlAvailable;
            Button Button = (Button)e.Control;
            Button.Click += ExportCSV_Click;
        }

        private void ExportCSV_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Microsoft.LightSwitch.Details.Client.IScreenCollectionProperty collectionProperty = this.Details.Properties.mySearch;
            var intPageSize = collectionProperty.PageSize;
            //Get the Current PageSize and store to variable
            collectionProperty.PageSize = 0;

            var dialog = new SaveFileDialog();
            dialog.Filter = "CSV (*.csv)|*.csv";
            if (dialog.ShowDialog() == true) {

                using (StreamWriter stream = new StreamWriter(dialog.OpenFile())) {
                    string csv = GetCSV();
                    stream.Write(csv);
                    stream.Close();
                    this.ShowMessageBox("Excel File Created Successfully. NOTE: When you open excel file and if you receive prompt about invalid format then just click yes to continue.", "Excel Export", MessageBoxOption.Ok);
                }
            }
            collectionProperty.PageSize = intPageSize;
            //Reset the Current PageSize
        }

        private string GetCSV()
        {
            StringBuilder csv = new StringBuilder();

            int i = 0;

            foreach (var orderRow_loopVariable in mySearch) {
                var orderRow = orderRow_loopVariable;
                ////HEADER
                if (i == 0) {
                    int c = 0;
                    foreach (var prop_loopVariable in orderRow.Details.Properties.All().OfType<Microsoft.LightSwitch.Details.IEntityStorageProperty>()) {
                        var prop = prop_loopVariable;
                        if (c > 0) {
                            csv.Append(",");//Constants.vbTab
                        }
                        c = c + 1;
                        csv.Append(prop.DisplayName);
                    }
                }
                csv.AppendLine("");

                ////DATA ROWS

                int c1 = 0;
                foreach (var prop_loopVariable in orderRow.Details.Properties.All().OfType<Microsoft.LightSwitch.Details.IEntityStorageProperty>()) {
                    var prop = prop_loopVariable;
                    if (c1 > 0) {
                        csv.Append(",");//Constants.vbTab
                    }
                    c1 = c1 + 1;
                    csv.Append(prop.Value);
                }
                i = i + 1;
            }

            if (csv.Length > 0) {
                return csv.ToString(0, csv.Length - 1);
            } else {
                return "";
            }
        }

这可行,但它只能让我获得第一页项目,在另一件事上我必须做我通过使用此代码解决了这个问题:

this.DataWorkspace.myDataContextData.MySearch(...).Execute();

然而,尝试而不是仅仅使用“MySearch”会给我以下错误:

t is not valid to call Execute() on a different Dispatcher than the ExecutableObject's Logic Dispatcher.

为什么在处理数据的系统构建上做这样一个与数据相关的基本事情(导出到 csv/excel)如此困难?

有任何想法吗 ?

4

2 回答 2

2

如果这是搜索屏幕的唯一用途,最简单的解决方法是关闭分页。为此,请转到屏幕设计器,突出显示左侧的查询,然后在属性中取消选中“支持分页”。

我不确定有什么限制,但您可以使用以下命令在不同的调度程序中运行一些代码:

                        this.Details.Dispatcher.BeginInvoke(() =>
                        {
                            //This runs on main dispatcher
                        });
于 2014-01-29T17:56:11.843 回答
0

我认为您的代码没有任何问题,但我注意到重置大型集合的页面大小需要一段时间,在此期间您的其余代码将继续执行。我想这就是为什么你只能得到第一页。我找到的唯一解决方案是等待。

当“文件下载 - 安全警告”对话框弹出时,请留意屏幕选项卡上的“忙碌”指示器以及网格底部的“第 x 页”状态(如果可以看到)。只有当忙碌指示符消失并且状态只是显示“页面”时,您才应单击“确定”继续。

我还没有想出一种以编程方式执行此操作的方法,因此除非您拥有非常严格控制的用户群,否则它不是一个非常有用的功能。但如果只有你和几个高级用户,它是可行的。我也不确定这是否在 VS2012 之后的版本中得到了改进。

完全取消分页查询的另一个答案可能有一个缺点。当网格集合显示在模式窗口中并且如果网格中有太多行时窗口变得无法关闭时,我已经尝试过这种解决方法。

菲尔

于 2015-03-12T12:52:33.467 回答