![]() System.IO.Directory.CreateDirectory(TempDirectory)ĭim file As String = System.IO.Path.GetRandomFileName & ".csv" You can use the code after the image for CSV export.ĬSV Method Private Sub DataGridToCSV(ByRef dt As DataGridView, Qualifier As String)ĭim TempDirectory As String = "A temp Directory" Writing to Excel with Excel Automation is only useful if you need to format the data and cells otherwise you should use CSV. I instead create a CSV from the DataGridView. I actually don't use this method in my Database program because it's a slow method when you have a lot of rows/columns. Return "WriteDataTable()" & Environment.NewLine & Environment.NewLine & ex.Message Public Function WriteDataTable(SheetName As String, ByRef dt As DataTable) As String Return "WriteDataGrid()" & Environment.NewLine & Environment.NewLine & ex.Message Public Function WriteDataGrid(SheetName As String, ByRef dt As DataGridView) As Stringĭim l(dt.Rows.Count + 1, dt.Columns.Count) As Objectįor c As Integer = 0 To dt.Columns.Count - 1ĭim errors As String = WriteArray(SheetName, l) Return "WriteArray()" & Environment.NewLine & Environment.NewLine & ex.Message Range = ws.Range("A1").Resize(1, ObjectArray.GetLength(1) - 1) Public Function WriteArray(Sheet As String, ByRef ObjectArray As Object(,)) As Stringĭim xl As Excel.Application = New Excel.Applicationĭim wb As Excel.Workbook = xl.Workbooks.Add()ĭim ws As Excel.Worksheet = wb.Worksheets.Add()ĭim range As Excel.Range = ws.Range("A1").Resize(ObjectArray.GetLength(0), ObjectArray.GetLength(1)) Probably makes sense to add this as a parameter. In the WriteArray method you'll want to change the line that saves the workbook to where you want to save it. ![]() ("The location and filename of your file") To open your file after exporting use this line In your button code add this and change the names to your controls. If you would like to use my library then use this link to download it and if you need help just let me know.Īfter copying the code to your solution you will use it like this. Normally, I create an instance of my class and call these methods. ![]() If you get errors just let me know and I'll correct them for you. I extracted this from my library and I think I changed it enough to work with this code only, but more minor tweaking might be necessary. This means I can write to Excel without a loop and retain data types. This method creates an object array from a DataTable or DataGridView and then writes the array to Excel. ![]() Others use a loop to write each cell and write the cells with text data type. This method is different than many you will see. ![]() Private Sub releaseObject(ByVal obj As Object) MsgBox("You can find the file D:\vbexcel.xlsx") XlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString() XlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText XlWorkSheet = xlWorkBook.Sheets("sheet1")įor j = 0 To DataGridView1.ColumnCount - 1įor k As Integer = 1 To System.EventArgs) Handles VIEW_Button.Clickĭim xlApp As .Applicationĭim xlWorkBook As .Workbookĭim xlWorkSheet As .Worksheetĭim misValue As Object = Then Add code given bellow to the Export button click event- Private Sub Export_Button_Click(ByVal sender As System.Object, ByVal e As Code below creates Excel File and saves it in D: driveįIRST ADD REFERRANCE (Microsoft office 12.0 object library ) to your project ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |