這里有段VB6.0的,你可以參考。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比漣源網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式漣源網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋漣源地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
注意添加引用。
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("確認(rèn)將文件信息導(dǎo)出到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 "無信息可供您導(dǎo)出,請確認(rèn)!", vbExclamation + vbOKOnly, "警告"
End If
End Sub
一、DCOM修復(fù)
1.運行:mmc comexp.msc /32,找到我的電腦 - DCom配置中的Microsoft Excel Application
2.在Microsoft Excel Application上點擊右鍵,選擇"屬性"
3.點擊"標(biāo)識"標(biāo)簽,選擇"交互式用戶"
4.點擊"安全"標(biāo)簽,在"啟動和激活權(quán)限"上點擊"自定義",然后點擊對應(yīng)的"編輯"按鈕,在彈出的"安全性"對話框中填加一個"NETWORK SERVICE"用戶(注意要選擇本計算機(jī)名),并給它賦予"本地啟動"和"本地激活"權(quán)限.
5.依然是"安全"標(biāo)簽,在"訪問權(quán)限"上點擊"自定義",然后點擊"編輯",在彈出的"安全性"對話框中也填加一個"NETWORK SERVICE"用戶,然后賦予"本地訪問"權(quán)限.
二、如果找不到Microsoft Excel 應(yīng)用程序
1、進(jìn)行OFFICE菜單中的組件重注冊
2、如果仍未解決問題,點 開始--運行--輸入EXCEL文件所在的路徑 “c:\program files\exel.exe” /regserver 進(jìn)行修復(fù)注冊
參考:
先在: My Project 中引用 Microsoft Excel 11.0 Object Library(這個是Excel 2003,Excel 2007 是12.0),之后在窗體代碼中加入相關(guān)內(nèi)容就行了。以下是一個窗體的簡單實例
假設(shè)在C盤根文件夾中有:TEST.xls
Public Class 引用EXCEL窗體
Private A() As String = {"A", "B", "CC", "C", "D"}
Private B() As Integer = {1, 22, 34, 50, 16, 99, 14}
Private excelapp As New Microsoft.Office.Interop.Excel.Application
Private excelworkbook As Microsoft.Office.Interop.Excel.Workbook
Private Sub 引用EXCEL_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Index As Integer
excelworkbook = excelapp.Workbooks.Open("c:\test.xls")
excelapp.Application.Workbooks.Add(True)
excelapp.Cells(1, 1) = "數(shù)組:A"
For Index = 0 To UBound(A)
excelapp.Cells(Index + 2, 1) = A(Index)
Next
excelapp.Cells(1, 2) = "數(shù)組:B"
For Index = 0 To UBound(B)
excelapp.Cells(Index + 2, 2) = B(Index)
Next
excelapp.Visible = True
End Sub
End Class
運行結(jié)果:
數(shù)組:A 數(shù)組:B
A 1
B 22
CC 34
C 50
D 16
99
14
以下是我以前百度找的資料 希望對你有用 你讀取DataGridView到DataGrid然后直接調(diào)用函數(shù)即可
Public Function ExportXLsD(ByVal datagrid As DataGrid) ', ByVal Title As String)
'Dim Mytable As New DataTable
'Mytable = CType(datagrid.DataSource, DataTable)
If mytable Is Nothing Then
MessageBox.Show("沒有記錄不能導(dǎo)出數(shù)據(jù)", "PurpleStar", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Function
End If
If mytable.Rows.Count 0 Then
Dim MyFileName As String
Dim FileName As String
With SaveFileDialog1
.AddExtension = True '如果用戶忘記添加擴(kuò)展名,將自動家上
.DefaultExt = "xls" '默認(rèn)擴(kuò)展名
.Filter = "Excel文件(*.xls)|*.xls"
.Title = "文件保存到"
If .ShowDialog = DialogResult.OK Then
FileName = .FileName
End If
End With
MyFileName = Microsoft.VisualBasic.Right(FileName, 4)
If MyFileName = "" Then
Exit Function
End If
If MyFileName = ".xls" Or MyFileName = ".XLS" Then
Dim FS As FileStream = New FileStream(FileName, FileMode.Create)
Dim sw As StreamWriter = New StreamWriter(FS, System.Text.Encoding.Default)
sw.WriteLine(vbTab FileName vbTab Date.Now)
Dim i, j As Integer
Dim str As String = ""
For i = 0 To mytable.Columns.Count - 1
str = mytable.Columns(i).Caption
sw.Write(str vbTab)
Next
sw.Write(vbCrLf)
For j = 0 To mytable.Rows.Count - 1
For i = 0 To mytable.Columns.Count - 1
Dim strColName, strRow As String
strRow = IIf(mytable.Rows(j).Item(i) Is DBNull.Value, "", mytable.Rows(j).Item(i))
sw.Write(strRow vbTab)
Next
sw.Write(vbLf)
Next
sw.Close()
FS.Close()
MessageBox.Show("數(shù)據(jù)導(dǎo)出成功!", "PurpleStar", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Exit Function
End If
Else
MessageBox.Show("沒有記錄不能導(dǎo)出數(shù)據(jù)", "PurpleStar", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Function
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Dim saveExcel As SaveFileDialog
saveExcel = New SaveFileDialog
saveExcel.Filter = "Excel文件(.xls)|*.xls"
Dim filename As String
If saveExcel.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
filename = saveExcel.FileName
Dim excel As Excel.Application
excel = New Excel.Application
excel.DisplayAlerts = False
excel.Workbooks.Add(True)
excel.Visible = False
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
excel.Cells(1, i + 1) = DataGridView1.Columns(i).HeaderText
Next
'設(shè)置標(biāo)題
Dim j As Integer
For i = 0 To DataGridView1.Rows.Count - 1 '填充數(shù)據(jù)
For j = 0 To DataGridView1.Columns.Count - 1
excel.Cells(i + 2, j + 1) = DataGridView1(j, i).Value
Next
Next
excel.Workbooks(1).SaveCopyAs(filename) '保存
Me.Close()
End Sub
                網(wǎng)頁名稱:vb點虐
輸出表格 vbnet chart
                
                轉(zhuǎn)載注明:http://www.yijiale78.com/article44/ddjgehe.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、外貿(mào)建站、做網(wǎng)站、全網(wǎng)營銷推廣、建站公司、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)