我正在使用vb.net生成一個datagrid,並基於datagrid我有一個函數允許用戶導出到excel的datagrid值。奇怪的是我的字符串列值的一個很奇怪:vb.net導出datagrid爲excel某些列數據格式丟失
(1)數據網格的結果:
(2)只有出口後 - 的BQ段數據很奇怪,並從不同什麼在DataGrid:
(3)I 「BQ段」 的單元格的格式更改爲一般或文本 - 結果也怪錯
我出口爲Excel代碼:
Dim xlApp As 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 i As Integer
Dim j As Integer
xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
Next
Next
Next
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.Filter = "Excel File|*.xlsx"
saveFileDialog1.Title = "Save an Excel File"
saveFileDialog1.ShowDialog()
xlApp.ScreenUpdating = True
xlApp.Visible = False
If Not saveFileDialog1.FileName.Equals("") Then
xlApp.DisplayAlerts = False
xlWorkSheet.SaveAs(saveFileDialog1.FileName)
MsgBox("The data has been successfully export as excel file.")
End If
xlWorkBook.Close()
xlApp.Quit()
xlApp = Nothing
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
由代碼造成的錯誤?
嗨赫裏奇,非常感謝你的回答。事實上,價值導致問題真的是一個字符串。我通過轉換它(在變量前添加單引號('))來修復它,然後保存到excel中。在Excel中,以單引號開頭的值被定義爲字符串。 – user30643 2014-09-12 06:05:35
嗨亥裏奇,忘了告訴你,轉換代碼是有用的。 – user30643 2014-09-12 06:18:03