我编写了一个代码来读取Excel文件中的列。我对此使用Microsoft.Office.Interop.Excel,首先读取整个Range,然后在System.Array中写入,之后我使用System.Array值进行一些操作,最后我将其转换为List,因为我填充了ListBox元素。这是代码(仅相关部分):
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 List<string> bdCleanList;
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;
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)
{
if (elem != "")
{
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();
}
}
}
我使用带有~800 000个单元格的Excel文件进行测试,所需时间不到2分钟。然后我测试来自EPPlus的样本并且比我的方法更快,所以我认为在使用EPPlus而不是Microsoft.Office.Interop.Excel我认为也在使用OpenXML SDK(但找不到任何示例来满足我的目标所以我离开现在)。在示例中,他们使用此代码从Excel文件中读取:
ExcelWorksheet sheet = package.Workbook.Worksheets[1];
var query1= (from cell in sheet.Cells["d:d"] where cell.Value is double && (double)cell.Value >= 9990 && (double)cell.Value <= 10000 select cell);
当然他们在这里使用LINQ,但我对这个主题的问题是:
我是来自PHP世界的C#世界的新手,这是我的第一个项目
答案 0 :(得分:2)
您使用哪种方法? -EPPlus
您对此有何建议? - 我发现EPPLus要快得多。在我看来,这也是一个更容易使用的API。出于多种原因,一个是缺少COM互操作(速度和易用性)。也有较少的要求,特别是在部署到服务器环境时:没有安装Excel垃圾。
使用EPPlus或OpenXML SDK编写相同内容有何帮助? -EPPlus API相当直接。尝试并使用您迄今为止尝试的内容发布更具体的问题。
循环遍历单元格的另一种方法:
var firstColumnRows = sheet.Cells["A2:A"];
// Loop through rows in the first column, get values based on offset
foreach (var cell in firstColumnRows)
{
var column1CellValue = cell.GetValue<string>();
var neighborCellValue = cell.Offset(0, 1).GetValue<string>();
}