export mysql table data with column header using vb.net - excel

i want to export mysql table data with column heading to excel sheet. am using vb.net. but am not able to get table column heading in excel sheet. am getting only mysql table data.
my code
-------
Dim i, j As Integer
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("sheet1")
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
ds.Tables(0).Rows(i).Item(j)
Next
Next
xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()

I don't know Vb.net, but I had a similar issue and had to use the syntax:
IGNORE 1 LINES in the statement.
http://dev.mysql.com/doc/refman/5.1/en/load-data.html (this is for importing, but the IGNORE 1 LINES syntax is described here...)

Related

Add an Excel Form Control to a file in vb.net

I've got an application written that does a pretty standard dump of a datagridview into an Excel file. The application loops through 12 items and creates a separate sheet in the workbook, then filling in the table using the datagridview.
The export is all good, but what I want to do now is add to every every sheet an additional column that contains a check box on every row in that sheet. It seems the better way to do that is add the form control before the Excel file is saved in the code, but I can't work out if that is possible - Adding a checkbox column to the datagridview doesn't seem to be a viable option.
Code - the dtResults is a datatable filled by a Microsoft SQL query.
Private Sub btnProcess_Click(sender As Object, e As EventArgs) Handles btnProcess.Click
Dim TestsDT As New DataTable
TestsDT.Columns.Add("SvcAbbrev")
TestsDT.Rows.Add("PH.")
TestsDT.Rows.Add("LCFA")
TestsDT.Rows.Add("FAECALP")
TestsDT.Rows.Add("PE-1")
TestsDT.Rows.Add("BGLUC")
TestsDT.Rows.Add("SIGAF")
TestsDT.Rows.Add("CALP")
TestsDT.Rows.Add("HELPFAE")
TestsDT.Rows.Add("M2-PK")
TestsDT.Rows.Add("FAEZONULIN")
TestsDT.Rows.Add("FAEFAESIGA")
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim z As Integer
Dim x As Integer
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
For l = 0 To TestsDT.Rows.Count - 1
txtTest.Text = TestsDT.Rows(l).Item("SvcAbbrev")
dgResults.DataSource = GetResults()
Dim Count As Integer = 0
Count = dgResults.Rows.Count - 1
If Count < 1 Then
Else
Dim ChkBox As New DataGridViewCheckBoxColumn()
ChkBox.HeaderText = "Checkbox"
ChkBox.Name = "Check"
dgResults.Columns.Insert(4, ChkBox)
xlWorkSheet = xlWorkBook.Sheets.Add()
xlWorkSheet.Name = txtTest.Text
xlWorkSheet.Columns.AutoFit()
For z = 0 To dgResults.RowCount - 2
For x = 0 To dgResults.ColumnCount - 1
For y As Integer = 1 To dgResults.Columns.Count
xlWorkSheet.Cells(1, y) = dgResults.Columns(y - 1).HeaderText
xlWorkSheet.Cells(z + 2, x + 1) = dgResults(x, z).Value.ToString()
Next
Next
Next
End If
Next
xlWorkBook.Sheets("Sheet1").Delete
xlWorkSheet.SaveAs("C:\Users\brettf\desktop\test-output.xlsx")
xlWorkBook.Close()
xlApp.Quit()
MessageBox.Show("Processing complete. Excel file should open automatically.", "Processing Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
Process.Start("C:\Users\brettf\desktop\test-output.xlsx")
Current Output:
Desired Output:
The checkbox has no bearing to anything in the datagridview and will be used once the document is printed by another group of staff within the business.

Export data from listview to excel vb.net

I export data from listview to excel. With this code below, it exports all the data I want to export. The problem shows when I export to excel next time. Exported data is added to previously created excel.
The code I have used is below:
Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
Dim objExcel As New Excel.Application
Dim bkWorkBook As Excel.Workbook
Dim shWorkSheet As Excel.Worksheet
Dim i As Integer
Dim j As Integer
objExcel = New Excel.Application
bkWorkBook = objExcel.Workbooks.Add
shWorkSheet = CType(bkWorkBook.ActiveSheet, Excel.Worksheet)
For i = 0 To Me.ListView3.Columns.Count - 1
shWorkSheet.Cells(1, i + 1) = Me.ListView3.Columns(i).Text
Next
For i = 0 To Me.ListView3.Items.Count - 1
For j = 0 To Me.ListView3.Items(i).SubItems.Count - 1
shWorkSheet.Cells(i + 2, j + 1) = Me.ListView3.Items(i).SubItems(j).Text
Next
objExcel.Cells.Select()
objExcel.Cells.EntireColumn.AutoFit()
objExcel.Cells.EntireRow.AutoFit()
objExcel.Cells.Range("A1").Select()
Next
bkWorkBook.SaveAs("C:\PDF\Naročilo " + TxtPodjetje.Text + " " + DT_DatumNaročila.Text + ".xlsx")
bkWorkBook.Close()
objExcel.Quit()
If I close created vb.net app after the first export and restart it works properly and I get data as I want.
Any help would be really appreciated!

Extract Powerpoint chart label to Excel using VBA

I need to find a way to extract chart data labels from a PowerPoint chart to Excel, as many times the PowerPoint chart given to me has it's linked data broken.
I wrote the code below, but I have no clue what to do after For Each datapoint In chtnow.SeriesCollection(1).Points...
Sub Extract_Datalabels()
'Goal: To extract datalabels of Chart's series collection and write to excel
Dim datapoint As Point
Dim sh As Shape
Dim sld As Slide
Dim chtnow As Chart
Dim label As DataLabel
Dim xlApp As New Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlworksheet As Excel.Worksheet
Set xlWorkbook = xlApp.Workbooks.Add
Set xlworksheet = xlWorksheets.Add
xlApp.Visible = True
Set chtnow = ActiveWindow.Selection.ShapeRange(1).Chart
For Each datapoint In chtnow.SeriesCollection(1).Points
'Extract data labels
If datapoint.HasDataLabel Then
[No clue how to write to Excel]
End If
Next
End Sub
If everything else works ok with the code, this is an easy way to write to the first column of the xlworksheet in excel:
Dim cnt As Long
If datapoint.HasDataLabel Then
cnt = cnt + 1
xlworksheet.Cells(cnt, 1) = datapoint.label
End If
However, I am not sure that after setting xlApp.Visible = True you would be allowed to do something like this Set chtnow = ActiveWindow.Selection.ShapeRange(1).Chart.
You had a couple of type errors with your example, but this should get the job done for you. You will need to add a reference to the Microsoft Excel [A Number] Object Library in order to use the Excel object type and all derivatives.
All testing was done using a bar chart.
Sub Extract_Datalabels()
''Goal: To extract datalabels of Chart's series collection and write to excel
Dim datapoint As ChartPoint
Dim chtnow As Chart
Dim xlApp As New Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlworksheet As Excel.Worksheet
Dim Row As Long
Let xlApp.SheetsInNewWorkbook = 1
Set xlWorkbook = xlApp.Workbooks.Add
Set xlworksheet = xlWorkbook.Worksheets(1)
Let xlApp.Visible = True
Call VBA.DoEvents
Set chtnow = ActiveWindow.Selection.ShapeRange(1).Chart
Let Row = 1
For Each datapoint In chtnow.SeriesCollection(1).Points
'Extract data labels
If datapoint.HasDataLabel Then
Let xlworksheet.Cells(Row, 1) = datapoint.DataLabel.Text
End If
Let Row = Row + 1
Next
End Sub

Excel file updated but no change: Export query to excel without header

My VBA code is supposed to transfer my query in MS Access (without header)
to cell K17 on an existing excel spreadsheet named Totals.
When I run the module, under the property of the excel file, last modified time did get updated to when I run the module. But I don't see my query on the excel spreadsheet.
Any advice on my code will be highly appreciated. Thank you so much!
Sub TransToXL()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim acRng As Variant
Dim xlRow As Integer
Dim qry As QueryDef
Dim rst As Recordset
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("C:\Users\MyName\Desktop\August 2017.xlsx")
Set xlWS = xlWB.Worksheets("Totals")
xlRow = (xlWS.Columns("K").End(xlDown).Row)
Set qry = CurrentDb.QueryDefs("DollarsSold")
Set rst = qry.OpenRecordset
Dim c As Integer
c = 11
xlRow = xlRow + 16
Do Until rst.EOF
For Each acRng In rst.Fields
xlWS.Cells(xlRow, c).Formula = acRng
c = c + 1
Next acRng
xlRow = xlRow + 1
c = 1
rst.MoveNext
If xlRow > 25 Then GoTo rq_Exit
Loop
rq_Exit:
rst.Close
Set rst = Nothing
Set xlWS = Nothing
xlWB.Close acSaveYes
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Sub
End Sub
The variable c is the one that stores colum number, in which c =1 means column A, 11 will be K. In this code, I'm confused about the way I set xlRow+16. xlRow+16 , and c = 11 is not pointing out to K17. Any advise on this? Thank you!
Is there a reason why you are using a Do Until loop? If the recordset only has 1 row of data (the total) it looks like you could remove the loop and just use something like:
xlWS.Range("K17").Value = rst.Fields(0)
This would appear after the "Set rst.." line.

Exporting to Excel - Make DateTime columns into Date

I've got an Export to Excel function in my application.
There are no problems with the program itself, but when exporting data, it shows DateTime values as dd/MM/yyyy 00:00:00, which, when displaying them on a DataGridView before importing, shows empty cells in some places.
I need to adapt my export code to remove the time portion of the data, and just display the date. When I highlight the cell in Excel, I am formatting it to be 'Date' of format dd/MM/yyyy.
How can I do this programatically?
my code
Dim xlapp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Dim misvalue As Object = Reflection.Missing.Value
xlapp = New Excel.Application
xlWorkbook = xlapp.Workbooks.Add(misvalue)
xlWorksheet = xlWorkbook.Sheets.Add
xlWorksheet.Name = "SupplierInformation"
xlWorksheet.Cells.NumberFormat = "#"
For k As Integer = 1 To dgvExport.Columns.Count
xlWorksheet.Cells(1, k) = dgvExport.Columns(k - 1).HeaderText
Next
For i = 0 To dgvExport.RowCount - 1
For j = 0 To dgvExport.ColumnCount - 1
xlWorksheet.Cells(i + 2, j + 1) = dgvExport(j, i).Value.ToString
Next
Next
xlWorksheet.Columns.AutoFit()
Declare a variable that will be used for referring to a range, eg;
Dim eRange as Excel.Range
Then, set the range in your Worksheet
eRange = xlWorksheet.Range("A1", B5")
Then set the format, using .NumberFormat
eRange.NumberFormat = "dd/MM/yyyy"
FYI: This link contains pretty much all of the information you could ever need on how to format Excel data and cells using VB

Resources