У меня есть эти данные в файле Excel, и я могу прочитать весь файл, используя Open XML, и сохранить его в ArrayList.
Acme Auto Dealer
ProductID Model Type Color MaxSpeed Manufacturer
10-01 Fortuner SUV Black 200 Toyota
10-02 Innova MPV Red 200 Toyota
10-03 Altis Car White 200 Toyota
10-04 Land Cruiser SUV Red 200 Toyota
10-05 Vios Car Black 200 Toyota
10-06 Avanza MPV White 200 Toyota
10-07 Wigo Car Grey 200 Toyota
10-08 Rush SUV Red 200 Toyota
10-09 Alphard Van Black 200 Toyota
10-10 Hiace Van Black 200 Toyota
10-11 Hilux Truck Red 200 Toyota
10-12 City Car White 200 Honda
10-13 Brio Car Red 200 Honda
10-14 Civic Car Black 200 Honda
10-15 Jazz Car White 200 Honda
10-16 Accord Car Grey 200 Honda
10-17 Mobilio MPV Grey 200 Honda
10-18 CR-V SUV Black 200 Honda
10-19 Odyssey Van Red 200 Honda
10-20 Ranger Truck White 200 Ford
10-21 Expedition SUV Red 200 Ford
10-22 Explorer SUV Black 200 Ford
10-23 Everest SUV White 200 Ford
10-24 Fiesta Car Grey 200 Ford
10-25 Focus Car White 200 Ford
Вот код:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace OpenXMLSDK
{
public class Program
{
static void Main(string[] args)
{
var fileName = @"C:\XML\Vehicles.xlsx";
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
ArrayList data = new ArrayList();
foreach (Row r in sheetData.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
if (c.DataType != null && c.DataType == CellValues.SharedString)
{
var stringId = Convert.ToInt32(c.InnerText);
data.Add(workbookPart.SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>().ElementAt(stringId).InnerText);
}
else if (c.InnerText != null || c.InnerText != string.Empty)
{
data.Add(c.InnerText);
}
}
}
Console.ReadKey();
}
}
}
}
Теперь я хочу извлечь значения из определенных столбцов и сохранить эти значения в словаре .. Dictionary<string, List<KeyValuePair<string, string>>>
Пример вывода будет примерно таким:
10-01 List(Model=Fortuner, Type=SUV, MaxSpeed=200, Manufacturer=Toyota)
10-02 List(Model=Innova, Type=MPV, MaxSpeed=200, Manufacturer=Toyota)
10-03 List(Model=Altis, Type=Car, MaxSpeed=200, Manufacturer=Toyota)
10-04 List(Model=Land Cruiser, Type=SUV, MaxSpeed=200, Manufacturer=Toyota)
10-05 List(Model=Vios, Type=Car, MaxSpeed=200, Manufacturer=Toyota)
10-06 List(Model=Avanza, Type=MPV, MaxSpeed=200, Manufacturer=Toyota)
10-07 List(Model=Wigo, Type=Car, MaxSpeed=200, Manufacturer=Toyota)
10-08 List(Model=Rush, Type=SUV, MaxSpeed=200, Manufacturer=Toyota)
10-09 List(Model=Alphard, Type=Van, MaxSpeed=200, Manufacturer=Toyota)
10-10 List(Model=Hiace, Type=Van, MaxSpeed=200, Manufacturer=Toyota)
10-11 List(Model=Hilux, Type=Truck, MaxSpeed=200, Manufacturer=Toyota)
10-12 List(Model=City, Type=Car, MaxSpeed=200, Manufacturer=Honda)
10-13 List(Model=Brio, Type=Car, MaxSpeed=200, Manufacturer=Honda)
10-14 List(Model=Civic, Type=Car, MaxSpeed=200, Manufacturer=Honda)
10-15 List(Model=Jazz, Type=Car, MaxSpeed=200, Manufacturer=Honda)
10-16 List(Model=Accord, Type=Car, MaxSpeed=200, Manufacturer=Honda)
10-17 List(Model=Mobilio, Type=MPV, MaxSpeed=200, Manufacturer=Honda)
10-18 List(Model=CR-V, Type=SUV, MaxSpeed=200, Manufacturer=Honda)
10-19 List(Model=Odyssey, Type=Van, MaxSpeed=200, Manufacturer=Honda)
10-20 List(Model=Ranger, Type=Truck, MaxSpeed=200, Manufacturer=Ford)
10-21 List(Model=Expedition, Type=SUV, MaxSpeed=200, Manufacturer=Ford)
10-22 List(Model=Explorer, Type= SUV, MaxSpeed=200, Manufacturer=Ford)
10-23 List(Model=Everest, Type= SUV, MaxSpeed=200, Manufacturer=Ford)
10-24 List(Model=Fiesta, Type=Car, MaxSpeed=200, Manufacturer=Ford)
10-25 List(Model=Focus, Type=Car, MaxSpeed=200, Manufacturer=Ford)
Я уже некоторое время искал, но не могу понять, как это сделать.Любая помощь приветствуется.Заранее спасибо.