2012-11-15 30 views
0

我使用下面的代码来选择Excel文件中的所有数据,并想知道是否可以从第三行开始并读取文件中的其余数据..Excel文件数据到数据表

excelConnectionString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFileName + ";Extended Properties=Excel 12.0"; 
      // Create Connection to Excel Workbook 
      using (OleDbConnection excelConnection = 
       new OleDbConnection(excelConnectionString)) 
       {      
        excelConnection.Open(); 
        System.Data.DataTable dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
        string[] excelSheet = new String[dt.Rows.Count]; 
        int sheet = 0; 
        foreach (DataRow row in dt.Rows) 
        { 
         excelSheet[sheet] = row["Table_Name"].ToString(); 
         sheet++; 

        } 
        excelDataTable.Clear(); 
        for (int i = 0; i < excelSheet.Length; i++) 
        { 
         OleDbCommand command = new OleDbCommand 
          ("Select * FROM [" + excelSheet[i] + "]", excelConnection); 

         excelAdapter.SelectCommand = command; 
         excelAdapter.Fill(excelDataTable); 
        } 
        excelConnection.Close(); 
       } 

      return excelDataTable; 
+0

文件中是否存在标题? – Derek

+0

不..但它们是以后需要用于其他方面的dat – user1776590

回答

0

你可以使用这样的事情ammend您的文件与OleDbConnection的阅读之前: -

private string customiseTemplate(string filename) 
    { 
     try 
     { 
      //Create Instance of Excel Appllication 
      Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); 

      //Create a WorkBook Object. 
      Workbooks wbks = app.Workbooks; 

      //Populate Workbook from Excel File. 
      _Workbook _wbk = wbks.Add(filename); 

      //Create Sheet Object. 
      Sheets shs = _wbk.Sheets; 

      //Determine which sheet to work with. In this instance there should only ever be one. 
      _Worksheet _wsh = (_Worksheet)shs.get_Item(1); 



      //Delete Unwanted Rows 1 to 3. 

      Microsoft.Office.Interop.Excel.Range 
      range1 = (Range)_wsh.get_Range("A1:A3", Missing.Value); 

      range1.EntireRow.Delete(Missing.Value); 

      // Release Range 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(range1); 

      //Save WorkBook. 

      app.AlertBeforeOverwriting = false; 

      string newFile = filename.Insert(3, "Ammended_"); 

      _wbk.SaveAs(newFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Missing.Value, 
      Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, 
      Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); 

      // CLose Excel App. 
      app.Quit(); 

      // Release unnecessary excel processes 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(app); 
      app = null; 

      return newFile; 

     } 
     catch(Exception x) 
     { 
      MessageBox.Show(x.Message); 
     } 

     return null; 

    } 
+0

我不想删除行,因为需要 – user1776590

+0

如果您不希望它们在您的DataTable中,它们需要什么? – Derek

+0

您可以随时读取数据,然后从DataTable中删除这三行。 Table.Rows [0] .Delete();上面的例子是保存一个新文件,它不会替换旧文件。 – Derek

0
var w = sqlData.AsEnumerable().Where(data => data.Field<String>("slideNo") == "5") 
       .Select(data=> data.Field<String>("QuestionStartText")); 

这是编码的答案...

0

德里克是对的,我同意他的看法。如果您仍想保留原始Excel文件数据并只显示数据表中的一部分,则需要首先将所有Excel数据导出到数据表,然后删除数据表中不需要的行。如果你不需要这些行,你可以先删除原来的excel文件中不需要的行,然后将excel导出为datatable。