Exemple #1
0
        public string WriteRowString(CSVRowModel row)
        {
            StringBuilder builder     = new StringBuilder();
            bool          firstColumn = true;

            foreach (string value in row)
            {
                // Add separator if this isn't the first value
                if (!firstColumn)
                {
                    builder.Append(',');
                }

                // Implement special handling for values that contain comma or quote
                // Enclose in quotes and double up any double quotes
                if (value.IndexOfAny(new char[] { '"', ',' }) != -1)
                {
                    builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\""));
                }
                else
                {
                    builder.Append(value);
                }

                firstColumn = false;
            }

            return(builder.ToString());
        }
        public string WriteRowString(CSVRowModel row)
        {
            StringBuilder builder = new StringBuilder();
            bool firstColumn = true;
            foreach (string value in row)
            {
                // Add separator if this isn't the first value
                if (!firstColumn)
                    builder.Append(',');

                // Implement special handling for values that contain comma or quote
                // Enclose in quotes and double up any double quotes
                if (value.IndexOfAny(new char[] { '"', ',' }) != -1)
                    builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\""));
                else
                    builder.Append(value);

                firstColumn = false;
            }

            return builder.ToString();
        }
        /// <summary>
        /// Reads a row of data from a CSV file
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public bool ReadRow(CSVRowModel row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
                return false;

            int pos = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                // Special handling for quoted field
                if (row.LineText[pos] == '"')
                {
                    // Skip initial quote
                    pos++;

                    // Parse quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        // Test for quote character
                        if (row.LineText[pos] == '"')
                        {
                            // Found one
                            pos++;

                            // If two quotes together, keep one
                            // Otherwise, indicates end of value
                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }

                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    // Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ',')
                        pos++;

                    value = row.LineText.Substring(start, pos - start);
                }

                // Add field to list
                if (rows < row.Count)
                    row[rows] = value;
                else
                    row.Add(value);

                rows++;

                // Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText[pos] != ',')
                    pos++;
                if (pos < row.LineText.Length)
                    pos++;
            }
            // Delete any unused items
            while (row.Count > rows)
                row.RemoveAt(rows);

            // Return true if any columns read
            return (row.Count > 0);
        }
        public ActionResult BackUpPosts()
        {
            if (Session["User"] == null)
            {
                return RedirectToAction("LoginPage", "Application");
            }
            else if (!Session["Elevation"].Equals("Administrator"))
            {
                return RedirectToAction("LoggedInProfile");
            }
            else
            {
                DateTime currDate = DateTime.Now;
                string fileName = currDate.ToString("yyyy-MM-dd hh-mm-ss-tt") + ".csv";
                CloudStorageAccount csa = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["UnbrickableStorage"].ConnectionString);
                CloudBlobClient cbcl = csa.CreateCloudBlobClient();
                CloudBlobContainer cbcon = cbcl.GetContainerReference("backups");
                cbcon.CreateIfNotExists();
                CloudBlockBlob cbb = cbcon.GetBlockBlobReference(fileName);

                string fileLocation = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/"), fileName);

                if (System.IO.File.Exists(fileLocation))
                {
                    System.IO.File.Delete(fileLocation);
                }
                using (CSVFileWriter writer = new CSVFileWriter(fileLocation))
                {
                    CSVRowModel row = new CSVRowModel();
                    row.Add("username");
                    row.Add("date_posted");
                    row.Add("entry");
                    writer.WriteRow(row);
                    foreach (Post p in db.Posts.ToList())
                    {
                        row = new CSVRowModel();
                        row.Add(p.Account.username);
                        row.Add(p.date_posted.ToString());
                        row.Add(p.entry);

                        writer.WriteRow(row);
                    }

                    BackUp bu = new BackUp();
                    bu.filename = fileName;
                    bu.date_uploaded = currDate;
                    bu.num_posts = db.Posts.Count();
                    db.BackUps.Add(bu);
                    db.SaveChanges();

                }

                using (var fileStream = System.IO.File.OpenRead(fileLocation))
                {
                    cbb.UploadFromStream(fileStream);
                }
                if (System.IO.File.Exists(fileLocation))
                {
                    System.IO.File.Delete(fileLocation);
                }
                return RedirectToAction("AdminPage", "Application");
            }
        }
Exemple #5
0
        /// <summary>
        /// Reads a row of data from a CSV file
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public bool ReadRow(CSVRowModel row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
            {
                return(false);
            }

            int pos  = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                // Special handling for quoted field
                if (row.LineText[pos] == '"')
                {
                    // Skip initial quote
                    pos++;

                    // Parse quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        // Test for quote character
                        if (row.LineText[pos] == '"')
                        {
                            // Found one
                            pos++;

                            // If two quotes together, keep one
                            // Otherwise, indicates end of value
                            if (pos >= row.LineText.Length || row.LineText[pos] != '"')
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }

                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    // Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ',')
                    {
                        pos++;
                    }

                    value = row.LineText.Substring(start, pos - start);
                }

                // Add field to list
                if (rows < row.Count)
                {
                    row[rows] = value;
                }
                else
                {
                    row.Add(value);
                }

                rows++;

                // Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText[pos] != ',')
                {
                    pos++;
                }
                if (pos < row.LineText.Length)
                {
                    pos++;
                }
            }
            // Delete any unused items
            while (row.Count > rows)
            {
                row.RemoveAt(rows);
            }

            // Return true if any columns read
            return(row.Count > 0);
        }