[英]C# Gembox Spreadsheet to populate comboBox
I am working on a program that uses comboboxes that are populated from a csv file, I initially just used streamreader to populate the combobox, but I was instructed to use gembox spreadsheet to populate it instead.我正在开发一个使用从 csv 文件填充的组合框的程序,我最初只是使用 streamreader 来填充组合框,但我被指示使用 gembox 电子表格来填充它。
Here is the streamreader code that I used:这是我使用的流阅读器代码:
private static List<ItemLine> Readfile(string fileName, string defaultValueDescription)
{
string path = $".\\Backend Files\\{fileName}";
var items = new List<ItemLine>();
int row = 0;
//read the file
StreamReader reader = new StreamReader(path);
while (reader.EndOfStream == false)
{
string lines = reader.ReadLine();
row++;
if (row == 1)
{
items.Add(new ItemLine { Description = defaultValueDescription, Quantity = 0 });
continue;
}
string[] itemFields = lines.Split(",");
ItemLine IL = new ItemLine();
if (itemFields.Length == 2)
{
IL.Description = itemFields[0];
IL.Quantity = 0;
if (int.TryParse(itemFields[1], out int qty))
{
IL.Quantity = qty;
}
}
else if (itemFields.Length >= 3)
{
IL.Plan = itemFields[0];
IL.Description = itemFields[1];
IL.Quantity = 0;
if (int.TryParse(itemFields[2], out int qty))
{
IL.Quantity = qty;
}
}
///Add Items to List
items.Add(IL);
}
reader.Close();
return items;
}
Here is an example of what I used gembox spreadsheet for in another part of my program which I use to update the csv files with, I think the code I am trying to figure out would look something like:这是我在我用来更新 csv 文件的程序的另一部分中使用 gembox 电子表格的示例,我认为我试图找出的代码看起来像:
public void updateFile(string filename, int range, ItemLine selectedItem, decimal outValue)
{
var fullPath = $".\\Backend Files\\{filename}";
SpreadsheetInfo.SetLicense("FREE -LIMITED-KEY");
var workbook = ExcelFile.Load(fullPath, new CsvLoadOptions(CsvType.CommaDelimited));
var worksheet = workbook.Worksheets[0];
for (int i = 1; i <= range; i++)
{
var plan = worksheet.Rows[i].Cells[0].Value;
var desc = worksheet.Rows[i].Cells[1].Value;
var csvDescription = plan + " - " + desc;
var platedescription = plan + " - ";
if (selectedItem.ComboDescription == csvDescription)
{
worksheet.Rows[i].Cells[2].Value = outValue;
}
if (selectedItem.plateDescription == platedescription)
{
worksheet.Rows[i].Cells[1].Value = outValue;
}
}
workbook.Save(fullPath, new CsvSaveOptions(CsvType.CommaDelimited));
}
Try this:试试这个:
private static List<ItemLine> ReadFile(string fileName, string defaultValueDescription)
{
string path = $".\\Backend Files\\{filename}";
var workbook = ExcelFile.Load(path, new CsvLoadOptions(CsvType.CommaDelimited));
var worksheet = workbook.Worksheets[0];
var items = new List<ItemLine>();
items.Add(new ItemLine { Description = defaultValueDescription, Quantity = 0 });
foreach (var row in worksheet.Rows)
{
var cells = row.AllocatedCells;
var il = new ItemLine();
items.Add(il);
if (cells.Count == 2)
{
il.Description = cells[0].Value.ToString();
il.Quantity = cells[1].ValueType == CellValueType.Int ? cells[1].IntValue : 0;
}
else if (cells.Count >= 3)
{
il.Plan = cells[0].Value.ToString();
il.Description = cells[1].Value.ToString();
il.Quantity = cells[2].ValueType == CellValueType.Int ? cells[2].IntValue : 0;
}
}
return items;
}
Also, I'm not sure about the logic around the defaultValueDescription
, it seems that in your StreamReader
approach you just skip the first line in CSV because of it.另外,我不确定
defaultValueDescription
周围的逻辑,似乎在您的StreamReader
方法中您只是跳过 CSV 中的第一行,因为它。
If that is the intended behavior, then add the .Skip(1)
extension method from System.Linq, like this:如果这是预期的行为,则从 System.Linq 添加
.Skip(1)
扩展方法,如下所示:
foreach (var row in worksheet.Rows.Skip(1))
{
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.