This code will read data from excelsheet in gridview......take a submit button and take a datagrid....and use below code...its works fine.... - private void btnsubmit_Click(object sender, EventArgs e)
-
{
-
-
Excel.Application appExl;
-
Excel.Workbook workbook;
-
Excel.Worksheet NwSheet;
-
Excel.Range ShtRange;
-
appExl = new Excel.ApplicationClass();
-
workbook = appExl.Workbooks.Open(("C:\\data.xlsx"), Missing.Value, Missing.Value,
-
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
-
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
-
NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
-
-
int Cnum = 0;
-
int Rnum = 0;
-
-
ShtRange = NwSheet.UsedRange;
-
DataTable dt = new DataTable();
-
dt.Columns.Add("name");
-
dt.Columns.Add("address");
-
// dt.Columns.Add("Status");
-
dt.Columns.Add("Phone");
-
for (Rnum = 1; Rnum <= ShtRange.Rows.Count; Rnum++)
-
{
-
DataRow dr = dt.NewRow();
-
for (Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
-
{
-
dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
-
}
-
dt.Rows.Add(dr);
-
dt.AcceptChanges();
-
}
-
workbook.Close(true, Missing.Value, Missing.Value);
-
appExl.Quit();
-
-
-
//Session["data"] = dt;
-
dataGridView1 .DataSource = dt;
-
//dataGridView1.DataBind();
-
-
}
3 17070
hi buddy.. yours code works good.. U r awesome.. But i need to know one more thing.. pls help.. how to display contents of different sheets in excel file.. for example my excel file contains sheet1,sheet2,sheet3.. i need to display contents of any sheet i want by selecting the desired sheet.
This code can select a sheet.
Set two buttons, one Combobox and one DatagridView on the form.
Click Button1 to set the sheet name of the excel file in the Combobox1.
Select a sheet from the combobox1 and click Button2, and the values of the selected sheet will be displayed in the DatagridView.
I used Spire.XLS for .NET to expert data from Excel sheet to DataGridView, and it worked like a charm.
Compared with Microsoft.Office.Interop.Excel, the biggest advantage of Spire.XLS is that there is no need to installed MS Office on computers. -
//Create a new workbook
-
Workbook workbook = new Workbook();
-
//Load an excel file
-
workbook.LoadFromFile(@”C:\Users\Administrator\Desktop\data.xlsx”);
-
//Get the first worksheet
-
Worksheet sheet = workbook.Worksheets[0];
-
//Export data from excel to datagridview
-
this.dataGridView1.DataSource = sheet.ExportDataTable();
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dariusz |
last post by:
Below is part of a code I have for a database. While the database table is
created correctly (if it doesn't exist), and data is input correctly into
the database when executed, I have a problem...
|
by: Andy |
last post by:
Hi,
In the code below (not pretty I know but it's an early version :-P) I'm
having problems reading the data object back in. If I move the reading
code to immediately after the section where it...
|
by: Magnus |
last post by:
allrite folks, got some questions here...
1) LAY-OUT OF REPORTS
How is it possible to fundamentaly change the lay-out/form of a report in
access? I dont really know it that "difficult", but...
|
by: abc my vclass |
last post by:
There are some programs written on .NET 1.1. These applications are apply
n-tiers contains Data Access Layers or Business Logic Layer. Now, our
company upgrade to .NET 2.0 and enhance or rewrite...
|
by: ashik478 |
last post by:
Hi! I have a problem. I have a table in oracle database with one field is primary key. Now I want to insert data from Datagridview with hide column which contain not null field(this field is...
|
by: wassssup |
last post by:
hi guys..im new to datagridview and im wondering is there a way to convert an encrypted data in access and show it in a datagridview? im using C# and microsoft access
normally this is how to show...
|
by: shripal |
last post by:
hello,
i am new to c#.net.
i have imported data in datagridview form ms access database.
now i want to store each row of datagridview saperatly in string array
so in saperate class i...
|
by: progvar |
last post by:
Hi
i want to update data in database using datagridview
actually i am displaying the data in datagridview and after that i want to update some rows data directly modifying the displayed data into...
|
by: premMS143 |
last post by:
Hi,
I have created application using VB.net & backend MS Access. During run time I add certain rows of data to Datagridview. After finalizing it I want to update those data present in datagridview...
|
by: Arnold Agolli |
last post by:
Hello!
Can sb explain me how to connect to a .accdb database and on Form_Load to display its data in DataGridView!(i want this in odbc because in oledb it didnt work because im using 64-bit windows...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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: 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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |