2011-08-30 3 views
20

मैं

मैं एक एक्सेल फ़ाइल खोलने के लिए Microsoft.Office.Interop.Excel namespace उपयोग करने के लिए कोशिश कर रहा हूँ क्या करना चाहते हैं क्या (XSL या सीएसवी, लेकिन दुर्भाग्य से नहीं XSLX) और एक डेटासेट में आयात करें। मेरे पास वर्कशीट या कॉलम नामों पर नियंत्रण नहीं है, इसलिए मुझे उन्हें परिवर्तनों की अनुमति देने की आवश्यकता है।माइक्रोसॉफ्ट.ऑफिस.इंटरोप.एक्ससेल का उपयोग कर एक्सेल से डेटासेट में कैसे आयात करूं?

मैं क्या कोशिश की है

मैं अतीत में इस के OLEDB method की कोशिश की है, और यह (बग्गी, धीमी गति से, और आवश्यक Excel फ़ाइल स्कीमा को पहले से जानकारी) के साथ समस्याओं का एक बहुत कुछ था, इसलिए मैं फिर से ऐसा करने से बचना चाहता हूं। मैं जो करना चाहता हूं वह माइक्रोसॉफ्ट.ऑफिस.इंटरोप.एक्ससेल का उपयोग सीधे कार्यसेट को डेटासेट में आयात करने के लिए, या वर्कशीट्स के माध्यम से लूप करने और प्रत्येक को डेटाटेबल में लोड करने के लिए करना है।

मानो या नहीं, मुझे इसके लिए संसाधन ढूंढने में परेशानी हुई है। A few searches on StackOverflow ज्यादातर लोग रिवर्स (डेटासेट => एक्सेल), या ओएलडीडीबी तकनीक करने की कोशिश कर रहे हैं। Google अधिक उपयोगी नहीं रहा है।

क्या मैं इतना मिल गया है अब तक

public void Load(string filename, Excel.XlFileFormat format = Excel.XlFileFormat.xlCSV) 
    { 
     app = new Excel.Application(); 
     book = app.Workbooks.Open(Filename: filename, Format: format); 

     DataSet ds = new DataSet(); 

     foreach (Excel.Worksheet sheet in book.Sheets) 
     { 
      DataTable dt = new DataTable(sheet.Name); 
      ds.Tables.Add(dt); 

      //??? Fill dt from sheet 
     } 

     this.Data = ds; 
    } 

मैं या तो एक ही बार में पूरी किताब का आयात, या एक समय में एक चादर के माध्यम से पाशन के साथ ठीक हूँ। क्या मैं Interop.Excel के साथ ऐसा कर सकता हूं?

+0

इनाम जोड़ा जा रहा है क्योंकि मैं वास्तव में जानना चाहते हैं कि Interop.Excel इस क्षमता है चाहते हैं। एक्सेल फ़ाइल की सामग्री के पूर्व ज्ञान के बिना, अगर कोई भी संभवतः स्वचालित रूप से डेटा प्राप्त कर सकता है तो मैं बक्षीस का पुरस्कार दूंगा। –

+0

यह संभव है _if_ आप पहले से डेटा के बारे में कुछ गारंटी दे सकते हैं। मुझे डर है कि आप किसी भी पुरानी कार्यपुस्तिका के लिए काम करने और टैब्यूलर डेटा खींचने के लिए कुछ काम करना चाहते हैं। उस सारणीबद्ध डेटा को या तो नामित श्रेणियों द्वारा निर्धारित किया जाना चाहिए या इसे किसी तरह के सम्मेलन का पालन करना होगा। यदि यह इस सम्मेलन का पालन करता है कि कार्यपुस्तिका में प्रत्येक शीट पंक्ति 1 में शीर्षलेख पंक्ति के साथ रिकॉर्ड सेट की तरह दिखती है तो यह संभव होगा। अन्यथा, आप भाग्य से बाहर होंगे ... – adamleerich

उत्तर

31

कोडप्लेक्स पर एक ओपन सोर्स प्रोजेक्ट Excel Data Reader (पहले here होस्ट किया गया) का उपयोग करने के बारे में क्या? एक्सेल शीट्स से डेटा निर्यात करने के लिए यह वास्तव में अच्छा काम करता है।

नमूना लिंक को निर्दिष्ट पर दिए गए कोड:

FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); 

//1. Reading from a binary Excel file ('97-2003 format; *.xls) 
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); 
//... 
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx) 
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); 
//... 
//3. DataSet - The result of each spreadsheet will be created in the result.Tables 
DataSet result = excelReader.AsDataSet(); 
//... 
//4. DataSet - Create column names from first row 
excelReader.IsFirstRowAsColumnNames = true; 
DataSet result = excelReader.AsDataSet(); 

//5. Data Reader methods 
while (excelReader.Read()) 
{ 
//excelReader.GetInt32(0); 
} 

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

अद्यतन

के आसपास कुछ खोज के बाद, मैं इस लेख भर में आया था: Faster MS Excel Reading using Office Interop Assemblies। आलेख केवल दिए गए एक्सेल शीट से डेटा पढ़ने के लिए Office Interop Assemblies का उपयोग करता है। प्रोजेक्ट का स्रोत कोड भी है। मुझे लगता है कि यह लेख आप जो हासिल करने की कोशिश कर रहे हैं उस पर एक प्रारंभिक बिंदु हो सकता है। कि अगर, मदद करता है

अद्यतन 2

कोड के नीचे एक excel workbook लेता है और प्रत्येक excel worksheetexcel workbook अंदर के लिए सभी मूल्यों पाया पढ़ता देखें।

private static void TestExcel() 
    { 
     ApplicationClass app = new ApplicationClass(); 
     Workbook book = null; 
     Range range = null; 

     try 
     { 
      app.Visible = false; 
      app.ScreenUpdating = false; 
      app.DisplayAlerts = false; 

      string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); 

      book = app.Workbooks.Open(@"C:\data.xls", Missing.Value, Missing.Value, Missing.Value 
               , Missing.Value, Missing.Value, Missing.Value, Missing.Value 
              , Missing.Value, Missing.Value, Missing.Value, Missing.Value 
              , Missing.Value, Missing.Value, Missing.Value); 
      foreach (Worksheet sheet in book.Worksheets) 
      { 

       Console.WriteLine(@"Values for Sheet "+sheet.Index); 

       // get a range to work with 
       range = sheet.get_Range("A1", Missing.Value); 
       // get the end of values to the right (will stop at the first empty cell) 
       range = range.get_End(XlDirection.xlToRight); 
       // get the end of values toward the bottom, looking in the last column (will stop at first empty cell) 
       range = range.get_End(XlDirection.xlDown); 

       // get the address of the bottom, right cell 
       string downAddress = range.get_Address(
        false, false, XlReferenceStyle.xlA1, 
        Type.Missing, Type.Missing); 

       // Get the range, then values from a1 
       range = sheet.get_Range("A1", downAddress); 
       object[,] values = (object[,]) range.Value2; 

       // View the values 
       Console.Write("\t"); 
       Console.WriteLine(); 
       for (int i = 1; i <= values.GetLength(0); i++) 
       { 
        for (int j = 1; j <= values.GetLength(1); j++) 
        { 
         Console.Write("{0}\t", values[i, j]); 
        } 
        Console.WriteLine(); 
       } 
      } 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     } 
     finally 
     { 
      range = null; 
      if (book != null) 
       book.Close(false, Missing.Value, Missing.Value); 
      book = null; 
      if (app != null) 
       app.Quit(); 
      app = null; 
     } 
    } 

उपरोक्त कोड में, values[i, j] मूल्य है कि आप dataset में जोड़े जाने की जरूरत है। i पंक्ति को दर्शाता है, जबकि, j कॉलम को दर्शाता है।

+0

यदि भी संभव हो तो मैं Interop.Excel का उपयोग करना पसंद करूंगा, लेकिन यह एक महान बैकअप योजना है। धन्यवाद। –

+1

यदि आपको Interop.Excel से संबंधित कुछ मिलता है, तो कृपया इसे यहां पोस्ट करें। उस पर आधारित कुछ करना अच्छा होगा। – reggie

+0

@ जस्टिन मॉर्गन मैंने कोडप्रोजेक्ट पर एक लेख के लिंक के साथ उत्तर अपडेट किया है जो केवल कार्यालय इंटरऑप असेंबली का उपयोग करता है। यदि इससे सहायता मिलती है तो मुझे बताएं। – reggie

4

क्या आपने इसे देखा है? http://www.aspspider.com/resources/Resource510.aspx से:

public DataTable Import(String path) 
{ 
    Microsoft.Office.Interop.Excel.ApplicationClass app = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
    Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 

    Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet; 

    int index = 0; 
    object rowIndex = 2; 

    DataTable dt = new DataTable(); 
    dt.Columns.Add("FirstName"); 
    dt.Columns.Add("LastName"); 
    dt.Columns.Add("Mobile"); 
    dt.Columns.Add("Landline"); 
    dt.Columns.Add("Email"); 
    dt.Columns.Add("ID"); 

    DataRow row; 

    while (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 1]).Value2 != null) 
    { 
     rowIndex = 2 + index; 
     row = dt.NewRow(); 
     row[0] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 1]).Value2); 
     row[1] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 2]).Value2); 
     row[2] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 3]).Value2); 
     row[3] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 4]).Value2); 
     row[4] = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 5]).Value2); 
     index++; 
     dt.Rows.Add(row); 
    } 
    app.Workbooks.Close(); 
    return dt; 
} 
+0

मैं शायद इसे अनुकूलित कर सकता हूं। मुझे कॉलम गिनती या डेटा प्रकार पहले से नहीं पता है, इसलिए मुझे एक वर्कअराउंड मिलना होगा। मैं उम्मीद कर रहा था कि इंटरऑप.एक्ससेल में कुछ ऐसा था जो वास्तव में कुछ डेटा ऑब्जेक्ट पर सीधे रूपांतरण की अनुमति देगा, लेकिन मैं जो कुछ प्राप्त कर सकता हूं वह ले जाऊंगा। –

+0

इंटरऑप.एक्ससेल एक डेटा प्रकार से दूसरे डेटा में सीधे रूपांतरण की अनुमति नहीं देगा। यह आपको शीट ऑब्जेक्ट तक पहुंच प्रदान करेगा ताकि आप प्रत्येक शीट तक पहुंच सकें और फिर उस शीट में प्रत्येक रेंज पर फिर से चालू हो सकें, लेकिन यही वह है। एक बार जब आप वहां हों तो आपको रूपांतरण कोड लिखना होगा। – adamleerich

+0

यह कोड सुंदर संरचित दिखता है। क्या हमारे पास एक्सेल अपडेट करने के लिए विस्तार है? – Shalem

4
object[,] valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault); 

//Get the column names 
for (int k = 0; k < valueArray.GetLength(1);) 
{ 
    //add columns to the data table. 
    dt.Columns.Add((string)valueArray[1,++k]); 
} 

//Load data into data table 
object[] singleDValue = new object[valueArray.GetLength(1)]; 
//value array first row contains column names. so loop starts from 1 instead of 0 
for (int i = 1; i < valueArray.GetLength(0); i++) 
{ 
    Console.WriteLine(valueArray.GetLength(0) + ":" + valueArray.GetLength(1)); 
    for (int k = 0; k < valueArray.GetLength(1);) 
    { 
     singleDValue[k] = valueArray[i+1, ++k]; 
    } 
    dt.LoadDataRow(singleDValue, System.Data.LoadOption.PreserveChanges); 
} 
+0

अच्छा और तेज़। धन्यवाद। –

1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 
using System.Reflection; 
using Microsoft.Office.Interop.Excel; 

namespace trg.satmap.portal.ParseAgentSkillMapping 
{ 
    class ConvertXLStoDT 
    { 
     private StringBuilder errorMessages; 

     public StringBuilder ErrorMessages 
     { 
      get { return errorMessages; } 
      set { errorMessages = value; } 
     } 

     public ConvertXLStoDT() 
     { 
      ErrorMessages = new StringBuilder(); 
     } 

     public System.Data.DataTable XLStoDTusingInterOp(string FilePath) 
     { 
      #region Excel important Note. 
      /* 
      * Excel creates XLS and XLSX files. These files are hard to read in C# programs. 
      * They are handled with the Microsoft.Office.Interop.Excel assembly. 
      * This assembly sometimes creates performance issues. Step-by-step instructions are helpful. 
      * 
      * Add the Microsoft.Office.Interop.Excel assembly by going to Project -> Add Reference. 
      */ 
      #endregion 

      Microsoft.Office.Interop.Excel.Application excelApp = null; 
      Microsoft.Office.Interop.Excel.Workbook workbook = null; 


      System.Data.DataTable dt = new System.Data.DataTable(); //Creating datatable to read the content of the Sheet in File. 

      try 
      { 

       excelApp = new Microsoft.Office.Interop.Excel.Application(); // Initialize a new Excel reader. Must be integrated with an Excel interface object. 

       //Opening Excel file(myData.xlsx) 
       workbook = excelApp.Workbooks.Open(FilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); 

       Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets.get_Item(1); 

       Microsoft.Office.Interop.Excel.Range excelRange = ws.UsedRange; //gives the used cells in sheet 

       ws = null; // now No need of this so should expire. 

       //Reading Excel file.    
       object[,] valueArray = (object[,])excelRange.get_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault); 

       excelRange = null; // you don't need to do any more Interop. Now No need of this so should expire. 

       dt = ProcessObjects(valueArray);     

      } 
      catch (Exception ex) 
      { 
       ErrorMessages.Append(ex.Message); 
      } 
      finally 
      { 
       #region Clean Up     
       if (workbook != null) 
       { 
        #region Clean Up Close the workbook and release all the memory. 
        workbook.Close(false, FilePath, Missing.Value);      
        System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); 
        #endregion 
       } 
       workbook = null; 

       if (excelApp != null) 
       { 
        excelApp.Quit(); 
       } 
       excelApp = null;     

       #endregion 
      } 
      return (dt); 
     } 

     /// <summary> 
     /// Scan the selected Excel workbook and store the information in the cells 
     /// for this workbook in an object[,] array. Then, call another method 
     /// to process the data. 
     /// </summary> 
     private void ExcelScanIntenal(Microsoft.Office.Interop.Excel.Workbook workBookIn) 
     { 
      // 
      // Get sheet Count and store the number of sheets. 
      // 
      int numSheets = workBookIn.Sheets.Count; 

      // 
      // Iterate through the sheets. They are indexed starting at 1. 
      // 
      for (int sheetNum = 1; sheetNum < numSheets + 1; sheetNum++) 
      { 
       Worksheet sheet = (Worksheet)workBookIn.Sheets[sheetNum]; 

       // 
       // Take the used range of the sheet. Finally, get an object array of all 
       // of the cells in the sheet (their values). You can do things with those 
       // values. See notes about compatibility. 
       // 
       Range excelRange = sheet.UsedRange; 
       object[,] valueArray = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault); 

       // 
       // Do something with the data in the array with a custom method. 
       // 
       ProcessObjects(valueArray); 
      } 
     } 
     private System.Data.DataTable ProcessObjects(object[,] valueArray) 
     { 
      System.Data.DataTable dt = new System.Data.DataTable(); 

      #region Get the COLUMN names 

      for (int k = 1; k <= valueArray.GetLength(1); k++) 
      { 
       dt.Columns.Add((string)valueArray[1, k]); //add columns to the data table. 
      } 
      #endregion 

      #region Load Excel SHEET DATA into data table 

      object[] singleDValue = new object[valueArray.GetLength(1)]; 
      //value array first row contains column names. so loop starts from 2 instead of 1 
      for (int i = 2; i <= valueArray.GetLength(0); i++) 
      { 
       for (int j = 0; j < valueArray.GetLength(1); j++) 
       { 
        if (valueArray[i, j + 1] != null) 
        { 
         singleDValue[j] = valueArray[i, j + 1].ToString(); 
        } 
        else 
        { 
         singleDValue[j] = valueArray[i, j + 1]; 
        } 
       } 
       dt.LoadDataRow(singleDValue, System.Data.LoadOption.PreserveChanges); 
      } 
      #endregion 


      return (dt); 
     } 
    } 
} 
संबंधित मुद्दे