Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# Read Data From Excel
Message
From
20/12/2008 22:02:03
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01368976
Message ID:
01369004
Views:
7
http://letmegooglethatforyou.com/?q=.net+oledb+excel

:-)

>Well that's wonderful!!!
>
>So post the code so we'll all know how!!
>
>
>
>>Gentleman,
>>I think both of your solutions create a dependency for Excel to be installed on the machine. I've read data in from Excel using an OleDbConnection with good success, and no dependencies required.
>>
>>>Here's what I've used before:
>>>
>>>
>>>using System;
>>>using System.Data;
>>>using System.Net.Mime;
>>>using Microsoft.Office.Interop.Excel;
>>>using System.Text;
>>>using System.Reflection;
>>>
>>>namespace ConsoleApplication3
>>>{
>>>    public class ExcelWrapper
>>>    {
>>>        public DataSet GetExcel(string fileName)
>>>        {
>>>            //
>>>            //  Include a reference to Microsoft.Office.Interop.Excel
>>>            //  ExcelWrapper wrap = new ExcelWrapper();
>>>            //  DataSet ds = wrap.GetExcel(@"c:\development\rda.xls");
>>>            //
>>>            //  I just ran this against an old spreadsheet an it worked fine.
>>>            //
>>>
>>>            Application oXL;
>>>            Workbook oWB;
>>>            Worksheet oSheet;
>>>            Range oRng;
>>>            try
>>>            {
>>>                //  creat a Application object
>>>                oXL = new ApplicationClass();
>>>                //   get   WorkBook  object
>>>                oWB = oXL.Workbooks.Open(fileName, 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);
>>>
>>>                //   get   WorkSheet object 
>>>                oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[1];
>>>                System.Data.DataTable dt = new System.Data.DataTable("dtExcel");
>>>                DataSet ds = new DataSet();
>>>                ds.Tables.Add(dt);
>>>                DataRow dr;
>>>
>>>                StringBuilder sb = new StringBuilder();
>>>                int jValue = oSheet.UsedRange.Cells.Columns.Count;
>>>                int iValue = oSheet.UsedRange.Cells.Rows.Count;
>>>                //  get data columns
>>>                for (int j = 1; j <= jValue; j++)
>>>                {
>>>                    dt.Columns.Add("column" + j, System.Type.GetType("System.String"));
>>>                }
>>>
>>>                //string colString = sb.ToString().Trim();
>>>                //string[] colArray = colString.Split(':');
>>>
>>>                //  get data in cell
>>>                for (int i = 1; i <= iValue; i++)
>>>                {
>>>                    dr = ds.Tables["dtExcel"].NewRow();
>>>                    for (int j = 1; j <= jValue; j++)
>>>                    {
>>>                        oRng = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[i, j];
>>>                        string strValue = oRng.Text.ToString();
>>>                        dr["column" + j] = strValue;
>>>                    }
>>>                    ds.Tables["dtExcel"].Rows.Add(dr);
>>>                }
>>>                return ds;
>>>            }
>>>            catch (Exception ex)
>>>            {
>>>                throw;
>>>            }
>>>        }
>>>    }
>>>}
>>>
Very fitting: http://xkcd.com/386/
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform