2010-06-09 64 views
0

任何人都可以建议我如何在断开连接模式下使用ado.net读取c#中的excel文件。我的excel文件非常大,不能保存在memory.please中,提示将数据加载到数据集。 为现在我用Excel =的Microsoft.Office.Interop.Excel并添加Excel引用(COM),然后使用类似范围等对象读取它们在c中导入excel文件#

+2

,如果你告诉我们,这将有助于怎样(什么库等等)你现在正在阅读他们。 – 2010-06-09 05:35:21

回答

0

在这里,我用什么来从一个Excel工作表中读取数据:

private DbDataReader ReadExcelSheet(string file, string sheet) 
     { 
      string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties=Excel 8.0;"; 
      DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); 
      DbConnection connection = factory.CreateConnection(); 
      connection.ConnectionString = connStr; 
      DbCommand command = connection.CreateCommand(); 

      string query = BuildSelectQuery(sheet, names_mapping);//you need column names here 

      command.CommandText = query; 
      connection.Open(); 
      DbDataReader dr = command.ExecuteReader(); 
      return dr; 
     } 
1

以下是我正在使用的一个示例,对于希望从Excel电子表格中获取数据的任何人可能都很方便。

  • 它加载每个工作表到DataTable一个DataSet内。
  • 它假定你有你的头从A1将 X 1.

using System; 
using System.Data; 
using System.IO; 
using System.Runtime.InteropServices; 
using Excel = Microsoft.Office.Interop.Excel; 

public class clsExcelWriter : IDisposable 
{ 
    private Excel.Application oExcel; 
    private Excel._Workbook oBook; 
    private Excel._Worksheet oSheet; 

    // Used to store the name of the current file 
    public string FileName 
    { 
     get; 
     private set; 
    } 

    public clsExcelWriter(string filename) 
    { 
     // Initialize Excel 
     oExcel = new Excel.Application(); 

     if (!File.Exists(filename)) 
     { 
      // Create a new one? 
     } 
     else 
     { 
      oBook = (Excel._Workbook)oExcel.Workbooks.Open(filename); 
      oSheet = (Excel._Worksheet)oBook.ActiveSheet; 
     } 

     this.FileName = filename; 

     // Supress any alerts 
     oExcel.DisplayAlerts = false; 
    } 


    private string GetExcelColumnName(int columnNumber) 
    { 
     int dividend = columnNumber; 
     string columnName = String.Empty; 
     int modulo; 

     while (dividend > 0) 
     { 
      modulo = (dividend - 1) % 26; 
      columnName = Convert.ToChar(65 + modulo).ToString() + columnName; 
      dividend = (int)((dividend - modulo)/26); 
     } 
     return columnName; 
    } 

    public void Dispose() 
    { 
     // Lets make sure we release those COM objects! 
     if (oExcel != null) 
     { 
      Marshal.FinalReleaseComObject(oSheet); 
      oBook.Close(); 
      Marshal.FinalReleaseComObject(oBook); 
      oExcel.Quit(); 
      Marshal.FinalReleaseComObject(oExcel); 

      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
      GC.Collect(); 
      GC.WaitForPendingFinalizers(); 
     } 
    } 


    public static DataSet OpenFile(string filename) 
    { 
     DataSet ds = new DataSet(); 
     using (clsExcelWriter xl = new clsExcelWriter(filename)) 
     { 
      // Iterate through each worksheet 
      foreach (Excel._Worksheet sheet in xl.oBook.Worksheets) 
      { 
       // Create a new table using the sheets name 
       DataTable dt = new DataTable(sheet.Name); 

       // Get the first row (where the headers should be located) 
       object[,] xlValues = (object[,])sheet.get_Range("A1", xl.GetExcelColumnName(sheet.UsedRange.Columns.Count) + 1).Value; 

       // Iterate through the values to add new DataColumns to the DataTable 
       for (int i = 0; i < xlValues.GetLength(1); i++) 
       { 
        dt.Columns.Add(new DataColumn(xlValues[1, i + 1].ToString())); 
       } 

       // Now get the rest of the rows 
       xlValues = (object[,])sheet.get_Range("A2", xl.GetExcelColumnName(sheet.UsedRange.Columns.Count) + sheet.UsedRange.Rows.Count).Value; 

       for (int row = 0; row < xlValues.GetLength(0); row++) 
       { 
        DataRow dr = dt.NewRow(); 

        for (int col = 0; col < xlValues.GetLength(1); col++) 
        { 
         // xlValues array starts from 1, NOT 0 (just to confuse yee) 
         dr[dt.Columns[col].ColumnName] = xlValues[row + 1, col + 1]; 
        } 
        dt.Rows.Add(dr); 
       } 
       ds.Tables.Add(dt); 
      } 
     } 
     // Your DataSet should now be filled! :) 
     return ds; 
    } 
} 

}

使用

using System.Data; 
using ExcelWriter; 

namespace Test 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      DataSet ds = clsExcelWriter.OpenFile(@"C:\Results.xls"); 

      // Do some fancy stuff with the DataSet! xD 

      while (true) ; 
     } 
    } 
}