hi all,
i have a vb.net form that let's me save a datagridview as excel, but it saves the numbers as text.
should i format the columns with numbers in the vb code or there's any other way to save numbers as numbers?
once i'm using a templated excel, where the datagrid values go to an existing file, i thought that they would be formatted the way the cells were formatted..
this is part of the code i'm using.
Code:Private Sub ButtonExport_Click(sender As Object, e As EventArgs) Handles ButtonExport.Click 'Sub Export() 'My.Application.DoEvents() Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName) Dim xlPath = IO.Path.Combine(exeDir.DirectoryName, "template.xlsx") Dim misValue As Object = System.Reflection.Missing.Value Dim i As Integer Dim j As Integer xlApp = New Microsoft.Office.Interop.Excel.Application xlWorkBook = xlApp.Workbooks.Open(xlPath) xlWorkSheet = xlWorkBook.Worksheets("data") For i = 0 To DataGridView1.RowCount - 1 For j = 0 To DataGridView1.ColumnCount - 1 For k As Integer = 1 To DataGridView1.Columns.Count xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j, i).Value Next Next Next Dim m As Integer Dim n As Integer For m = 0 To DataGridView2.RowCount - 1 For n = 0 To DataGridView2.ColumnCount - 1 For k As Integer = 1 To DataGridView2.Columns.Count xlWorkSheet.Cells(m + 1, n + 6) = DataGridView2(n, m).Value Next Next Next xlWorkSheet.SaveAs("C:\Users\User\Desktop\" & TextBox3.Text & ".xlsx") xlWorkBook.Close() xlApp.Quit() Me.Close() releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorkSheet) End Sub




Reply With Quote
