[英]Read a huge Excel column from C#
我有一个包含四列的Excel文档(可能在2010年或2013年以后不知道是否会成为问题)。 前三列存储电话号码,该电话号码基本上是包含10个或更多字符的字符串。 仅四列并将永远存储1、2、3或4,这是一个类别。 我需要检查A列中的每个数字是否出现在B列和C列中,所以我认为在阅读每列的所有Excel单元格并将其存储在列表中(尚未实现,因为问题将在下面解释) 。 为此,我编写以下代码:
private void btnCargarExcel_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (System.IO.File.Exists(openFileDialog1.FileName))
{
filePath.Text = openFileDialog1.FileName.ToString();
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string str;
int rCnt = 0;
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);
range = xlWorkSheet.UsedRange;
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
str = (range.Cells[rCnt, 1] as Excel.Range).Value2.ToString();
//bd.Add(cleanString(str));
bd.Add(cleanString(str, 10));
}
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
str = (range.Cells[rCnt, 2] as Excel.Range).Value2.ToString();
//bd.Add(cleanString(str));
bl.Add(cleanString(str, 10));
}
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
str = (range.Cells[rCnt, 3] as Excel.Range).Value2.ToString();
//bd.Add(cleanString(str));
cm.Add(cleanString(str, 10));
}
nrosProcesados.Text = bd.Count().ToString();
listBox1.DataSource = bd;
noProcesadosBL.Text = bl.Count().ToString();
listBox2.DataSource = bl;
noProcesadosCM.Text = cm.Count().ToString();
listBox3.DataSource = cm;
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
else
{
MessageBox.Show("No se pudo abrir el fichero!");
System.Runtime.InteropServices.Marshal.ReleaseComObject(appExcel);
appExcel = null;
System.Windows.Forms.Application.Exit();
}
}
}
因此,我对列中的槽单元进行了迭代,并在更改了一些字符串后将每个数字存储在列表中,如您在代码中所看到的。 这里的问题是,列A有797340个单元格,列B有91617个单元格,列C有95891个单元格,因此,如果我运行该应用程序,请加载Excel并等待我的PC挂出(即使具有12GB的RAM和Core i3处理器)我需要打开任务管理器并结束任务。 为了得到我想要的东西(只留下非重复的数字)而不挂出我的PC的最佳解决方案是什么? 在每个周期中,可以将事情划分为单独的线程吗(我对此不太了解,因为我是从C#开始的,所以会有所帮助)? 您对此主题有何看法?
编辑:添加一个新的和干净的方法
因此,在阅读和阅读本文并获得一些成员的帮助之后,我对代码进行了一些改进,但现在又遇到了另一个问题(在代码下方进行了注释)。 现在查看代码:
// this goes first when I declare vars
public static System.Array objRowAValues;
// this goes in action when I click the button (I leave only relevant part)
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range, rngARowLast;
string str;
int rCnt = 0;
long lastACell, fullRow;
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);
range = xlWorkSheet.UsedRange;
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;
现在,因为我将使用objRowAValues中的值填充ListBox,并且ListBox仅接受List作为数据源,所以我需要将objRowAValues转换为字符串列表。 我尝试了一下,但对我没有用。 有什么帮助吗?
不幸的是,我更像是VB.NET的家伙-所以我已经为您转换了一些代码。 我希望它能开箱即用-我在这里不使用这种工具,因此我无法对其进行测试。
public void test()
{
object[,] RaWData = null;
dynamic range = xlWorkSheet.UsedRange;
//i am unsure here about the correct order - I do not work with excel at Work, so you might have to change the following lange, if columns needs to be before rows or so
RaWData = range.value2;
//I am using a list here, because Lists are a lot easier to work with then simple arrays
List<List<string>> RealData = new List<List<string>>();
//start at 1 because the excel-delivered array do not have values at index 0 - this is the only 1-based array you will ever encounter in .net
for (x = 1; x <= Information.UBound(RaWData, 1); x++) {
List<string> templist = new List<string>();
for (y = 1; y <= Information.UBound(RaWData, 2); y++) {
templist.Add(RaWData[x, y].ToString());
}
RealData.Add(templist);
}
//you should be finished here...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.