预览模式: 普通 | 列表

使用VB.net建立excel文件

' Add the following code snippet on top of Form1.vb
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim appXL As Excel.Application '声明一个application对象
Dim wbXl As Excel.Workbook '声明一个工作簿对象
Dim shXL As Excel.Worksheet '声明一个工作表对象
Dim raXL As Excel.Range '声明一个范围对象
' Start Excel and get Application object.
appXL = CreateObject("Excel.Application")
appXL.Visible = True
' Add a new workbook.
wbXl = appXL.Workbooks.Add
shXL = wbXl.ActiveSheet
' Add table headers going cell by cell.
shXL.Cells(1, 1).Value = "First Name"
shXL.Cells(1, 2).Value = "Last Name"
shXL.Cells(1, 3).Value = "Full Name"
shXL.Cells(1, 4).Value = "Specialization"
' Format A1:D1 as bold, vertical alignment = center.
With shXL.Range("A1", "D1")
.Font.Bold = True
.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
End With
' Create an array to set multiple values at once.
Dim students(5, 2) As String
students(0, 0) = "Zara"
students(0, 1) = "Ali"
students(1, 0) = "Nuha"
students(1, 1) = "Ali"
students(2, 0) = "Arilia"
students(2, 1) = "RamKumar"
students(3, 0) = "Rita"
students(3, 1) = "Jones"
students(4, 0) = "Umme"
students(4, 1) = "Ayman"
' Fill A2:B6 with an array of values (First and Last Names).
shXL.Range("A2", "B6").Value = students
' Fill C2:C6 with a relative formula (=A2 & " " & B2).
raXL = shXL.Range("C2", "C6")
raXL.Formula = "=A2 & "" "" & B2"
' Fill D2:D6 values.
With shXL
.Cells(2, 4).Value = "Biology"
.Cells(3, 4).Value = "Mathmematics"
.Cells(4, 4).Value = "Physics"
.Cells(5, 4).Value = "Mathmematics"
.Cells(6, 4).Value = "Arabic"
End With
' AutoFit columns A:D.
raXL = shXL.Range("A1", "D1")
raXL.EntireColumn.AutoFit()
' Make sure Excel is visible and give the user control
' of Excel's lifetime.
appXL.Visible = True
appXL.UserControl = True
' Release object references.
raXL = Nothing
shXL = Nothing
wbXl = Nothing
appXL.Quit()
appXL = Nothing
Exit Sub
Err_Handler:
MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
End Sub
End Class

Tags: vb.net

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 155

vb.net 下的 CSV文件操作

常见CSV文件的操作

Private Function ExportCsvProcess(ByVal FilePath As String ByVal, dt As DataTable) As Boolean

Dim fileStream As System.IO.FileStream
Dim streamWriter As System.IO.StreamWriter
Dim intRow, intCol As Integer
Dim strRow As String

'删除旧CSV文件
If (System.IO.File.Exists(FilePath)) Then
System.IO.File.Delete(FilePath)
End If

Try
fileStream = New FileStream(FilePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write)
If Not dt Is Nothing Then
streamWriter = New StreamWriter(fileStream, System.Text.Encoding.Default)
strRow = ""
'讀列名
For intCol = 0 To dt.Columns.Count - 1
strRow += dt.Columns(intCol).ColumnName
If intCol < dt.Columns.Count - 1 Then
strRow += ","
End If
Next
streamWriter.WriteLine(strRow)
'读每行的值
For intRow = 0 To dt.Rows.Count - 1
strRow = ""
For intCol = 0 To dt.Columns.Count - 1
strRow += CStr(dt.Rows(intRow).Item(intCol))
If intCol < dt.Columns.Count - 1 Then
strRow += ","
End If
Next
streamWriter.WriteLine(strRow)
Next
streamWriter.Close()
End If
Catch ex As Exception
MessageShow(ex.ToString())
Return False
Finally
fileStream.Close()
End Try
Return True

End Function

查看更多...

Tags: vb.net

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 131

 广告位

↑返回顶部↑