本文介绍了格式化数据网格中的数据导出未格式化的方式创先争优的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要你的指导/帮助这个问题。我有格式化数据网格视图这种格式00-00-00-00-00-000数据。但是,当我导出这些数据的Excel工作表,此格式消失了,数据是在这个形式 - 0000000000000.的格式消失在Excel中。我已经写在vb.net。请提供我在这件事上的帮忙\解决方案。
I need your guidance/help in this question. I have data formatted in the data grid view in this format 00-00-00-00-00-000. But when i am exporting this data to excel sheet , this formatting is gone and data is in this form- 0000000000000. The formatting is gone in excel. I have written in vb.net. Please provide me help\solution in this matter.
推荐答案
您可以设置所需要的细胞沿东西线的风格:的NumberFormat
You could set the style of the required cells to something along the lines of:NumberFormat
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim ExcelApp As Excel.Application
Dim ExcelWorkBk As Excel.Workbook
Dim ExcelWorkSht As Excel.Worksheet
Public Function exportToExcel(ByVal dgv As DataGridView)
Try
exportToExcel = True
ExcelApp = New Excel.Application
ExcelWorkBk = ExcelApp.Workbooks.Add()
ExcelWorkSht = ExcelWorkBk.Sheets("Sheet1")
''// Rename the sheet
ExcelWorkSht.Name = "MySheet"
ExcelWorkSht.Tab.ThemeColor = Excel.XlThemeColor.xlThemeColorAccent1
Dim style As Excel.Style = ExcelWorkSht.Application.ActiveWorkbook.Styles.Add("StyleName")
style.Font.Bold = True
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen)
style.NumberFormat = format
Dim styleHeader As Excel.Style = ExcelWorkSht.Application.ActiveWorkbook.Styles.Add("Header")
styleHeader.Font.Bold = True
styleHeader.ShrinkToFit = True
styleHeader.Orientation = Excel.XlOrientation.xlHorizontal
styleHeader.VerticalAlignment = Excel.XlVAlign.xlVAlignJustify
styleHeader.WrapText = False
styleHeader.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.WhiteSmoke)
''// get all visible columns in display index order
Dim ColNames As List(Of String) = (From col As DataGridViewColumn _
In dgv.Columns.Cast(Of DataGridViewColumn)() _
Where (col.Visible = True) _
Order By col.DisplayIndex _
Select col.Name).ToList
Dim colcount = 0
For Each s In ColNames
colcount += 1
ExcelWorkSht.Cells(1, colcount) = dgv.Columns.Item(s).HeaderText
ExcelWorkSht.Cells(1, colcount).style = "Header"
Next
''// get the values and formatt them
For rowcount = 0 To dgv.Rows.Count - 1 ''// for each row
colcount = 0
For Each s In ColNames ''// for each column
colcount += 1
''xlWorkSheet.Cells(rowcount + 2, colcount) = dgv.Rows(rowcount).Cells(s).Value
ExcelWorkSht.Cells(rowcount + 2, colcount) = dgv.Rows(rowcount).Cells(s).FormattedValue
If colcount = 4 Then ''// current Column by it's Index
If dgv.Rows(rowcount).Cells(s).FormattedValue = 1 Then
''// formatt this column with the style 'StyleName'
ExcelWorkSht.Cells(rowcount + 2, colcount).Style = "StyleName"
End If
End If
Next
Next
ExcelWorkSht.Range("$D$1:$D$100").AutoFilter(Field:=1, Criteria1:="", Operator:=Excel.XlAutoFilterOperator.xlFilterAutomaticFontColor)
MsgBox("Exported successfully to Excel")
ExcelApp.Visible = True
Catch ex As Exception
exportToExcel = False
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
End Function
这篇关于格式化数据网格中的数据导出未格式化的方式创先争优的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!