日期:2014-05-17  浏览次数:20922 次

关于C#读excel的例子,看不懂,请教高手
Excel.Application excel = null;
   Excel.Workbooks wbs = null;
   Excel.Workbook wb = null;
   Excel.Worksheet ws = null;
   Excel.Range range1 = null;
   object Nothing = System.Reflection.Missing.Value;
     
   try
   {
    excel = new Excel.Application();
    excel.UserControl = true;
    excel.DisplayAlerts = false;
                     
    excel.Application.Workbooks.Open(this.FilePath,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing ) ;
     
    wbs = excel.Workbooks;
    wb = wbs[1];
    ws = (Excel.Worksheet)wb.Worksheets["Sheet2"];
     
          
    int rowCount = ws.UsedRange.Rows.Count;
    int colCount = ws.UsedRange.Columns.Count;
    if (rowCount <= 0)
     throw new InvalidFormatException("文件中没有数据记录");
    if (colCount < 4 ) 
     throw new InvalidFormatException("字段个数不对");
     
    for (int i = 0;i    {

     this.rowNo = i + 1;
     object[] row = new object[4];
     for (int j = 0;j<4;j++)
     {
      range1 = ws.get_Range(ws.Cells[i+2,j+1],ws.Cells[i+2,j+1]);
      row[j] = range1.Value;

      if (row[0] == null)
      {
       this.isNullRecord++;
       break;
      }
     }
                    
     if (this.isNullRecord > 0)
      continue;

     DataRow dataRow = this.readExcel(row);

     if (this.isNullRecord == 1)
      continue;
   
     if (this.verifyData(dataRow) == false)
      errFlag++;
   
     this.updateTableCurr(dataRow);
    }
     
   }
   finally
   {
    if (excel != null)
    {
     if (wbs != null)
     {
      if (wb&