2

How can I export a ListView (populated with sqlite) to .CSV in WPF? i can do it on winforms but not in wpf.. pls help. thanks in advance.

enter image description here

4

1 に答える 1

6

ListView を Datatable に変換します。

var listView1 = new ListView();
DataTable table = new DataTable();
foreach (ListViewItem item in listView1.Items)
{
    table.Columns.Add(item.ToString());
    foreach (var it in item.SubItems)
         table.Rows.Add(it.ToString());
 }

csv を作成するには、次のコードを使用します。

public void CreateCSVFile(DataTable dt, string strFilePath)
{
    StreamWriter sw = new StreamWriter(strFilePath, false);

    int iColCount = dt.Columns.Count;
    for (int i = 0; i < iColCount; i++)
    {
        sw.Write(dt.Columns[i]);
        if (i < iColCount - 1)
        {
            sw.Write(",");
        }
    }
    sw.Write(sw.NewLine);

    foreach (DataRow dr in dt.Rows)
    {
        for (int i = 0; i < iColCount; i++)
        {
            if (!Convert.IsDBNull(dr[i]))
            {
                sw.Write(dr[i].ToString());
            }
            if (i < iColCount - 1)
            {
                sw.Write(",");
            }
        }
        sw.Write(sw.NewLine);
    }
    sw.Close();
}
于 2013-03-22T07:10:50.957 に答える