我有一个将DataGrid
转换为excel表的程序。它正在运行。但现在我的问题是,如果我的数据是" 763040059412"在Datagrid
中,在我的Excel工资单中进行转换后,显示为" 7.6304E + 11"。我不知道该怎么办..请帮帮我。谢谢你
答案 0 :(得分:0)
使用Excel自动设置范围的NumberFormat,如下所示,我在使用您显示的值。如果我在没有设置NumberFormat的情况下插入该值,结果与您获得的结果相同,但使用NumberFormat时,数字格式正确。
在这里,我正在使用可执行文件夹中的Excel文件,您将调整为指向现有或新创建的Excel文件。
Dim FileName As String = IO.Path.Combine(Application.StartupPath, "Formatting.xlsx")
Dim value As Long = 763040059412
OpenExcelDoFormat(FileName, "", "Sheet1", "G2", 763040059412)
支持代码,我通过process.start打开文件,立即查看结果。注意参数SaveFileName在这里没有任何意义,因为我调整了另一个我为另一个问题做的演示。
Option Strict On
Imports Excel = Microsoft.Office.Interop.Excel
Imports Microsoft.Office
Imports System.Runtime.InteropServices
Module OpenWorkSheets4
Public Sub OpenExcelDoFormat(
ByVal OpenFileName As String,
ByVal SaveFileName As String,
ByVal SheetName As String,
ByVal CellAddress As String,
ByVal NewCellValue As Long)
If IO.File.Exists(OpenFileName) Then
Dim Proceed As Boolean = False
Dim xlApp As Excel.Application = Nothing
Dim xlWorkBooks As Excel.Workbooks = Nothing
Dim xlWorkBook As Excel.Workbook = Nothing
Dim xlWorkSheet As Excel.Worksheet = Nothing
Dim xlWorkSheets As Excel.Sheets = Nothing
Dim xlCells As Excel.Range = Nothing
xlApp = New Excel.Application
xlApp.DisplayAlerts = False
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(OpenFileName)
xlApp.Visible = False
xlWorkSheets = xlWorkBook.Sheets
For x As Integer = 1 To xlWorkSheets.Count
xlWorkSheet = CType(xlWorkSheets(x), Excel.Worksheet)
If xlWorkSheet.Name = SheetName Then
Proceed = True
Exit For
End If
Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet)
xlWorkSheet = Nothing
Next
If Proceed Then
xlCells = xlWorkSheet.Range(CellAddress)
xlCells.Select()
xlCells.NumberFormat = "0"
xlCells.Value = NewCellValue.ToString
Dim xlColumns As Excel.Range = Nothing
xlColumns = xlCells.EntireColumn
xlColumns.AutoFit()
Runtime.InteropServices.Marshal.FinalReleaseComObject(xlColumns)
xlColumns = Nothing
Else
MessageBox.Show(SheetName & " not found.")
End If
xlWorkSheet.SaveAs(OpenFileName)
xlWorkBook.Close()
xlApp.UserControl = True
xlApp.Quit()
ReleaseComObject(xlCells)
ReleaseComObject(xlWorkSheets)
ReleaseComObject(xlWorkSheet)
ReleaseComObject(xlWorkBook)
ReleaseComObject(xlWorkBooks)
ReleaseComObject(xlApp)
Process.Start(OpenFileName)
Else
MessageBox.Show("'" & OpenFileName & "' not located. Try one of the write examples first.")
End If
End Sub
Private Sub ReleaseComObject(ByVal obj As Object)
Try
If obj IsNot Nothing Then
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
End If
obj = Nothing
Catch ex As Exception
obj = Nothing
End Try
End Sub
End Module
答案 1 :(得分:0)
直到细胞或范围级别并应用NumerFormat = "#,##0.00"