First, I use an OpenFileDialog to import an excel file into a datagrid...
Then I format some of the cells based on certain variables:
Private Sub customerDataGridView_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles customerDataGridView.CellFormatting If Me.customerDataGridView.Columns(e.ColumnIndex).Name = "COLUMN TITLE" Then If e.Value IsNot Nothing Then If CDbl(e.Value.ToString) > CDbl(My.Settings.FDr2LowValue) And CDbl(e.Value.ToString) < CDbl(My.Settings.FDr2HighValue) Then e.CellStyle.BackColor = My.Settings.FDRcolor End If End If End If End Sub
then here is the code I use to export to excel, but the formatting does not stick during the export...
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New Excel.Application
Try
Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
xlApp.Visible = True
rowsTotal = CShort(customerDataGridView.RowCount - 1)
colsTotal = CShort(customerDataGridView.Columns.Count - 1)
With excelWorksheet
.Cells.Select()
.Cells.Delete()
For iC = 0 To colsTotal
.Cells(1, iC + 1).Value = customerDataGridView.Columns(iC).HeaderText
Next
For I = 0 To rowsTotal
For j = 0 To colsTotal
.Cells(I + 2, j + 1).value = customerDataGridView.Rows(I).Cells(j).Value
Next j
Next I
.Rows("1:1").Font.FontStyle = "Bold"
.Rows("1:1").Font.Size = 10
.Cells.Columns.AutoFit()
.Cells.Select()
.Cells.EntireColumn.AutoFit()
.Cells(1, 1).Select()
End With
Catch ex As Exception
MsgBox("ex.tostring")
Finally
'RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
xlApp = Nothing
End Try


Sign In
Create Account

Back to top









