大写一个对象列表 c#
Uppercase a List of object c#
我有这个class:
public class Customer
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Contact { get; set; }
}
我有一个代码可以使用 EPPLUS 从 excel 获取数据并将其放入列表中。
我想添加一个名为 name_upper 的新列,其中所有名称都将以 uppercase() 形式出现。
代码:
static void Main(string[] args)
{
var customer = ReadXls();
foreach (var item in customer)
{
Console.WriteLine($"Name:{item.Name}\n Phone:{item.Phone}\nContact:{item.Contact}\nEmail:{item.Email}\n");
}
}
private static List<Customer> ReadXls()
{
var response = new List<Customer>();
string FileName;
Console.WriteLine("Diretório do ficheiro:");
FileName = Console.ReadLine();
FileInfo existingFile = new FileInfo(FileName);
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using(ExcelPackage package = new ExcelPackage(existingFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
int row = 2;
int col = 1;
while(string.IsNullOrWhiteSpace(worksheet.Cells[row,col].Value?.ToString()) == false)
{
Customer customer = new();
customer.Name = worksheet.Cells[row, 4].Text.Trim();
customer.Phone = worksheet.Cells[row, 5].Text.Trim();
customer.Contact = worksheet.Cells[row, 7].Text.Trim();
customer.Email = worksheet.Cells[row, 8].Text.Trim();
response.Add(customer);
row += 1;
}
}
return response;
}
现在我需要名称以大写形式出现我不知道在哪里以及如何做。
如何让名字只显示大写
只需使用 ToUpper()
方法将每个字符转换为大写版本
customer.Name = worksheet.Cells[row, 4].Text.Trim().ToUpper();
更多关于 ToUpper
- https://docs.microsoft.com/en-us/dotnet/api/system.string.toupper?view=net-5.0
我有这个class:
public class Customer
{
public string Name { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Contact { get; set; }
}
我有一个代码可以使用 EPPLUS 从 excel 获取数据并将其放入列表中。 我想添加一个名为 name_upper 的新列,其中所有名称都将以 uppercase() 形式出现。 代码:
static void Main(string[] args)
{
var customer = ReadXls();
foreach (var item in customer)
{
Console.WriteLine($"Name:{item.Name}\n Phone:{item.Phone}\nContact:{item.Contact}\nEmail:{item.Email}\n");
}
}
private static List<Customer> ReadXls()
{
var response = new List<Customer>();
string FileName;
Console.WriteLine("Diretório do ficheiro:");
FileName = Console.ReadLine();
FileInfo existingFile = new FileInfo(FileName);
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using(ExcelPackage package = new ExcelPackage(existingFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
int row = 2;
int col = 1;
while(string.IsNullOrWhiteSpace(worksheet.Cells[row,col].Value?.ToString()) == false)
{
Customer customer = new();
customer.Name = worksheet.Cells[row, 4].Text.Trim();
customer.Phone = worksheet.Cells[row, 5].Text.Trim();
customer.Contact = worksheet.Cells[row, 7].Text.Trim();
customer.Email = worksheet.Cells[row, 8].Text.Trim();
response.Add(customer);
row += 1;
}
}
return response;
}
现在我需要名称以大写形式出现我不知道在哪里以及如何做。 如何让名字只显示大写
只需使用 ToUpper()
方法将每个字符转换为大写版本
customer.Name = worksheet.Cells[row, 4].Text.Trim().ToUpper();
更多关于 ToUpper
- https://docs.microsoft.com/en-us/dotnet/api/system.string.toupper?view=net-5.0