Как бы вы go сравнили два списка и сказали, что, если эти два списка имеют одинаковое значение, возьмите индекс двух разных пятен и выложите два других значения списка из двух разных списков с этим индексом. Оба списка имеют одинаковое количество значений. Оба списка, которые должны сравниваться, содержат идентификаторы, а два других, которые должны записать значение, содержат в себе categoryNames. Мой код выглядит следующим образом ...
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
namespace PrestaConverter
{
public class ExcellCreation
{
#region privata variabler
/// <summary>
/// WhatExcelFile Håller reda på Vilken excelfil som ska sparas,
/// Name Namnet på sparfilen,
/// count är en räknare som ger id till Kategorierna
/// </summary>
Hashtable myHashtable = new Hashtable();
private int WhatExcellFile;
private string name = "CategoriesCatalog";
private List<string> ColumnNames = new List<string>() {"ID",
"Active(0/1)",
"Name*",
"Parent Category",
"Root Category(0/1)",
"Description",
"Meta title",
"Meta keywords",
"Meta description",
"URL rewritten",
"Image URL"
};
private Categories categories1 = new Categories();
private Categories categories2 = new Categories();
private Categories categories3 = new Categories();
private int count;
ExcelConverter converter = new ExcelConverter();
#endregion
#region Skapa excel
/// <summary>
/// Funktion som skapar de nya Excelfilerna
/// </summary>
/// <param name="excelValue">Kategorinamnen sänds över till denna funktion för upplägg i ny fil</param>
/// <param name="rowAmmount">Används till att få tag på hur många rader som ska existera i excel filen</param>
/// <returns></returns>
public string CreateExcel(List<Categories> categories)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
return "Excel är inte korrekt installerat";
}
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
categories1 = categories[0];
categories2 = categories[1];
categories3 = categories[2];
for(int i = 0; i < categories1.CategoryId.Count; i++)
{
if(categories1.NewCategoryId.Contains(categories1.CategoryId[i]) == false && categories1.CategoryId[i].Substring(3,4) == "0000")
{
categories1.NewCategoryId.Add(categories1.CategoryId[i]);
categories1.NewCategoryName.Add(categories1.CategoryName[i]);
}
}
Console.WriteLine(categories1.NewCategoryId.Count);
for(int i = 0; i < ColumnNames.Count; i++)
{
xlWorkSheet.Cells[1, i + 1].Value2 = ColumnNames[i];
}
for(int i = 0; i < categories1.NewCategoryId.Count; i++)
{
xlWorkSheet.Cells[2 + i , 3].Value2 = categories1.NewCategoryName[i];
xlWorkSheet.Cells[2 + i, 4].Value2 = "Home";
}
for(int i = 0; i < categories2.CategoryId.Count; i++)
{
if(categories2.NewCategoryId.Contains(categories2.CategoryId[i]) == false && categories2.CategoryId[i].Substring(5, 2) == "00")
{
categories2.NewCategoryId.Add(categories2.CategoryId[i]);
categories2.NewCategoryName.Add(categories2.CategoryName[i]);
}
}
Console.WriteLine(categories2.NewCategoryName.Count);
for(int i = 0; i < categories1.NewCategoryId.Count; i++)
{
for(int j = 0; j < categories2.NewCategoryId.Count; j++)
{
if(categories1.NewCategoryId[i].Substring(0,3) == categories2.NewCategoryId[j].Substring(0, 3))
{
categories2.parentCategoryId.Add(categories1.NewCategoryId[i]);
categories2.parentCategoryName.Add(categories1.NewCategoryName[i]);
}
}
}
for(int i = 0; i < categories2.parentCategoryId.Count; i++)
{
xlWorkSheet.Cells[categories1.NewCategoryId.Count + i, 4].Value2 = categories2.parentCategoryName[i];
}
for(int i = 0; i < categories2.NewCategoryName.Count; i++)
{
xlWorkSheet.Cells[categories1.NewCategoryId.Count + i, 3].Value2 = categories2.NewCategoryName[i];
}
//Here saving the file in xlsx
xlWorkBook.SaveAs(@"C:\Users\Jens Svensson\Documents\" + name + ".xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, misValue,
misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true);
xlApp.Quit();
return "Funkar";
}
#endregion
private string createId(string Id, int start, int end)
{
return Id.Substring(start, end);
}
}
}
TestCode
public List<string> NewCategoryId = new List<string>();
public List<string> NewCategoryName = new List<string>();
public List<string> parentCategoryId = new List<string>();
public List<string> parentCategoryName = new List<string>();
parentCategoryId = <"3930000", "3930000",4230000, 5200000 >
parentCategoryName = <"Computers", "Computers", Toys, Furniture>
NewCategoryId = <"3931200", "4231400","5201300" "3931700">
NewCategoryName = <"Chairs","HP","ToyCars", "Lenovo">
for(int i = 0; i < NewCategoryId.Count; i++){
for(int j = 0; parentCategoryId.Count; j++){
if(parentCategoryId[j] == NewCategoryId[i]){
Console.Write(NewCategoryId[i] + " ")
Console.Write(NewCategoryName[i] + " ")
Console.Write(parentCategoryId[j] + " " )
Console.Write(parentCategoryName[j] + "\n")
}
}
}
Wanted result
3931700 Lenovo 3930000 Computers
3931200 HP 3930000 Computers
4231400 Chairs 4230000 Furniture
5201300 ToyCars 5201300 Toys
Это результат i sh, который я хочу, но я их просто забираю, так как это сделать?