Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Excel Tutorial
Home
Ask a Question
Jobs
Projects
Forum
Submit Tutorial
Latest Tutorials
Android | ASP | C | C++ | C# | Cocoa | Java | JavaScript | JSP | J2ME | JSF | JDBC| Struts | HTML5 | PHP | Perl | Python | Ruby | VB.net | T .net rends | T estpapers
Jump T o
Program ing Language m
T utorial Archive: Android AJAX ASP C C++ C# .net By: Andrei - Where should I call the a Cocoa Cloud Com puting EJB Java Certification By: Amit Agarwal - this code is running succ Interview iPhone By: Chandra Sekhar Bodireddy - The coding which is givenJavascript JSF JSP Java Beans J2ME JDBC Linux By: avinash - It's good working very we Mac OS X MySQL Perl PHP Python Ruby SAP VB.net By: JUST_PREET - not bad EJB Struts T rends XML Office 365 Hibernate By: Alfa - Hi there, a nice tutorial Latest T utorials on: Android By: N - As C does not support Fun AJAX ASP C Cocoa C++ .net C# EJB Java Certification By: Rahul - too good post....very eas Interview iPhone Javascript JSF JSP Java By: Janani - very Good machi....... Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net For exporting data from Datagridview to Excel, connect the database and load data from the database EJB Struts Cloud to Datagridview and then create a new excel file and write the data from Datagridview to Excel file. Com puting XML Office 365 Hibernate
Latest comments
Related Links
First step is to Load the Product table data to DataGridView, and create new Excel file and write the data from Datagridview to Excel file.
Imports System.Data Imports System.Data.SqlClient Imports Excel = Microsoft.Office.Interop.Excel Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim cnn As SqlConnection Dim connectionString As String Dim sql As String
converted by Web2PDFConvert.com
connectionString = "data source=servername;" & _ "initial catalog=databasename;user id=username;password=password;" cnn = New SqlConnection(connectionString) cnn.Open() sql = "SELECT * FROM Product" Dim dscmd As New SqlDataAdapter(sql, cnn) Dim ds As New DataSet dscmd.Fill(ds) DataGridView1.DataSource = ds.Tables(0) cnn.Close() End Sub
F acebook social plugin
Dim xlApp As Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim i As Integer Dim j As Integer xlApp = New Excel.ApplicationClass xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets("sheet1") For i = 0 To DataGridView1.RowCount - 2 For j = 0 To DataGridView1.ColumnCount - 1 xlWorkSheet.Cells(i + 1, j + 1) = _ DataGridView1(j, i).Value.ToString() Next Next xlWorkSheet.SaveAs("C:\vbexcel.xlsx") xlWorkBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorkSheet) MsgBox("You can find the file C:\vbexcel.xlsx") End Sub Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub End Class
Excel Tutorials
www.lynda.com/Excel Online video tutorials for Excel. Free Trial.
your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.
Excel Consulting
www.ExcelCorporateSolutions.com Automation-Macros Add-Ins Custom Functions
Ads by Google
Bookkeeping Schools
Colleges.CampusCorner.com Search Affordable and Flexible Bookkeeping Degree Programs.
Comments(14)
1. View Comment
It's very helpful to me... Thanks...
2.
Is there anyway the datagridview can export the column name too? Douglas
3.
Dim xlApp As Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value xlApp = New Excel.ApplicationClass xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets("Feuil1") For Each col As DataGridViewColumn In DG_ENCOURS.Columns 'noms des colonnes xlWorkSheet.Cells(1, col.Index + 1) = col.HeaderText For Each rowa As DataGridViewRow In DG_ENCOURS.Rows
converted by Web2PDFConvert.com
4.
5.
6.
7.
Hello all.. can anyone please help me.. i just got this error.. xlWorkBook = xlApp.Workbooks.Add(misValue) >> Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
8.
I have use the above code, Am getting the below error Unable to cast COM Object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' of Interface type 'Microsoft.Office.Interop.Excel._Application'. This Operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000C000-000000000046}' failed due to the following error : could not be found. (Exception from HRESULT: 0x80030002(STG_E_FILENOTFOUND)). Kindly anyone give me the solution
converted by Web2PDFConvert.com
9.
10.
while exporting data i am getting error on below statement saying "Null Reference Exception was unhandled" xlWorkSheet.Cells(i + 1, j + 1) =_ DataGridView1(j,i).Value.ToString() help me outta this.......
11.
Thanks for your code. Nice working but I have one problem. I have time in DataGridview , In that condition it is not working. Without time it is working fine. Plz suggest me. I have urgent required. Thanks
12.
Thank you... I have the same problem , I have date and time in my datagridview, could not exported and display in excel. Only shows ########. tnx
13.
I have a problem , i cant see the Microsoft.Office.Interop. in add reference -> .net -> .. i am using Visual Basic Express Edition (2008) .. there is no "Microsoft.Office.Interop.Excel" there , dont know what to do to be able to use that code for my project.. please help.. Thanks.
14.
I am using VS 2010 Express and MS Excel 2007 wherein I am trying to export data from the DataGridView into an Excel file.
converted by Web2PDFConvert.com
xlWorkSheet.SaveAs( C:\ExportTagDetails.xlsx ) the above line of code works for me. But if I again try to create a file, it obviously shows the Message : A file named C:\ExportTagDetails.xlsx already exists in this location. Do you want to replace it? When I press Yes it creates no problem and calmly replaces itself in place of the old file. But if I click on No or Cancel then it throws an exception stating : System.Runtime.Interop.Services.COMException(0x800A03EC): Exception from HRESULT: 0x800A03EC Here is the entire block of code : Private Sub cmd_export_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_export.Click Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim i As Integer Dim j As Integer Dim row As Long = 1 xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets( Sheet1 ) xlWorkSheet.Cells(1, 1).Font.Bold = True xlWorkSheet.Cells(1, 1).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red) xlWorkSheet.Cells(1, 2).Font.Bold = True xlWorkSheet.Cells(1, 2).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red) xlWorkSheet.Columns(1).ColumnWidth = 45 xlWorkSheet.Columns(2).ColumnWidth = 30 xlWorkSheet.Range( A & row).Value = Tag Name xlWorkSheet.Range( B & row).Value = Start Value
row = row + 1 Try For i = 0 To DataGridView1.RowCount 2 For j = 0 To DataGridView1.ColumnCount 1 xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString() Next Next xlWorkSheet.SaveAs( C:\ExportTagDetails.xlsx ) Catch ex As Exception MsgBox(ex.ToString) End Try xlWorkBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlWorkBook)
converted by Web2PDFConvert.com
releaseObject(xlWorkSheet) MsgBox( You can find the file at C:\ExportTagDetails.xlsx ) End Sub Is there any way I can handle the issue. Or can you provide me with a solution where the user can himself enter the name of the file and also specify the location. Thanks in advance.
View Tutorial
Your name (required):
http://www .java-samples.com
Your comments:
converted by Web2PDFConvert.com
How to create an XML file in VB.NET using Dataset Multi Threaded Client Socket Programming in VB.net Multi Threaded Server Socket Programming in VB.net Multi threaded Socket Programming in VB.net Client Socket Program sample in VB.net
Understanding Scope in VB.net Using Resume Next and Resume Line in VB.net Using On Error GoTo 0 in VB.net Getting an Exception's Number and Description in VB.net Raising an Exception Intentionally in VB.net
2013 Java-samples.com T utorialArchive: Android AJAX ASP C Cloud Com .net puting Cocoa C++ C# EJB Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby VB.net Struts T Papers T est rends SAP XML Office 365 Hibernate Latest T utorials on: Android AJAX ASP C Cocoa Cloud Com .net puting C++ C# EJB Java Certification Interview iPhone JavaScript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby VB.net SAP Struts T rends XML Office 365 Hibernate
converted by Web2PDFConvert.com