2016-06-11 4 views
0

に消える:ポイント私はExcelファイルにDGVを保存するには、このコードを使用していますエクセル

If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then 
     Exit Sub 
    End If 

    'Creating dataset to export 
    Dim dset As New DataSet 
    'add table to dataset 
    dset.Tables.Add() 
    'add column to that table 
    For i As Integer = 0 To DataGridView1.ColumnCount - 1 
     dset.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText) 
    Next 
    'add rows to the table 
    Dim dr1 As DataRow 
    For i As Integer = 0 To DataGridView1.RowCount - 1 
     dr1 = dset.Tables(0).NewRow 
     For j As Integer = 0 To DataGridView1.Columns.Count - 1 
      dr1(j) = DataGridView1.Rows(i).Cells(j).Value 

     Next 
     dset.Tables(0).Rows.Add(dr1) 
    Next 

    Dim excel As Microsoft.Office.Interop.Excel.Application 
    Dim wBook As Microsoft.Office.Interop.Excel.Workbook 
    Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet 

    excel = New Microsoft.Office.Interop.Excel.Application 
    wBook = excel.Workbooks.Add() 
    wSheet = wBook.ActiveSheet() 

    Dim dt As System.Data.DataTable = dset.Tables(0) 
    Dim dc As System.Data.DataColumn 
    Dim dr As System.Data.DataRow 
    Dim colIndex As Integer = 0 
    Dim rowIndex As Integer = 0 

    For Each dc In dt.Columns 
     colIndex = colIndex + 1 
     excel.Cells(1, colIndex) = dc.ColumnName 
    Next 

    For Each dr In dt.Rows 
     rowIndex = rowIndex + 1 
     colIndex = 0 
     For Each dc In dt.Columns 
      colIndex = colIndex + 1 
      excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName) 

     Next 
    Next 

    wSheet.Columns.AutoFit() 
    Dim strFileName As String = "D:\ss.xls" 
    Dim blnFileOpen As Boolean = False 
    Try 
     Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName) 
     fileTemp.Close() 
    Catch ex As Exception 
     blnFileOpen = False 
    End Try 

    If System.IO.File.Exists(strFileName) Then 
     System.IO.File.Delete(strFileName) 
    End If 

    wBook.SaveAs(strFileName) 
    excel.Workbooks.Open(strFileName) 
    excel.Visible = True 

が、Excelのページが現れたときに、いくつかの数はDataGridViewの中exempleのために、ポイントを持っていないX = 5.76546753435677が、それはXのようになりますエクセルで= 576546753435677 あなたは私を助けることができますPLZ。

+2

あなたのコードはどの言語ですか? 'For i As Integer = 0 To ... 'は有効なVBA構文ではありません。 –

答えて

0

小数点以下の桁は、おそらくExcelとこのVB(VB.NET?)環境のソフトウェアによって異なります。 Excelでは、デフォルトで、数値書式設定用のウィンドウの地域設定(小数点と桁区切り記号)を使用していますが、上書きすることはできます。同時に、上記のコードがWebページなどで実行される場合、数値書式設定のロケール設定が使用されます。私は2つの違いが問題を引き起こさなければならないと思う。

関連する問題