1 生成txt文件。
十載的項(xiàng)城網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整項(xiàng)城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“項(xiàng)城網(wǎng)站設(shè)計(jì)”,“項(xiàng)城網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
DimSaveFileDialog1AsNewSaveFileDialog()?'創(chuàng)建一個(gè)保存對(duì)話框
SaveFileDialog1.Filter?="txt?files?(*.txt)|*.txt"?'設(shè)置擴(kuò)展名
IfSaveFileDialog1.ShowDialog()?=?System.Windows.Forms.DialogResult.OKThen?'如果確定保存
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.Filename,?Textbox1.Text,False)?'保存文本,False表示不追加文本,直接覆蓋其內(nèi)容
EndIf
原文鏈接:
以下是我以前百度找的資料 希望對(duì)你有用 你讀取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ò)展名,將自動(dòng)家上
.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
‘首先,創(chuàng)建一個(gè)VB.NET項(xiàng)目,再點(diǎn)擊菜單”項(xiàng)目“-”添加引用“,在”Com“-”類型庫“里,選擇Microsoft.Word?14.0?Object?Library(對(duì)應(yīng)Office版本為2010),將以下代碼復(fù)制粘貼到Form1窗體替換原代碼。
’PS:以下代碼僅創(chuàng)建表格,將第行2至4列合并,添加1個(gè)文字,其他的你自己分析。
Imports?Microsoft.Office.Interop
Imports?Microsoft.Office.Interop.Word??'引入Word
Public?Class?Form1
Private?Sub?Form1_Load(sender?As?Object,?e?As?EventArgs)?Handles?MyBase.Load
Dim?DWrd?As?New?Word.Application,?DDoc?As?Word.Document?'定義Word及文檔對(duì)象
DDoc?=?DWrd.Documents.Add?'新增文檔
Dim?DTbl?As?Word.Table??'定義表格對(duì)象
DTbl?=?DDoc.Tables.Add(DDoc.Range,?7,?4)??'新增表格
DTbl.Style?=?"網(wǎng)格型"??'表格的樣式
DTbl.Cell(1,?2).Select()??'選擇起始單元格為第1行/第2列
DWrd.Selection.MoveRight(,?2,?SelectionMode.MultiExtended)??'向右擴(kuò)展兩個(gè)單元格(即選擇第1行的第2至4列)
DWrd.Selection().Cells.Merge()??'單元格合并
DTbl.Cell(1,?1).Range.Text?=?1??'在第1個(gè)單元格填寫文本
DWrd.Visible?=?True?'顯示W(wǎng)ord
End?Sub
End?Class
文章標(biāo)題:關(guān)于vbnet表格實(shí)例的信息
文章地址:http://jinyejixie.com/article6/dohpsog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、全網(wǎng)營銷推廣、網(wǎng)站導(dǎo)航、定制網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)