patterncsharpMinor
Validating Excel file columns
Viewed 0 times
fileexcelcolumnsvalidating
Problem
I'm looking for ways to improve this code (more readable, less redundant and maybe cleaner/faster).
The problem I needed to solve:
I was designated to implement a software that is going to validate an 96 column Excel file and if there is no error on it, create an XML file from it. In case there are any error in the excel file I have to display them to the user and indicate where the problem occurred.
How I attempted to solve the problem:
Since I knew the numbers of columns, I thought about making a class that represents an cell on the Excel file and include a string property to hold an possibly error description, by making that it'd make it easy to display the error log. So I actually created 2 collections of my class that is called
Here is the
This is the
For readability purposes, I create another 96 collections of
I've created 96 methods of validation one per column/collection:
```
private void NameValidation(IEnumerable excelColumn, List log)
{
foreach (Cell item in excelColumn)
{
if (string.IsNullOrEmpty(item.Value))
{
item.ErrorDescription = "You need to fill up this Cell.";
}
else
{
if (item.Value.Length > 27)
{
item.ErrorDescription = "The MAX length of this field is 27 characters.";
}
}
The problem I needed to solve:
I was designated to implement a software that is going to validate an 96 column Excel file and if there is no error on it, create an XML file from it. In case there are any error in the excel file I have to display them to the user and indicate where the problem occurred.
How I attempted to solve the problem:
Since I knew the numbers of columns, I thought about making a class that represents an cell on the Excel file and include a string property to hold an possibly error description, by making that it'd make it easy to display the error log. So I actually created 2 collections of my class that is called
Cell. One collection to hold all of Excel's cell values, and the other is the error log one.Here is the
Cell class code:public class Cell
{
public string Value { get; set; }
public int Row { get; set; }
public int Column { get; set; }
public string ErrorDescription { get; set; }
}This is the
Cells collection:Range worksheetCells = sheet.get_Range(firstCell, lastCell);
private List Cells = new List();
foreach (Range item in worksheetCells)
{
Cells.Add(new Cell{ Value = item.Text, Row = item.Row, Column = item.Column});
}For readability purposes, I create another 96 collections of
Cell, one for each column:var Name = Cells.Where(c => c.Column == 1);
....I've created 96 methods of validation one per column/collection:
```
private void NameValidation(IEnumerable excelColumn, List log)
{
foreach (Cell item in excelColumn)
{
if (string.IsNullOrEmpty(item.Value))
{
item.ErrorDescription = "You need to fill up this Cell.";
}
else
{
if (item.Value.Length > 27)
{
item.ErrorDescription = "The MAX length of this field is 27 characters.";
}
}
Solution
Consider using custom attributes.
Here is a possible variant:
Create a class representing the data from the excel file and decorate it with attributes.
Then create a class that gets excel and returns a list of this data using ExcelColumn attribute.
Then create a class to convert this data to XML.
Maybe you'll be able to use attributes for actual converting data to XML.
Of course it's only a very crude sketch, and only some parts of it may be useful for you, but I hope you've got the idea.
Here is a possible variant:
Create a class representing the data from the excel file and decorate it with attributes.
public class ExcelData
{
[ExcelColumn("Name")]
[Required]
[MaxLength(27)]
public string Name { get; set; }
[ExcelColumn("NextColumn")]
[Required]
public string NextColumn { get; set; }
//...another 94 property.
}Then create a class that gets excel and returns a list of this data using ExcelColumn attribute.
public class ExcelParser
{
public IEnumerable Parse(string fileName)
{
//Get file and return ExcelData, using ExcelColumn attribute.
}
}Then create a class to convert this data to XML.
public class ExcelDataToXmlConverter
{
private ExcelDataValidator _validator = new ExcelDataValidator();
public XDocument Convert(IEnumerable excelData)
{
foreach (var data in excelData)
{
var errors = new Dictionary>();
if (_validator.IsValid(data, out errors))
{
//Convert the row to XML
}
else
{
//Do something with errors.
}
}
}
}
public class ExcelDataValidator
{
//errors - is a list of errors where the key is the name of the field and value is the list of errors.
public bool IsValid(ExcelData excelData, out Dictionary> errors)
{
//Validate using different attributes: Required, MaxLength and so on.
}
}Maybe you'll be able to use attributes for actual converting data to XML.
Of course it's only a very crude sketch, and only some parts of it may be useful for you, but I hope you've got the idea.
Code Snippets
public class ExcelData
{
[ExcelColumn("Name")]
[Required]
[MaxLength(27)]
public string Name { get; set; }
[ExcelColumn("NextColumn")]
[Required]
public string NextColumn { get; set; }
//...another 94 property.
}public class ExcelParser
{
public IEnumerable<ExcelData> Parse(string fileName)
{
//Get file and return ExcelData, using ExcelColumn attribute.
}
}public class ExcelDataToXmlConverter
{
private ExcelDataValidator _validator = new ExcelDataValidator();
public XDocument Convert(IEnumerable<ExcelData> excelData)
{
foreach (var data in excelData)
{
var errors = new Dictionary<string, List<string>>();
if (_validator.IsValid(data, out errors))
{
//Convert the row to XML
}
else
{
//Do something with errors.
}
}
}
}
public class ExcelDataValidator
{
//errors - is a list of errors where the key is the name of the field and value is the list of errors.
public bool IsValid(ExcelData excelData, out Dictionary<string, List<string>> errors)
{
//Validate using different attributes: Required, MaxLength and so on.
}
}Context
StackExchange Code Review Q#4073, answer score: 6
Revisions (0)
No revisions yet.