簡體   English   中英

使用Filehelpers將WPF ListView數據導出到CSV時出現問題

[英]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

任何指針將不勝感激。

測試:通過

  • 版本3.2:沒有問題
  • 版本2.2:沒有問題

使用FileHelpers的任何版本都可以按預期工作。 我將以下代碼放入測試控制台,它運行完美,因此,我現在的唯一建議是,您要么不傳遞數據,要么嘗試寫入只讀或無效位置。

  • 您在Visual Studio的“輸出”選項卡中看到任何例外嗎?
  • 您是否已確認將數據輸入dataSource參數?
  • 您是否已確認要將export.csv寫入的完整路徑?
  • 您是否在Excel中打開了csv文件?

注意: 在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

CSV文件

標題,名字,姓氏,電子郵件,電話,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

筆記:

  • 沒有復制ID列,因此它始終為零,但這可能只是因為您的示例代碼。
  • 我認為建議在基類上使用通用FileHelperEngine而不是typeof()參數,因為這會初始化各種方法/屬性以利用T而不是通用對象。
  • 您可以嘗試將源代碼下載到FileHelpers,然后將項目直接鏈接到庫,以調試內部發生的情況。
  • 您之前確實提到過您遇到System。*。dll引用問題,請檢查您使用的是Full Framework,而不是Client Client,因為這可能會導致該問題。 我不確定W8通用應用程序是否允許這樣做。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM