2015-10-26 42 views
0

我需要讀取.xlsx文件而不使用第三方庫。移到右邊一行讀取c#中的Excel文件

我做了這種方式:

private void Upload(string filename) 
{ 
    FileStream stream = File.Open(filename, FileMode.Open, FileAccess.Read); 

    // Reading from a OpenXml Excel file (2007 format; *.xlsx) 
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); 

    //DataSet - The result of each spreadsheet will be created in the result.Tables 
    excelReader.IsFirstRowAsColumnNames = false; 
    DataSet result = excelReader.AsDataSet(); 

    //5. Data Reader methods 
    string value = GetValue(0, 0, excelReader); 

    //6. Free resources (IExcelDataReader is IDisposable) 
    excelReader.Close(); 
} 

我不知道如何在正確的單元讀取。問題不列位置(I可以使用,但該行位置

public string GetValue(int row, int col, IExcelDataReader excelReader) 
{ 
    string s; 

    // ??? how to positionate on the right row? 

    s = excelReader(column_value); 

    return s; 
} 
+0

我已經找到了正確的解決方案,在這個討論。http://stackoverflow.com/questions/33389393/reading-excel-in-c-sharp-where-some -columns-are-empty – Gioce90

回答

1

我創建和使用,下面的類讀取來自.xlsx.xls文件中的第一片材​​:

/// <summary> 
/// Reads a table from a spreadsheet. 
/// </summary> 
public sealed class XlsxReader 
{ 
    /// <summary> 
    /// Loads an xlsx file from a filepath into the datatable. 
    /// </summary> 
    /// <param name="filePath"></param> 
    /// <returns>Returns a DataTable with data from the first sheet.</returns> 
    public static DataTable FromXLSX(string filePath) 
    { 
     try 
     { 
      // Create the new datatable. 
      DataTable dtexcel = new DataTable(); 

      // Define the SQL for querying the Excel spreadsheet. 
      bool hasHeaders = true; 
      string HDR = hasHeaders ? "Yes" : "No"; 
      string strConn; 

      // If it is a xlsx file 
      if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx") 
       strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=1;\""; 
      else 
       strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1;\""; 

      // Create connection 
      OleDbConnection conn = new OleDbConnection(strConn); 
      conn.Open(); 

      // Get scheme 
      DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); 
      DataRow schemaRow = schemaTable.Rows[0]; 

      // Get sheet name 
      string sheet = schemaRow["TABLE_NAME"].ToString(); 
      if (!sheet.EndsWith("_")) 
      { 
       // Query data from the sheet 
       string query = "SELECT * FROM [" + sheet + "]"; 
       OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn); 
       dtexcel.Locale = CultureInfo.CurrentCulture; 

       // Fill the datatable. 
       daexcel.Fill(dtexcel); 
      } 

      // Close connection. 
      conn.Close(); 

      // Set the datatable. 
      return dtexcel; 
     } 
     catch { throw; } 
    } 
} 

它返回該紙張,作爲一個DataTable

+0

但以這種方式,我不能指定我想要的單元格。 – Gioce90

+0

您可以從DataTable中獲取正確的單元格。 – rotgers

+0

但不使用代碼單元格,對吧?像A01,B12 ... – Gioce90