简体   繁体   中英

Lookup table in excel using EPPLUS

Using EPPlus , I have added 2 worksheets to an excel workbook like so

// add a new worksheet
ExcelWorksheet worksheetProduct = package.Workbook.Worksheets.Add("Product");
worksheetProduct.Cells[1, 1].Value = "Producer";

// add a new worksheet
ExcelWorksheet worksheetProducer = package.Workbook.Worksheets.Add("Producers");
//Add the headers
worksheetProducer.Cells[1, 1].Value = "Name";
worksheetProducer.Cells[1, 2].Value = "ID";

In the Product work sheet, I then add a Producer column with a drop down containing the names of the Producers, it was added as follows

var producerList = worksheetProduct.DataValidations.AddListValidation("A2");
producerList.Formula.ExcelFormula = "=ProducerList";

The user then fills in the spreadsheet and uploads it, I would like to get the ProducerId based on the value in the drop down, so in the Product sheet I can get the Producer Name easily enough,

string producerText = worksheetProduct.Cells[row, 1].Value.ToString();

How do I now query the Poducer worksheet to get the ID?

You need to read your Producers sheet first and load it into a lookup-friendly object like a Dictionary!

Here are some links to get you started with dictionaries:

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx http://www.dotnetperls.com/dictionary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM