我在一个lightswitch网络应用程序上使用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)以处理数据是如此困难?
有什么想法吗?
答案 0 :(得分:2)
如果这是搜索屏幕的唯一用途,最简单的解决方法是关闭分页。要执行此操作,请转到屏幕设计器,突出显示左侧的查询,然后在属性中取消选中“支持分页”。
我不确定这些限制是什么,但您可以使用以下命令在不同的调度程序中运行一些代码:
this.Details.Dispatcher.BeginInvoke(() =>
{
//This runs on main dispatcher
});
答案 1 :(得分:0)
我认为您的代码没有任何问题,但我注意到在大型集合上重置页面大小需要一段时间,在此时其余代码将继续执行。我想这就是你只获得第一页的原因。我发现的唯一解决方案是等待。
当弹出“文件下载 - 安全警告”对话框时,请注意屏幕选项卡上的“忙碌”指示灯以及网格底部的“Page x of y”状态(如果可以看到它) 。只有当忙指示消失并且状态只显示“页面”时,才应单击“确定”继续。
我还没有找到一种以编程方式执行此操作的方法,因此除非您拥有非常严格控制的用户群,否则它不是一个非常有用的功能。但如果它只是你和几个超级用户,它是可行的。我还不确定VS2012之后的版本是否有所改进。
完全取消查询分页的另一个答案可能存在缺点。当网格集显示在模态窗口中时,我尝试了这种解决方法,如果网格中有太多行,则窗口变得不可关闭。
菲尔