473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

Extract Data from Excel to fill DataGridView

126 100+
Hi Good Guys
I need your help. Please help me. I have asked to migrate VBNET2008 to C#Net2008 version of extracting data from Excel to fill DataGridView.

I tried to convert it and here are the not working new C# version:

Expand|Select|Wrap|Line Numbers
  1.  using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Data.OleDb;
  8. using System.Data.SqlClient;
  9. using System.IO;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using Microsoft.Office.Interop.Excel;
  14. using Microsoft.Office.Core;
  15. using System.Xml;
  16.  
  17.     private void btnImportExcel_Click(object sender, EventArgs e)
  18.         {  // open up excel and fill datagridview
  19.  
  20.             Excel.Application objExcel = new Excel.Application();
  21.             Excel.Workbook objBook = new Excel.Workbook(); 
  22.             objBook.Open(ExcelPathName);
  23.             Excel.Worksheet objSheet = new Excel.Worksheet();
  24.             objSheet.Activate;
  25.             objExcel.Visible= false;
  26.  
  27.             //common variable
  28.             Boolean bolFlag = true;
  29.             int excelRow = 2;
  30.             int excelCol = 0;
  31.             int DGVRow = 1;
  32.             int intRecCnt = 0;
  33.  
  34.             string strCell1 = "";
  35.             string strCell2 = "";
  36.             string strCell3 = "";
  37.             string strCell4 = "";
  38.             string strCell5 = "";
  39.             string strCell6 = "";
  40.             string strCell7 = "";
  41.             string strCell8 = "";
  42.             string strCell9 = "";
  43.             string strCell10= "";
  44.  
  45.             try
  46.             {
  47.                 while (bolFlag == true)
  48.                 {
  49.  
  50.                     strCell1 = (string)objSheet.Cells[excelRow, 1];
  51.                     strCell2 = (string)objSheet.Cells[excelRow, 2];
  52.                     strCell3 = (string)objSheet.Cells[excelRow, 3];
  53.                     strCell4 = (string)objSheet.Cells[excelRow, 4];
  54.                     strCell5 = (string)objSheet.Cells[excelRow, 5];
  55.                     strCell6 = (string)objSheet.Cells[excelRow, 6];
  56.                     strCell7 = (string)objSheet.Cells[excelRow, 7];
  57.                     strCell8 = (string)objSheet.Cells[excelRow, 8];
  58.                     strCell9 = (string)objSheet.Cells[excelRow, 9];
  59.                     strCell10 = (string)objSheet.Cells[excelRow, 10];
  60.  
  61.                     if ( strCell1 == "")
  62.                     {
  63.                         bolFlag = false;
  64.                         break;
  65.                     }
  66.  
  67.                    this.dataGridView1.Rows.Add(new string()( strCell1, strCell2, strCell3,strCell4,strCell5,strCell6,strCell7,strCell8,strCell9,strCell10)) ; 
  68.  
  69.                     //DataGridView1.Rows.Add()
  70.                     DGVRow += 1;
  71.                     excelRow += 1;
  72.  
  73.                     //initialise variables
  74.                     strCell1 = "";
  75.                     strCell2 = "";
  76.                     strCell3 = "";
  77.                     strCell4 = "";
  78.                     strCell5 = "";
  79.                     strCell6 = "";
  80.                     strCell7 = "";
  81.                     strCell8 = "";
  82.                     strCell9 = "";
  83.                     strCell10= "";
  84.  
  85.                     intRecCnt += 1;
  86.                 }
  87.  
  88.             }
  89.             catch (Exception Ex)
  90.             {
  91.                 MessageBox.Show(Ex.Message);
  92.  
  93.             }
  94.             finally
  95.             {
  96.                 this.dataGridView1.ClearSelection();
  97.                 this.toolStripStatusLabel1.Text = "Total Rows from Excel are " + Convert.ToString(intRecCnt);
  98.                 MessageBox.Show("Excel Spreadsheet import Completed", "Import Completed");
  99.                 objBook.Close();
  100.                 objExcel.Quit();
  101.             }
  102.  
  103.         }
  104.  

Thank You.

Have a Good Day.
cheers,
Lennie
Jun 11 '10 #1
4 4526
ThatThatGuy
449 Expert 256MB
@lenniekuah
What's the error you're facing
Jun 11 '10 #2
lenniekuah
126 100+
Hi ThatThatGuy,
Here are the error message caused by the coding.

objBook.Open = ExcelPathName;

Error 1 The event 'Excel.WorkbookEvents_Event.Open' can only appear on the left hand side of += or -=

---------------------------------------------------------

this.dataGridView1.Rows.Add(new string()(strCell1, strCell2, strCell3, strCell4, strCell5, strCell6, strCell7, strCell8, strCell9, strCell10));

Error 2 'string' does not contain a constructor that takes '0' arguments

---------------------------------------------------------
objBook.Close();
Error 3 No overload for method 'Close' takes '0' arguments
Jun 11 '10 #3
ThatThatGuy
449 Expert 256MB
@lenniekuah
Error1 Ans:
Since 'Excel.WorkbookEvents_Event.Open' being an event... this has to be on the left side of += or -= so that event delegates can be assigned to them....

Error 2 Ans:
this.dataGridView1.Rows.Add(new string()(strCell1, strCell2, strCell3, strCell4, strCell5, strCell6, strCell7, strCell8, strCell9, strCell10));
The Array symbol you specified is valid for VB.Net Code..

use string[] for C#
Jun 11 '10 #4
lenniekuah
126 100+
Hi AhNaa ThatThatGuy,
Thank you for your suggestion. I will try it out and get back to you. I like you to be my friend very much.

Hope you don't mind share your personal email with me. I was formerlly from Malaysia and my Indian name is Saravana.

Have a Good Day.

Cheers.
Lennie
Jun 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rachel | last post by:
Hi All: I would like to know how to prevent a dataset from being filled with garbage data. Forinstance, if I am importing data from an excel spreadsheet into a dataadapter and the first...
1
by: Gonzosez | last post by:
How can I extract data from an excel spread sheet? I know the sheet name but the range varies.
7
by: Greg P | last post by:
I'm new to VS2005 and want to simply update my data with the dataGridView that was generated when I draged my query from the Data Sources Pane. I think I may need to create a Data Adaptor but I'm...
4
by: Anthoni | last post by:
Dear All, I need to export data of unbound datagridview to XML file. Since my DataSource is NULL, What other options do I have? Does i need to make 2 loops (on all the records and inside loop of...
1
by: fly2irfan | last post by:
Hi All, I am new to IT/Developer Network I have to create an application which has to Extract data from Excel Spreadsheet using C# or VB.net then save the data into SQL database. Can anybody...
1
by: manishabh77 | last post by:
I will be obliged if anybody can help me with this problem: I am trying to extract data from an excel sheet that matches IDs given in column 4 of the excel sheet.I have stored those query IDs in an...
1
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, is it possible to extract data from Pdf file, in several formats, like .txt or Excel. And from an aspx page (ASP.NET 2.0 - C#). Thanks in advance. -- Luigi
1
by: veer | last post by:
Hi i am making a program in which i want to extract data from html file . Actually there are two dates on html file i want to extract these dates but the main probleum is that these dates are...
3
by: lenniekuah | last post by:
I have been asked to extract data from XML File using NODE NAME. I am new to XML and I do not know how to identify or understand what is NODE NAME. This is the XML File content, Please show me...
0
by: lenniekuah | last post by:
Hi Friends, I need your help. Please help me. I am trying to retrieve data from Excel Spreadsheet to fill the DataGridView for display prior to updating SQL SERVER with individual DataGridView...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.