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: - using System;
-
using System.Collections;
-
using System.Collections.Generic;
-
using System.Configuration;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Data.OleDb;
-
using System.Data.SqlClient;
-
using System.IO;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using Microsoft.Office.Interop.Excel;
-
using Microsoft.Office.Core;
-
using System.Xml;
-
-
private void btnImportExcel_Click(object sender, EventArgs e)
-
{ // open up excel and fill datagridview
-
-
Excel.Application objExcel = new Excel.Application();
-
Excel.Workbook objBook = new Excel.Workbook();
-
objBook.Open(ExcelPathName);
-
Excel.Worksheet objSheet = new Excel.Worksheet();
-
objSheet.Activate;
-
objExcel.Visible= false;
-
-
//common variable
-
Boolean bolFlag = true;
-
int excelRow = 2;
-
int excelCol = 0;
-
int DGVRow = 1;
-
int intRecCnt = 0;
-
-
string strCell1 = "";
-
string strCell2 = "";
-
string strCell3 = "";
-
string strCell4 = "";
-
string strCell5 = "";
-
string strCell6 = "";
-
string strCell7 = "";
-
string strCell8 = "";
-
string strCell9 = "";
-
string strCell10= "";
-
-
try
-
{
-
while (bolFlag == true)
-
{
-
-
strCell1 = (string)objSheet.Cells[excelRow, 1];
-
strCell2 = (string)objSheet.Cells[excelRow, 2];
-
strCell3 = (string)objSheet.Cells[excelRow, 3];
-
strCell4 = (string)objSheet.Cells[excelRow, 4];
-
strCell5 = (string)objSheet.Cells[excelRow, 5];
-
strCell6 = (string)objSheet.Cells[excelRow, 6];
-
strCell7 = (string)objSheet.Cells[excelRow, 7];
-
strCell8 = (string)objSheet.Cells[excelRow, 8];
-
strCell9 = (string)objSheet.Cells[excelRow, 9];
-
strCell10 = (string)objSheet.Cells[excelRow, 10];
-
-
if ( strCell1 == "")
-
{
-
bolFlag = false;
-
break;
-
}
-
-
this.dataGridView1.Rows.Add(new string()( strCell1, strCell2, strCell3,strCell4,strCell5,strCell6,strCell7,strCell8,strCell9,strCell10)) ;
-
-
//DataGridView1.Rows.Add()
-
DGVRow += 1;
-
excelRow += 1;
-
-
//initialise variables
-
strCell1 = "";
-
strCell2 = "";
-
strCell3 = "";
-
strCell4 = "";
-
strCell5 = "";
-
strCell6 = "";
-
strCell7 = "";
-
strCell8 = "";
-
strCell9 = "";
-
strCell10= "";
-
-
intRecCnt += 1;
-
}
-
-
}
-
catch (Exception Ex)
-
{
-
MessageBox.Show(Ex.Message);
-
-
}
-
finally
-
{
-
this.dataGridView1.ClearSelection();
-
this.toolStripStatusLabel1.Text = "Total Rows from Excel are " + Convert.ToString(intRecCnt);
-
MessageBox.Show("Excel Spreadsheet import Completed", "Import Completed");
-
objBook.Close();
-
objExcel.Quit();
-
}
-
-
}
-
Thank You.
Have a Good Day.
cheers,
Lennie
4 4526
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
@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#
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Gonzosez |
last post by:
How can I extract data from an excel spread sheet?
I know the sheet name but the range varies.
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |