Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Ok, I have this Excel read this is working great my trouble is that my excel table is password protected. Can any one tell me what I need to do for this?

Thank you,

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace CK01
{
   class Program
   {
       static void Main(string[] args)
       {
           string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;"+
				"Data Source=C:\\CK01\\Book1.xls;"+
				"Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;IMEX=1" + (char)34;


           //Create the connection

           System.Data.OleDb.OleDbConnection ExcelConnection =
           new System.Data.OleDb.OleDbConnection
                            (ConnectionString);

           //create a string for the query
           string ExcelQuery;

           //Sheet1 is the sheet name
           //create the query:
           //read column  header  from the Excel file
           ExcelQuery = "Select 'COST CTR','CUSTOMER NAME' from [sheet1$]"; // from Sheet1";

           //use "Select * ... " to select the entire sheet
           //create the command
           System.Data.OleDb.OleDbCommand ExcelCommand = new System.Data.OleDb.OleDbCommand(ExcelQuery, ExcelConnection);

           //Open the connection
           ExcelConnection.Open();

           //Create a reader
           System.Data.OleDb.OleDbDataReader ExcelReader;
           ExcelReader = ExcelCommand.ExecuteReader();

           //For each row after the first
           while (ExcelReader.Read())
           {
               Console.WriteLine((ExcelReader.GetValue(0)).ToString());
               Console.WriteLine(ExcelReader.GetValue(1).ToString());
           }
           ExcelConnection.Close();
       }
   }
}

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...