Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# Read Data From Excel
Message
De
20/12/2008 16:07:24
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
 
 
À
20/12/2008 14:43:37
John Baird
Coatesville, Pennsylvanie, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01368976
Message ID:
01368989
Vues:
6
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/
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform