如何用GemBox Spreadsheet替代StreamReader填充C# ComboBox?
使用GemBox Spreadsheet替代StreamReader填充ComboBox的C#实现方案
需求说明
我正在开发一款从CSV文件填充ComboBox的C#程序,原本采用StreamReader实现该功能,现需改用GemBox Spreadsheet来完成。以下是原StreamReader实现代码,以及程序中使用GemBox Spreadsheet更新CSV文件的示例代码,需要实现GemBox Spreadsheet填充ComboBox的功能。
原StreamReader实现代码
private static List<ItemLine> Readfile(string fileName, string defaultValueDescription) { string path = $"\\.\\Backend Files\\{fileName}"; var items = new List<ItemLine>(); int row = 0; // 读取文件 StreamReader reader = new StreamReader(path); while (reader.EndOfStream == false) { string lines = reader.ReadLine(); row++; if (row == 1) { // 添加默认项,跳过CSV第一行内容 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; } } // 将项添加到列表 items.Add(IL); } reader.Close(); return items; }
现有GemBox Spreadsheet更新CSV示例代码
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)); }
改用GemBox Spreadsheet实现的填充逻辑
以下是完全匹配原StreamReader逻辑的GemBox实现代码,保持了相同的业务规则:
private static List<ItemLine> ReadFileWithGemBox(string fileName, string defaultValueDescription) { string path = $"\\.\\Backend Files\\{fileName}"; var items = new List<ItemLine>(); // 初始化GemBox许可证,与更新代码保持一致 SpreadsheetInfo.SetLicense("FREE -LIMITED-KEY"); // 加载CSV文件 var workbook = ExcelFile.Load(path, new CsvLoadOptions(CsvType.CommaDelimited)); var worksheet = workbook.Worksheets[0]; // 添加默认项,对应原代码中跳过CSV第一行的逻辑 items.Add(new ItemLine { Description = defaultValueDescription, Quantity = 0 }); // 从CSV第二行开始遍历(原代码中row>=2的部分) for (int rowIndex = 1; rowIndex < worksheet.Rows.Count; rowIndex++) { var currentRow = worksheet.Rows[rowIndex]; int cellCount = currentRow.AllocatedCells.Count; ItemLine IL = new ItemLine(); if (cellCount == 2) { // 处理两列的CSV行 IL.Description = currentRow.Cells[0].Value?.ToString() ?? string.Empty; IL.Quantity = 0; if (int.TryParse(currentRow.Cells[1].Value?.ToString(), out int qty)) { IL.Quantity = qty; } } else if (cellCount >= 3) { // 处理三列及以上的CSV行 IL.Plan = currentRow.Cells[0].Value?.ToString() ?? string.Empty; IL.Description = currentRow.Cells[1].Value?.ToString() ?? string.Empty; IL.Quantity = 0; if (int.TryParse(currentRow.Cells[2].Value?.ToString(), out int qty)) { IL.Quantity = qty; } } // 将项添加到列表 items.Add(IL); } return items; }
关键说明
- 索引对应:GemBox的行索引从0开始,因此原代码中跳过的CSV第一行对应
worksheet.Rows[0],实际业务数据从rowIndex=1开始遍历 - 单元格值处理:通过
Value?.ToString()避免空值异常,与原代码的字符串分割逻辑对齐 - 许可证复用:保持与更新CSV代码相同的许可证设置,确保GemBox功能正常启用
- 业务逻辑一致:完全复刻原代码中对2列/3列以上CSV行的处理规则,以及Quantity的解析逻辑
内容的提问来源于stack exchange,提问作者Csumner
相关产品推荐
相关产品推荐

