Exemple #1
0
        private string SaveReportInFile()
        {
            DataTable     dt = new DataTable();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < DataGridName.Columns.Count; i++)
            {
                if (DataGridName.Columns[i].Visibility == Visibility.Visible)
                {
                    dt.Columns.Add(DataGridName.Columns[i].Header.ToString()
                                   .Replace(" ", "_")
                                   .Replace("[", "")
                                   .Replace("]", "")
                                   .Replace("%", "procent"));
                }
            }

            foreach (var rowObject in DataGridName.Items)
            {
                DataRow datar = dt.NewRow();
                foreach (DataGridColumn column in DataGridName.Columns)
                {
                    if (column.Visibility == Visibility.Visible)
                    {
                        var correctRow = DisplayNameHelper.GetPropertyValues(rowObject, column.Header.ToString());

                        foreach (var item in correctRow)
                        {
                            if (item.Key == column.Header.ToString())
                            {
                                datar[item.Key
                                      .Replace(" ", "_")
                                      .Replace("[", "")
                                      .Replace("]", "")
                                      .Replace("%", "procent")] = item.Value;
                            }
                        }
                    }
                }

                dt.Rows.Add(datar);
            }

            IEnumerable <string> columnNames = dt.Columns.Cast <DataColumn>().
                                               Select(column => column.ColumnName);

            sb.AppendLine(string.Join(";", columnNames));

            foreach (DataRow row in dt.Rows)
            {
                IEnumerable <string> fields = row.ItemArray.Select(field => field.ToString());
                sb.AppendLine(string.Join(";", fields));
            }

            return(sb.ToString());

            File.WriteAllText("D:\\ApkaRaporty\\test.csv", sb.ToString());
        }