Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# Read Data From Excel
Message
 
À
20/12/2008 16:07:24
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01368976
Message ID:
01369003
Vues:
7
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;
>>            }
>>        }
>>    }
>>}
>>
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform