1
votes

I need export ListView to csv and i found solution from this answer.Here
The problem is that I am getting full line into one column.

I got this : enter image description here

I need this: enter image description here

Code below:

   public static void ListViewToCSV(ListView listView, string filePath, bool includeHidden)
    {
        //make header string
        StringBuilder result = new StringBuilder();
        WriteCSVRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listView.Columns[i].Text);

        //export data rows
        foreach (ListViewItem listItem in listView.Items)
            WriteCSVRow(result, listView.Columns.Count, i => includeHidden || listView.Columns[i].Width > 0, i => listItem.SubItems[i].Text);

        File.WriteAllText(filePath, result.ToString());
    }

    private static void WriteCSVRow(StringBuilder result, int itemsCount, Func<int, bool> isColumnNeeded, Func<int, string> columnValue)
    {
        bool isFirstTime = true;
        for (int i = 0; i < itemsCount; i++)
        {
            if (!isColumnNeeded(i))
                continue;

            if (!isFirstTime)
                result.Append(",");
            isFirstTime = false;

            result.Append(String.Format("\"{0}\"", columnValue(i)));
        }
        result.AppendLine();
    }

Any ideas what I have to do ?

1
A ListView is just a UI control, used to display multi-property/column data. It's not the data itself. If you want to export that data, use a library like CsvHelper or even hand-written code to export the data, not the control. - Panagiotis Kanavos
If your data comes as a collection of data items, eg List<Customer>, exporting it is easy. You can write each value to a file, or you can use CsvHelper and a single call to CsvWriter.WriteRecords(myCustomers) - Panagiotis Kanavos
BTW what you posted doesn't show any kind of problem with CSVs. It shows an attempt to open the CSV in Excel with the wrong settings. You can generate real Excel files with Epplus. Again, generating the file is as simple as a call to sheet.Cells.LoadFromCollection - Panagiotis Kanavos
@jdweng in this case the real problem is that the OP tried to open the file in Excel in a non-US locale. This has nothing to do with CSVs at all - Panagiotis Kanavos
@Karoliuakas this has nothing to do with CSVs, it's 100% about Excel and the end-user's locale settings. - Panagiotis Kanavos

1 Answers

2
votes

This has nothing to do with CSVs. The first screenshot shows a perfectly good CSV. It also shows an attempt to open that file in Excel.

Excel will use the end-user's locale to decide what column, decimal separators and date formats to use. When you double-click on a text file Excel will import that data using the end-user's locale settings as the default.

In most European countries , is the decimal separator so it can't be used as a list separator too. Otherwise you wouldn't know what 100,00,234 mean. Is that 2 or 3 columns?

In such cases, the typical list separator is ;. This is specified in the end user's locale settings. In most European countries you'd see 100,00;234

If you want your end users to be able to open the generated files in Excel with double-clicking you'll have to use the list and decimal separators that match their locales.

A better option would be to generate real Excel files with, eg Epplus. It doesn't require installing Excel on the client or server, and generates real Excel (xlsx) files.

Exporting your data can be as easy as a call to :

using (var pck = new ExcelPackage())
{
    var sheet = pck.Workbook.Worksheets.Add("sheet");
    sheet.Cells["C1"].LoadFromCollection(items);
    pck.SaveAs(new FileInfo(@"c:\workbooks\myworkbook.xlsx"));
}