Click here to Skip to main content
15,902,876 members
Home / Discussions / C#
   

C#

 
AnswerWrongMagicConstantInHorribleCodeException Pin
Luc Pattyn19-Mar-12 13:03
sitebuilderLuc Pattyn19-Mar-12 13:03 
GeneralRe: WrongMagicConstantInHorribleCodeException Pin
CCodeNewbie19-Mar-12 13:05
CCodeNewbie19-Mar-12 13:05 
GeneralRe: WrongMagicConstantInHorribleCodeException Pin
CCodeNewbie19-Mar-12 13:08
CCodeNewbie19-Mar-12 13:08 
Answerfloat maxDaysInInt=((float)int.MaxValue)/1000/60/60/24; Pin
Luc Pattyn19-Mar-12 13:25
sitebuilderLuc Pattyn19-Mar-12 13:25 
GeneralRe: float maxDaysInInt=((float)int.MaxValue)/1000/60/60/24; Pin
CCodeNewbie20-Mar-12 2:32
CCodeNewbie20-Mar-12 2:32 
AnswerRe: same enumeration, different results? Pin
Alan N19-Mar-12 13:29
Alan N19-Mar-12 13:29 
GeneralRe: same enumeration, different results? Pin
CCodeNewbie20-Mar-12 2:21
CCodeNewbie20-Mar-12 2:21 
QuestionQuery an Excel Spreadsheet Pin
PDTUM19-Mar-12 6:35
PDTUM19-Mar-12 6:35 
Good Morning,

I am trying to extract a column of data from an Excel spreadsheet and pass it to a list box. I thought this was straight forward and I have found several very similar code samples through Google and Code Project search, but I cannot get any to work. I have created several variations of each with my own syntax (which is usually simpler) but I keep getting this error:

ERROR: "The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data."

I have tried adding a piece of code that I found to make sure the process is not running (just guessing here...see Sample 1), but I do not think that it works either. Below are 2 different code snippets that produce the same error. I would appreciate it these can be corrected of if a new code sample can be demonstrated to cure this issue ot produce the desired result.

Sample 1
C#
private DataTable GetExcel(string fileName, string sheetName)
	    {
            DataTable dt = new DataTable(sheetName);

	        try
	        {
                string ConStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", fileName); 
                string Sql = "SELECT * FROM [" + sheetName + "$]";

	            OleDbConnection Conn = new OleDbConnection(ConStr);
                OleDbCommand Comm = new OleDbCommand(Sql, Conn);
	                
	            OleDbDataAdapter da = new OleDbDataAdapter(Comm);

                Process[] process;
                process = System.Diagnostics.Process.GetProcessesByName("Excel");
                if (process.Length > 0)
                {
                    process[0].Kill();
                }

	            Conn.Open(); 
	            da.Fill(dt);
	            Conn.Close();
	        }
	        catch (Exception ex)
	        {
	            Console.WriteLine(ex.ToString());
	        }
	 
	        return dt;
	    }


Sample 2
C#
        private void GetExcelInfo()
        {
            string theFile = textBoxExcelFile.Text;
            string ConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + theFile + ";Extended Properties=Excel 8.0";
            string sheetName = "Sheet1";

            // Create the connection object
            OleDbConnection Conn = new OleDbConnection(ConStr);
            try
            {
                // Open connection
                Conn.Open();

                // Create OleDbCommand object and select data from worksheet Sheet1
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheetName + "$]", Conn);

                // Create new OleDbDataAdapter
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);

                // Create a DataSet which will hold the data extracted from the worksheet.
                DataSet ds = new DataSet();

                // Fill the DataSet from the data extracted from the worksheet.
                da.Fill(ds);

                // Bind the data to the GridView
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                // Close connection
                Conn.Close();
            }

        }

Thank you of your help...Pat

AnswerRe: Query an Excel Spreadsheet Pin
PIEBALDconsult19-Mar-12 8:26
mvePIEBALDconsult19-Mar-12 8:26 
GeneralRe: Query an Excel Spreadsheet Pin
PDTUM19-Mar-12 8:47
PDTUM19-Mar-12 8:47 
GeneralRe: Query an Excel Spreadsheet Pin
PIEBALDconsult19-Mar-12 12:04
mvePIEBALDconsult19-Mar-12 12:04 
AnswerRe: Query an Excel Spreadsheet Pin
Dave Kreskowiak19-Mar-12 9:10
mveDave Kreskowiak19-Mar-12 9:10 
GeneralRe: Query an Excel Spreadsheet Pin
PDTUM19-Mar-12 11:08
PDTUM19-Mar-12 11:08 
AnswerRe: Query an Excel Spreadsheet Pin
JOAT-MON19-Mar-12 13:27
JOAT-MON19-Mar-12 13:27 
GeneralRe: Query an Excel Spreadsheet Pin
PDTUM19-Mar-12 13:51
PDTUM19-Mar-12 13:51 
GeneralRe: Query an Excel Spreadsheet Pin
JOAT-MON19-Mar-12 14:12
JOAT-MON19-Mar-12 14:12 
AnswerRe: Query an Excel Spreadsheet Pin
PDTUM21-Mar-12 20:10
PDTUM21-Mar-12 20:10 
GeneralRe: Query an Excel Spreadsheet Pin
JOAT-MON21-Mar-12 21:48
JOAT-MON21-Mar-12 21:48 
Questionregex for validating port number Pin
anik butt19-Mar-12 6:28
anik butt19-Mar-12 6:28 
AnswerRe: regex for validating port number Pin
BobJanova19-Mar-12 6:33
BobJanova19-Mar-12 6:33 
AnswerRe: regex for validating port number Pin
phil.o19-Mar-12 7:19
professionalphil.o19-Mar-12 7:19 
AnswerRe: regex for validating port number Pin
PIEBALDconsult19-Mar-12 8:42
mvePIEBALDconsult19-Mar-12 8:42 
AnswerRe: regex for validating port number Pin
jschell19-Mar-12 9:58
jschell19-Mar-12 9:58 
AnswerRe: regex for validating port number Pin
V.19-Mar-12 21:07
professionalV.19-Mar-12 21:07 
QuestionVarying sliding window size(deflate ratio) Pin
Shailesh H19-Mar-12 1:59
Shailesh H19-Mar-12 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.