日期:2014-05-18  浏览次数:20849 次

请教各位,winform中C#读取Excel数据
在做这部分的时候,用以下语句来读取Excel的数据
"Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\\test\\aa.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"
现在Excel文件中有94条数据,执行程序调试跟踪发现每次读出来的数据总会出现其中有几行是空,有时又不是空,请问这个问题该怎么解决呢?都快弄了一天了,我是用C#来做的

------解决方案--------------------
C# code
"Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\\test\\aa.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';" 
最后两个参数去掉试试!

------解决方案--------------------
还有一种情况!就是同一列上!如果类型不一致的话有时候也会出现漏掉的现象!
------解决方案--------------------
给你个例子 这是我以前已经测试好的
public void OpenExcel()
{
Application app = new Application();//Open Excel Application.
Workbooks wbs = app.Workbooks;
_Workbook wb = wbs.Add(@"D:\work\30yr_pmmsmnth.xls");//Open an existing workbook
Sheets shs = wb.Sheets;
_Worksheet wsh = (_Worksheet)shs.get_Item(1);//Select the first Sheet page

int a = 8;
int b = 20;
for (int i = 0; i < 6; i++)
{
if (i == 0)
{
for (; a < b; a++)
{
Range rr = wsh.get_Range("A" + a, "U" + a);
Object[,] saRet = (System.Object[,])rr.get_Value(System.Reflection.Missing.Value);
for (int n = 0; n < 7; n++)
{
string indexs = "";
if (n == 0)
{
indexs = "B5";
}
else if (n == 1)
{
indexs = "E5";
}
else if (n == 2)
{
indexs = "H5";
}
else if (n == 3)
{
indexs = "K5";
}
else if (n == 4)
{
indexs = "N5";
}
else if (n == 5)
{
indexs = "Q5";
}
else if (n == 6)
{
indexs = "T5";
}
Range range0 = wsh.get_Range(indexs, System.Reflection.Missing.Value);
Pmmsmnthyr pp = new Pmmsmnthyr();
pp.WeekDay = saRet[1, 1].ToString() + "/" + range0.Value2.ToString();
pp.Rate = saRet[1, 2 + n * 3].ToString();
pp.Pts = saRet[1, 3 + n * 3].ToString();
}
}
}
}
------解决方案--------------------
给你关键代码,这段代码我使用了好几年了
fileName = this.txtexcel.Text.Trim();
if (System.IO.File.Exists(fileName) == false)
{
MessageBox.Show("請確認Excel檔的位置,路徑請設置為英文或數字,並盡可能短,謝謝!");
return;
}
GetWindowThreadProcessId(t,out k);
System.Diagnostics.Process pe=System.Diagnostics.Process.GetProcessById(k); 
object missing=Type.Missing;