重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

关于vb.net导出xls的信息

vb.net 输出 excel

这里有段VB6.0的,你可以参考。

为科尔沁左翼等地区用户提供了全套网页设计制作服务,及科尔沁左翼网站建设行业解决方案。主营业务为成都网站设计、网站制作、科尔沁左翼网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

注意添加引用。

Private Sub Command6_Click()

Dim i, j As Integer

Dim xlApplication As Excel.Application, xlWorkbook As Excel.Workbook, xlSheet

Dim xlApp As Excel.Application

On Error Resume Next

Set xlApplication = GetObject(, "Excel.Application")

Set xlApp = CreateObject("Excel.Application")

If MsgBox("确认将文件信息导出到EXCEL中?�", vbExclamation + vbYesNo, "警告") = vbYes Then

If Err.Number 0 Then Set xlApplication = CreateObject("Excel.Application")

Set xlWorkbook = xlApplication.Workbooks.Add

Set xlSheet = xlWorkbook.ActiveSheet

xlSheet.Cells(1, 2) = lblcl.Caption

xlSheet.Range("A1:E1").MergeCells = True

xlSheet.Range("A1:E1").HorizontalAlignment = xlCenter

xlSheet.Cells(2, 2).ColumnWidth = 18

For i = 1 To DataGrid1.Columns.Count

xlSheet.Cells(2, 1) = "编号"

xlSheet.Cells(2, i + 1) = DataGrid1.Columns(i).Caption

For j = 0 To DataGrid1.VisibleRows - 1

xlSheet.Cells(j + 3, 1) = j + 1

xlSheet.Cells(j + 3, i + 1) = DataGrid1.Columns(i).CellText(DataGrid1.RowBookmark(j))

Next j

Next i

xlApplication.Visible = True

Set xlSheet = Nothing

Set xlWorkbook = Nothing

Set xlApplication = Nothing

'xlApp.Range("A2:L2").Columns.Interior.ColorIndex = 40

'xlApp.Range("A2:L2").Borders.LineStyle = xlContinuous

'xlApp.Visible = True

'xlApp.Range(xlSheet.Cells(2 + PartsRs.RecordCount + 1, 1), xlSheet.Cells(2 + PartsRs.RecordCount + 1, 8)).Columns.Interior.ColorIndex = 40

'xlApp.Range(xlSheet.Cells(2 + PartsRs.RecordCount + 1, 1), xlSheet.Cells(2 + PartsRs.RecordCount + 1, 8)).Borders.LineStyle = xlContinuous

Else

MsgBox "无信息可供您导出,请确认!", vbExclamation + vbOKOnly, "警告"

End If

End Sub

VB.NET怎么将DataGridview里的内容导出到EXCEL去

不好意思 现在才看到;Private Sub 导出EXCEL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim xlApp, xlBook, xlSheet As Object

xlapp = CreateObject("Excel.Application")

xlbook = xlapp.Workbooks.Add

xlsheet = xlbook.Worksheets(1)

'Dim xlapp As New Excel.Application

'Dim xlbook As Excel.Workbook

'Dim xlsheet As Excel.Worksheet

Dim rowindex, colindex As Integer rowindex = 1 '行

colindex = 0 '列

'xlbook = xlapp.Workbooks.Open("c:\EXCEL.xls") '打开EXCEL文件

xlsheet = xlapp.Worksheets("sheet1") '打开sheet1那页 Dim a As New DataSet

Dim table As New System.Data.DataTable table = DataSet11.Tables("INVMATLISTA") '填充表 Dim row As DataRow '定义row为表格的行

Dim col As DataColumn '定义col为表格的列 '把表格的每一列写到EXCEL去

For Each col In table.Columns

colindex = colindex + 1

xlapp.Cells(1, colindex) = col.ColumnName Next

'把表格的每一行写到EXCEL去

For Each row In table.Rows

rowindex = rowindex + 1

colindex = 0

For Each col In table.Columns

colindex = colindex + 1

xlapp.Cells(rowindex, colindex) = row(col.ColumnName)

Next

Next

xlapp.Visible = True

End Sub 前提要先引用一个Microsoft.Office.Interop.Excel.dll然后在最上面先输入Imports Microsoft.Office.Interop; 就可以了

vb中如何将数据导出到excel

介绍

下面通过一步一步的介绍,如何通过VB.NET来读取数据,并且将数据导入到Excel中。

第一步:

打开VS开发工具,并且添加引用。

然后选择。

Microsoft Excel 12.0 object library and。

Microsoft Excel 14.0 object library。

第二步:

创建一个Excle在你的电脑中。

第三步:

在VS中写入如下代码:

Imports System.Data

Imports System.Data.SqlClient

Imports Excel = Microsoft.Office.Interop.Excel。

Public Class excel

‘添加按钮

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

  Handles Button1.Click

Try

    '创建连接

    Dim cnn As DataAccess = New DataAccess(CONNECTION_STRING)

    

    Dim i, j As Integer

    '创建Excel对象

    Dim xlApp As Microsoft.Office.Interop.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

    xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass

    xlWorkBook = xlApp.Workbooks.Add(misValue)

    ' 打开某一个表单

    xlWorkSheet = xlWorkBook.Sheets("sheet1")

    ' sql查询

    '  xlWorkBook.Sheets.Select("A1:A2")

    Dim sql As String = "SELECT * FROM EMP"

    ' SqlAdapter

    Dim dscmd As New SqlDataAdapter(sql, cnn.ConnectionString)

    ' 定义数据集

    Dim ds As New DataSet

    dscmd.Fill(ds)

     ‘添加字段信息到Excel表的第一行

    xlWorkSheet.Cells(1, 1).Value = "First Name"

    xlWorkSheet.Cells(1, 2).Value = "Last Name"

    xlWorkSheet.Cells(1, 3).Value = "Full Name"

    xlWorkSheet.Cells(1, 4).Value = "Salary"

    ' 将数据导入到excel

      For i = 0 To ds.Tables(0).Rows.Count - 1

        'Column

        For j = 0 To ds.Tables(0).Columns.Count - 1

            ' this i change to header line cells

            xlWorkSheet.Cells(i + 3, j + 1) = _

            ds.Tables(0).Rows(i).Item(j)

        Next

    Next

    'HardCode in Excel sheet

    ' this i change to footer line cells  

   xlWorkSheet.Cells(i + 3, 7) = "Total"

    xlWorkSheet.Cells.Item(i + 3, 8) = "=SUM(H2:H18)"

    ' 保存到Excel

    xlWorkSheet.SaveAs("D:\vbexcel.xlsx")

    xlWorkBook.Close()

    xlApp.Quit()

    releaseObject(xlApp)

    releaseObject(xlWorkBook)

    releaseObject(xlWorkSheet)

    '弹出对话框显示保存后的路径

    MsgBox("You can find the file D:\vbexcel.xlsx")

Catch ex As Exception

End Try

End Sub

' Function of Realease Object in Excel Sheet

Private Sub releaseObject(ByVal obj As Object)

Try

    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)

    obj = Nothing

Catch ex As Exception

    obj = Nothing

Finally

    GC.Collect()

End Try

End Sub

End Class

复制代码。

第四步:

看到如下导出结果。


文章标题:关于vb.net导出xls的信息
分享URL:http://cqcxhl.cn/article/doddejc.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP