49688

Export to Excel using Vb.net + Windows app

Question:

<strong>Simple code :</strong>

Private Sub ExportGridToExcel() Dim Excel As Object = CreateObject("Excel.Application") If Excel Is Nothing Then MsgBox("It appears that Excel is not installed on this machine. This operation requires MS Excel to be installed on this machine.", MsgBoxStyle.Critical) Return End If 'Make Excel visible Excel.Visible = True 'Initialize Excel Sheet With Excel .SheetsInNewWorkbook = 1 .Workbooks.Add() .Worksheets(1).Select() 'How to bind entire grid into excel without looping... End With Excel.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel) Excel = Nothing MsgBox("Export to Excel Complete", MsgBoxStyle.Information) End Sub

How to bind entire grid in to excel without looping..

Answer1:

I assume by the grid you mean a datagridview? I don't think you will be able to bind your data to excel. I wrote this some time ago to export a datagridview to excel:

private _dt as new Datatable Dim strTempFile As String = My.Computer.FileSystem.GetTempFileName() _dt = dgvResults.DataSource Dim strLine As New StringBuilder("") For c As Integer = 0 To _dt.Columns.Count - 1 strLine.Append(_dt.Columns(c).ColumnName.ToString & ",") Next My.Computer.FileSystem.WriteAllText(strTempFile, strLine.ToString.TrimEnd(",") & vbCrLf, True) For r As Integer = 0 To _dt.Rows.Count - 1 strLine = New StringBuilder("") For c As Integer = 0 To _dt.Columns.Count - 1 strLine.Append(_dt.Rows(r).Item(c).ToString & ",") Next My.Computer.FileSystem.WriteAllText(strTempFile, strLine.ToString.TrimEnd(",") & vbCrLf, True) Next Process.Start("excel", strTempFile)

Recommend

  • How do I merge multiple excel files to a single excel file
  • I want to add only one sheet after creating an Excel workbook through C#
  • remove non-ASCII characters (using Microsoft.Office.Interop.Excel)
  • MATCH() function in VBA
  • IronPython - Run an Excel Macro
  • Unable to delete a worksheet using EPPlus
  • Excel Range in C# using interop.Excel. Range both empty and not empty?
  • Copy and Paste Entire Row
  • Removing levels from data frame read from CSV file - R
  • Using VBA to select a dynamic range of cells and create a chart
  • Insert Columns between columns in excel
  • Get the Id/name of an element from dynamically generated HTML JQuery
  • LINQ to Entities does not recognize the method 'System.String ToString()' method, and this
  • Selenium - How to skip the record from the Set after the its first occurrence
  • WPF Animation doesn't run first time
  • Browserify and Reactify source maps include full local path names
  • ONLY Visible Div form elements will get submitted
  • How to know if a JFrame is on screen in a multi screen environment
  • Fixed Background Works in Chrome but Not Firefox?
  • Using an STL Iterator without initialising it
  • Accessing another variable in the same class with a click event
  • Responsive Form on top of Responsive Image? - Bootstrap
  • Unix Network Programming Clarification
  • Separate ID and Class for JS and CSS
  • Feature detection of foreignObject in SVG
  • Why does the font in these TD elements render at different sizes?
  • Google map in top corner left
  • Why can't UI components be accessed from a backgroundworker?
  • garbled css name when styling within UiBinder
  • How to open html table in xls on click of a button
  • How to getText() from the input field of an angularjs Application
  • Android activity accessing service's static reference before the service is ready
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Read a local file using javascript
  • Unanticipated behavior
  • Data Validation Drop Down Box Arrow Disappearing
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • UserPrincipal.Current returns apppool on IIS