[英]Trouble Exporting WPF ListView Data to CSV with Filehelpers
碰壁了,所以希望SO可以有所幫助,而我也沒有忽略先前回答的明顯問題。 我正在嘗試將數據從ListView(實際上是通過列表填充的SQLite數據)導出到新的CSV文件-尚無精美的文件選擇器,只需將文件保存在本地(這是Metro 8.1 App,但已部署到Surface 3,不是RT)。 我已經根據發現的示例創建了一個方法,但是它似乎並未寫入文件(嘗試導出后已在本地計算機上搜索,但未找到任何內容)。 它的編譯良好,並且在調試時沒有遇到任何異常,而且我正在使用Filehelpers 2.0,因為無法安裝當前版本(VS 2015社區)。 “候選”是數據源(數據庫/列表視圖)的類。
類:
using SQLite;
using FileHelpers;
namespace SolutionName.Model
{
[Table("Candidates")]
[DelimitedRecord(",")]
[IgnoreEmptyLines()]
[IgnoreFirst()]
public class Candidate
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string AreasInterest { get; set; }
} // end class Candidate
} // end namespace
方法(通過按鈕調用):
private void WriteCSVFile(List<Candidate> dataSource)
{
//filehelper object
FileHelperEngine engine = new FileHelperEngine(typeof(Candidate));
List<Candidate> csv = new List<Candidate>();
//convert any datasource to csv based object
foreach (var item in dataSource)
{
Candidate temp = new Candidate();
temp.Title = item.Title;
temp.FirstName = item.FirstName;
temp.LastName = item.LastName;
temp.Email = item.Email;
temp.Phone = item.Phone;
temp.AreasInterest = item.AreasInterest;
csv.Add(temp);
} // end foreach
//give file a name and header text
engine.HeaderText = "Title,FirstName,LastName,Email,Phone,AreaInterest";
//save file locally
engine.WriteFile("export.csv", csv);
} // end method WriteCSVFile
任何指針將不勝感激。
使用FileHelpers的任何版本都可以按預期工作。 我將以下代碼放入測試控制台,它運行完美,因此,我現在的唯一建議是,您要么不傳遞數據,要么嘗試寫入只讀或無效位置。
注意: 在Excel中打開CSV會導致CSV文件完全鎖定,因此您必須退出Excel或關閉該文件才能寫入該文件
static void TestMain2(string[] args)
{
List<Candidate> source = new List<Candidate>()
{
new Candidate() { Id = 1, Email = "test1@test.com", Title = "Mr", FirstName = "Fred", LastName = "Flintstone", AreasInterest = "Area1", Phone = "+44 1234 123123" },
new Candidate() { Id = 3, Email = "test2@test.com", Title = "Mr", FirstName = "Barney", LastName = "Rubble", AreasInterest = "Area2", Phone = "+44 1234 231231" },
new Candidate() { Id = 2, Email = "test3@test.com", Title = "Mrs", FirstName = "Wilma", LastName = "Flintstone", AreasInterest = "Area3", Phone = "+44 1234 312312" }
};
WriteCSVFile(source);
}
private static void WriteCSVFile(List<Candidate> dataSource)
{
//filehelper object
FileHelperEngine engine = new FileHelperEngine(typeof(Candidate));
List<Candidate> csv = new List<Candidate>();
//convert any datasource to csv based object
foreach (var item in dataSource)
{
Candidate temp = new Candidate();
temp.Title = item.Title;
temp.FirstName = item.FirstName;
temp.LastName = item.LastName;
temp.Email = item.Email;
temp.Phone = item.Phone;
temp.AreasInterest = item.AreasInterest;
csv.Add(temp);
} // end foreach
//give file a name and header text
engine.HeaderText = "Title,FirstName,LastName,Email,Phone,AreaInterest";
//save file locally
engine.WriteFile("export.csv", csv);
} // end method WriteCSVFile
標題,名字,姓氏,電子郵件,電話,AreaInterest
0,先生,弗雷德,打火石,test1 @ test.com,+ 44 1234 123123,區域1
0,先生,Barney,Rubble,test2 @ test.com,+ 44 1234 231231,Area2
0,太太,威爾瑪,打火石,test3 @ test.com,+ 44 1234 312312,Area3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.