从2个Excel文件中读取的程序,使用户能够从两个文件中search相关的字段

这应该是一个程序,从2个Excel文件读取,将数据存储在两个数组,并使用户能够通过文本框search特定的zip /位置,并在search框中的input提示与地点与标签相关联,反之亦然。

我的问题是文件需要花费太多的时间才能成为红色。 我很乐意提供如何缩短阅读时间的build议。

using System; using System.Collections.Generic; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel ; namespace ExcelDateilesen_1 { public partial class Form1 : Form { string[] zip = new string[3000]; string[] place = new string[3000]; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Excel.Application objExcel = new Excel.Application(); Excel.Workbook objWorkbook = objExcel.Workbooks.Open (@"C:\Users\ggr\Documents\plz.xlsx"); Excel.Worksheet objWorksheet = objWorkbook.Worksheets["Tabelle1"]; Excel.Range objCell; int i = 1; do { objCell = objWorksheet.Cells[i, 1]; if (objCell.Value == null) break; if (objCell.Value is string) { zip[i - 1] = objCell.Value; } i++; } while (true); objWorkbook.Close(); objExcel.Quit(); Excel.Application objExcel1 = new Excel.Application(); Excel.Workbook objWorkbook1 = objExcel1.Workbooks.Open (@"C:\Users\ggr\Documents\ort.xlsx"); Excel.Worksheet objWorksheet1 = objWorkbook1.Worksheets["Tabelle1"]; Excel.Range objCell1; int j = 1; do { objCell1 = objWorksheet1.Cells[j, 1]; if (objCell1.Value == null) break; if (objCell1.Value is string) { place[j - 1] = objCell1.Value; } j++; } while (true); objWorkbook1.Close(); objExcel1.Quit(); } } 

}

用代码读取每个包含3000行数据的Excel文件需要大约20秒的时间。 我创build了一个方法(ReadSheet)来取代Excel工作表,并返回3000行的第一列数据的string数组,而不是通过Excel文件循环查找。 我使用一个对象Array来获取所有的单元格值,然后将其转换为一个string数组。 阅读速度更快。 不幸的是,我无法利用worksheet.UsedRange函数获取所有行,无论多less次,因为它一直给我错误的数字。 我将不得不考虑这一点。 希望这可以帮助。

编辑:更改为UsedRange获取所有单元格。 我遇到的问题是我使用的Excel表格。

 public partial class Form1 : Form { string[] zip; string[] place; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Excel.Application objExcel = new Excel.Application(); Excel.Workbook objWorkbook = objExcel.Workbooks.Open(@"C:\Users\John\Desktop\cs\TestExcel2_3000.xlsx"); Excel.Worksheet objWorksheet = objWorkbook.Worksheets["Tabelle1"]; zip = ReadSheet(objWorksheet); objWorkbook.Close(); Excel.Workbook objWorkbook1 = objExcel.Workbooks.Open(@"C:\Users\John\Desktop\cs\TestExcel3_3000.xlsx"); Excel.Worksheet objWorksheet1 = objWorkbook1.Worksheets["Tabelle1"]; place = ReadSheet(objWorksheet1); objWorkbook1.Close(); objExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(objWorkbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(objWorkbook1); System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel); } private string[] ReadSheet(Excel.Worksheet inSheet) { Excel.Range targetCells = inSheet.UsedRange; //Excel.Range targetCells = inSheet.Range["A1:A3000"]; Array allValues = (Array)targetCells.Cells.Value; return allValues.Cast<string>().ToArray(); } }