Hi all, I have two question,First:Is how will data export to excel in datagridview.Second: Is how will data import to database in excel. Thanks in advance.
private void btnExportToExcel_Click(object sender, EventArgs e) {
// Create DataTable for Excel file.
var dataTable = ((DataTable)dataGridView1.DataSource).Copy(); // Remove rows that do not meat the condition.
for (int i = 0; i < dataTable.Rows.Count; i++) if (Convert.ToBoolean(dataTable.Rows[i][0]) == false) dataTable.Rows.Remove(dataTable.Rows[i]); // Remove unwanted column.
dataTable.Columns.Remove(dataTable.Columns[0]); // Create new excel file.
var excelFile = new ExcelFile(); // Create new excel worksheet.
var excelSheet = excelFile.Worksheets.Add(dataTable.TableName); // Insert data from dataTable to the worksheet starting at cell "A1".
excelSheet.InsertDataTable(dataTable, new InsertDataTableOptions("A1") { ColumnHeaders = true }); // Save as XLSX file.
excelFile.Save("New File.xlsx"); }
private void btnImportExcelToDB_Click(object sender, EventArgs e) { var dataSet = new DataSet(); // Load excel file.
var excelFile = ExcelFile.Load("New File.xlsx"); var excelWorksheet = excelFile.Worksheets.ActiveWorksheet; // Extract the data from the worksheet to newly created DataTable starting at
// first row and first column until the first empty row appears.
var dataTable = excelWorksheet.CreateDataTable(new CreateDataTableOptions() { StartRow = 0, StartColumn = 0, ExtractDataOptions = ExtractDataOptions.StopAtFirstEmptyRow }); dataTable.TableName = excelWorksheet.Name; // Add DataTable to DataSet.
dataSet.Tables.Add(dataTable); // TODO import dataSet to Database
}
Ely Sanders
22-Aug-2013Hi, here is an alternative and for me a more flexible and simplier way you can try to export DataGridView to Excel file in C#:
Also here is how you can import Excel file to a DataTable in C#:
I used this C# Excel component.
Anonymous User
03-Oct-2011You can use following link to export data from excel to data-grid view,
http://support.microsoft.com/kb/321686
Thanks,
James Smith
01-Oct-2011This is really good.
Can any one give me some example of datagridview demo in c#. So i can make a complete program on it.
Thanks,
John Smith
27-Sep-2011Amit Singh
27-Sep-2011Kenny Tangnde
25-Sep-2011Hi all,
thanks Awadhendra Tiwari,It helps me a lot.
Thanks.
Anonymous User
24-Sep-2011Anonymous User
24-Sep-2011You can take a look at following link for learning linq
http://msdn.microsoft.com/en-us/vcsharp/aa336766
http://geekswithblogs.net/elroydsilva/archive/2008/11/03/learning-linq---an-overview.aspx
http://www.linqlearning.com/sp/16295-LINQ-to-SQL-Tutorials-Learning-Resources/
Thanks.
Kenny Tangnde
23-Sep-2011hi Awadhendra Tiwari,Rohit Kesharwani,
Thank you for your help,Thanks Rohit Kesharwani . he helps me a lot.
again ask a question:
you can provide LINQ learning resources to me,and LINQ in project the integrated application.
Grateful.
John Smith
23-Sep-2011Uttam Misra
23-Sep-2011using System.Data.SqlClient;
Anonymous User
23-Sep-2011Anonymous User
23-Sep-2011You can make following modification on above code
using System.Data.SqlClient; //import this namespace at top
then replace OleDbCommand by SqlCommand class.
Then above code also work for you.
Thanks.
Kenny Tangnde
22-Sep-2011hi Rohit Kesharwani,
i not used using System.Data.OleDb; i want used using System.Data.SqlClient;
Thank you always help me, I hereby express my sincere thank you.
Chris Anderson
22-Sep-2011hi,
If you want that only checked rows will be exported then simply manipulate the above code:
Thanks.
Kenny Tangnde
22-Sep-2011hi Rohit Kesharwani,
Chris Anderson
22-Sep-2011Here are the solution of your both the problem:
Export Data from Excel in dataGridView:
Write the below code on the button click or on form load function:
Import Data from dataGridView in Excel:
Happy Coding.