我有三个 System.Array,其中基本上存储了 10 个字符的电话号码。定义是这样的:
private static System.Array objRowAValues;
private static System.Array objRowBValues;
private static System.Array objRowCValues;
我这样做是因为我读取了一个包含许多单元格(大约 1 000 000 个)的巨大 Excel 文件,并且处理List<string>
速度有点慢。稍后在我的代码中,我将 System.Array 更改为 aList<string>
主要是因为我填充了一个 ListBox 元素。这些是列表的定义
private List<string> bd = new List<string>();
private List<string> bl = new List<string>();
private List<string> cm = new List<string>();
我想检查 objRowAValues 中的任何值是否存在于 objRowBValues 或 objRowCValues 中,如果存在则删除现有值,我该怎么做?我是 C# 的新手,这是我的第一步。
编辑:这是我正在做的代码(仅相关部分):
private List<string> bd = new List<string>();
private static System.Array objRowAValues;
private List<string> bl = new List<string>();
private static System.Array objRowBValues;
private List<string> cm = new List<string>();
private static System.Array objRowCValues;
private List<string> pl = new List<string>();
private static Microsoft.Office.Interop.Excel.Application appExcel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range rngARowLast, rngBRowLast, rngCRowLast;
long lastACell, lastBCell, lastCCell, fullRow;
// this is the main method I use to load all three Excel files and fill System.Array and then convert to List<string>
private void btnCargarExcel_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (System.IO.File.Exists(openFileDialog1.FileName))
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
filePath.Text = openFileDialog1.FileName.ToString();
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(openFileDialog1.FileName, 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false,
false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet) xlWorkBook.Worksheets.get_Item(1);
fullRow = xlWorkSheet.Rows.Count;
lastACell = xlWorkSheet.Cells[fullRow, 1].End(Excel.XlDirection.xlUp).Row;
rngARowLast = xlWorkSheet.get_Range("A1", "A" + lastACell);
objRowAValues = (System.Array) rngARowLast.Cells.Value;
foreach (object elem in objRowAValues) { bd.Add(cleanString(elem.ToString(), 10)); }
nrosProcesados.Text = bd.Count().ToString();
listBox1.DataSource = bd;
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
executiontime.Text =
String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds/10).ToString();
}
else
{
MessageBox.Show("No se pudo abrir el fichero!");
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
appExcel = null;
System.Windows.Forms.Application.Exit();
}
}
}
// This is the method where I clean the existent values from bd (the List where I should remove existent values on bl and existent values on cm
private void btnClean_Click(object sender, EventArgs e)
{
pl = bd;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
pl.RemoveAll(element => bl.Contains((element)));
pl.RemoveAll(element => cm.Contains((element)));
textBox2.Text = pl.Count.ToString();
listBox4.DataSource = pl;
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
textBox6.Text =
String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10).ToString();
}