Example #1
0
        public static Byte[] WriteToCSV(DateTime reportDate, DataSet ds)
        {
            Byte[] fileBytes = null;

            DataTable dt = ds.Tables[0];

            StringBuilder sb = new StringBuilder();

            IEnumerable <string> columnNames = dt.Columns.Cast <DataColumn>().Select(column => column.Caption.Insert(column.Caption.Length, "\"").Insert(0, "\""));

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

            //generate running numbers.
            int i = 1;

            foreach (DataRow row in dt.Rows)
            {
                string[] fields = new string[dt.Columns.Count];
                foreach (DataColumn col in dt.Columns)
                {
                    if (col.Ordinal == 0)
                    {
                        fields[0] = i.ToString();
                    }
                    else if (col.DataType == typeof(DateTime))
                    {
                        fields[col.Ordinal] = !Convert.IsDBNull(row[col.Ordinal]) ? Convert.ToDateTime(row[col.Ordinal]).ToString(Constants.DateTimeFormat.DefaultFullDateTime) : string.Empty;
                        //fields[col.Ordinal] = !Convert.IsDBNull(row[col.Ordinal])?Convert.ToDateTime(row[col.Ordinal]).ToString("dd/MM/yyyy HH:mm:ss"):string.Empty;
                    }
                    else if (col.ColumnName.ToLower().Contains("subject") || col.ColumnName.ToLower().Contains("remark") || col.ColumnName.ToLower().Contains("description"))
                    {
                        fields[col.Ordinal] = !Convert.IsDBNull(row[col.Ordinal]) ?
                                              ApplicationHelpers.StripHTML(HttpUtility.HtmlDecode(ApplicationHelpers.StripNewLine(row[col.Ordinal].ToString().Replace(",", string.Empty))))
                            : string.Empty;
                    }
                    else
                    {
                        fields[col.Ordinal] = !Convert.IsDBNull(row[col.Ordinal]) ? row[col.Ordinal].ToString() : string.Empty;
                    }

                    fields[col.Ordinal] = fields[col.Ordinal].Insert(fields[col.Ordinal].ToString().Length, "\"").Insert(0, "\"");
                }
                sb.AppendLine(string.Join(",", fields));
                i++;
            }

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

            //File.WriteAllText("test.csv", sb.ToString());

            //Read the Excel file in a byte array
            //fileBytes = Encoding.UTF8.GetBytes(sb.ToString());
            fileBytes = Encoding.GetEncoding(874).GetBytes(sb.ToString());

            return(fileBytes);
        }
Example #2
0
 public static string FormatDateTime(this DateTime date, string pattern, string culture)
 {
     return(date.ToString(pattern, ApplicationHelpers.GetCultureInfo(culture)));
 }
Example #3
0
 public override string FormatErrorMessage(string name)
 {
     return(string.Format(ApplicationHelpers.GetMessage(ErrorMessageString), _displayName));
 }