0% found this document useful (0 votes)
3 views

Code to Clean the CSV File

Uploaded by

Kaleem Shahzad
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Code to Clean the CSV File

Uploaded by

Kaleem Shahzad
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.IO;
using OfficeOpenXml;

class Program
{
static void Main()
{
// Set the license context (Required for EPPlus 5+)
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

string inputFilePath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Downloads", "Fast people search.xlsx");
string outputFilePath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"Oklahoma Ok Leads.xlsx");

List<Person> records = new List<Person>();

// Read XLSX file


using (var package = new ExcelPackage(new FileInfo(inputFilePath)))
{
var worksheet = package.Workbook.Worksheets[0]; // Read the first sheet
int rowCount = worksheet.Dimension.Rows; // Get total rows
int colCount = worksheet.Dimension.Columns; // Get total columns

for (int row = 2; row <= rowCount; row++) // Start from row 2 to skip
headers
{
string fullName = worksheet.Cells[row, 1].Text.Trim();
string address = worksheet.Cells[row, 2].Text.Trim();
string contact = worksheet.Cells[row, 3].Text.Trim();

if (string.IsNullOrEmpty(fullName) ||
string.IsNullOrEmpty(address))
continue; // Skip empty rows

// Extract First Name and Last Name


string[] nameParts = fullName.Split(' ');
string firstName = nameParts[0];
string lastName = nameParts.Length > 1 ? string.Join(" ",
nameParts.Skip(1)) : "";

// Extract Zip Code (last part of the address)


string[] addressParts = address.Split(' ');
string zipCode = addressParts[^1]; // Get the last word as ZipCode

records.Add(new Person
{
FirstName = firstName,
LastName = lastName,
Address = address,
City = "Oklahoma City",
State = "OK",
ZipCode = zipCode,
Contact = contact
});
}
}

// Write to a new XLSX file


using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("Processed Data");

// Add headers
worksheet.Cells[1, 1].Value = "First Name";
worksheet.Cells[1, 2].Value = "Last Name";
worksheet.Cells[1, 3].Value = "Address";
worksheet.Cells[1, 4].Value = "City";
worksheet.Cells[1, 5].Value = "State";
worksheet.Cells[1, 6].Value = "ZipCode";
worksheet.Cells[1, 7].Value = "Contact";

int row = 2;
foreach (var person in records)
{
worksheet.Cells[row, 1].Value = person.FirstName;
worksheet.Cells[row, 2].Value = person.LastName;
worksheet.Cells[row, 3].Value = person.Address;
worksheet.Cells[row, 4].Value = person.City;
worksheet.Cells[row, 5].Value = person.State;
worksheet.Cells[row, 6].Value = person.ZipCode;
worksheet.Cells[row, 7].Value = person.Contact;
row++;
}

// Save to file
File.WriteAllBytes(outputFilePath, package.GetAsByteArray());
}

Console.WriteLine("Excel processing completed. Check 'output.xlsx'.");


}
}

// Class to hold structured data


public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string Contact { get; set; }
}

You might also like