Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I want to import an excel file in my program automatically.

At the moment, I can import a PDF and with that I have a path given for an excel file, that gets imported and shows the data in TextBoxes.

What I want now is that every time I import a PDF file, I want the program/application to automatically search for an Excel file (.xls or .xlsx) in the Debug folder and import it.

Regards
Aman Chaurasia

What I have tried:

Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            //string str;
            int rCnt;
            int cCnt;
            int rw = 0;
            int cl = 0;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(@"C:\Test\Text.xlsx");
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            rw = range.Rows.Count;
            cl = range.Columns.Count;

            for (rCnt = 2; rCnt <= rw; rCnt++)
            {
                for (cCnt = 1; cCnt <= cl; cCnt++)
                {
                    TitleTextBox.Text = (string)(range.Cells[2, 1] as Excel.Range).Value2;
                    PublisherTextBox.Text = (string)(range.Cells[2, 2] as Excel.Range).Value2;
                    FileNameTextBox.Text = (string)(range.Cells[2, 3] as Excel.Range).Value2;
                    IssueTextBox.Text = (string)(range.Cells[2, 4] as Excel.Range).Value2;
                    ReleaseDateTextBox.Text = (string)(range.Cells[2, 5] as Excel.Range).Value2;
                    PageCountTextBox.Text = Convert.ToString((range.Cells[2, 6] as Excel.Range).Value2);
                }
            }

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();
        }
Posted
Comments
Richard MacCutchan 7-May-18 4:50am    
The question is not clear, what does the PDF file have to do with this?
Primo Chalice 10-May-18 2:29am    
Actually, the PDF details as in Title, Publisher, Release Date, Number of Pages etc are included in the excel. So once the PDF is imported(loaded), I want the program to search for an Excel file in the same directory with a particular name and import it's data to TextBoxes.

Currently, I am using a sample Excel file with my own data and that is being imported by manual calling.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900