千家信息网

VB.NET如何操作Excel

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,这篇文章主要为大家展示了"VB.NET如何操作Excel",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"VB.NET如何操作Excel"这篇文章吧。VB.N
千家信息网最后更新 2025年01月23日VB.NET如何操作Excel

这篇文章主要为大家展示了"VB.NET如何操作Excel",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"VB.NET如何操作Excel"这篇文章吧。

VB.NET操作Excel代码

  1. Public Sub Printer()Sub Printer
    (ByVal pid As Int16, ByVal dbhlp
    As DBHelper, ByVal objds As ploy)

  2. Dim sqlstr As String

  3. sqlstr = "select * from Ploy
    where ployID="
    & pid

  4. dbhlp.FillDataTable(sqlstr,
    objds, "Ploy")

  5. sqlstr = "select * from PForeign
    where ployID="
    & pid

  6. dbhlp.FillDataTable(sqlstr,
    objds, "PForeign")

  7. Dim exl As New Excel.Application

  8. exl.Visible = True

  9. exl.DisplayAlerts = True

  10. exl.WindowState = Excel.XlWindow
    State.xlMaximized

  11. 'Dim wb As Excel.Workbook =
    exl.Workbooks.Open(Application.
    StartupPath & "\print.xls")

  12. 'Dim ws As Excel.Worksheet =
    wb.Worksheets("prt")

  13. Dim wb As Excel.Workbook =
    exl.Workbooks.Add()

  14. Dim ws As Excel.Worksheet =
    wb.Worksheets.Item(1)

  15. Dim Rng As Excel.Range

  16. Rng = ws.Columns(1)

  17. Rng.ColumnWidth = 16

  18. Rng = ws.Columns(2)

  19. Rng.ColumnWidth = 46

  20. Rng = ws.Columns(3)

  21. Rng.ColumnWidth = 16

  22. Dim dr As ploy.PloyRow

  23. dr = objds.Ploy.Rows(0)

  24. Try

  25. Rng = ws.Range("A1:A1", "C1:C1")

  26. Rng.Cells.Merge()'合并单元格

  27. Rng.Font.Name = "黑体"

  28. Rng.Font.Size = "22"

  29. Rng.HorizontalAlignment = -4108

  30. Rng.VerticalAlignment = -4108

  31. Rng.Value = dr.PSubject

  32. Catch ex As Exception

  33. End Try

Try  Rng = ws.Range("A2:A2", "C2:C2")  Rng.Cells.Merge()  Rng.Font.Name = "黑体" Rng.Font.Size = "14" Rng.HorizontalAlignment = -4108  Rng.VerticalAlignment = -4108  Rng.Value = "时间:" & dr.PTime  Catch ex As Exception  End Try
Try  Rng = ws.Range("A3:A3", "C3:C3")  Rng.Cells.Merge()  Rng.Font.Name = "黑体" Rng.Font.Size = "14" Rng.HorizontalAlignment = -4108  Rng.VerticalAlignment = -4108  Rng.Value = "地点:" & dr.Place  Catch ex As Exception  End Try
  1. Dim drF As ploy.PForeignRow

  2. Dim i, rngi As Integer

  3. Dim rngs, cellB, cellE As String

  4. rngi = 5 + objds.PForeign.Rows.Count

  5. cellB = "A5:A5"

  6. cellE = "C" & rngi.ToString &
    ":C" & rngi.ToString

  7. Rng = ws.Range(cellB, cellE)

  8. Rng.Font.Name = "仿宋_GB2312"

  9. Rng.Font.Size = "16"

  10. Rng.Borders.LineStyle = 1

  11. Rng.HorizontalAlignment = -4108

  12. Rng.VerticalAlignment = -4108

  13. Rng = ws.Range("A5")

  14. Rng.Value = "领馆"

  15. Rng = ws.Range("B5")

  16. Rng.Value = "出席人员"

  17. Rng = ws.Range("C5")

  18. Rng.Value = "职衔"

  19. rngi = 6

  20. For i = 0 To objds.PForeign.
    Rows.Count - 1

  21. drF = objds.PForeign.Rows(i)

  22. Rng = ws.Range("A" & rngi.ToString)

  23. Rng.Value = drF.PConsulate

  24. Rng = ws.Range("B" & rngi.ToString)

  25. Rng.Value = drF.PName

  26. Rng = ws.Range("C" & rngi.ToString)

  27. Rng.Value = drF.PRank

  28. rngirngi = rngi + 1

  29. Next

  30. 'wb.Save()

  31. 'If Not exl Is Nothing Then

  32. ' exl.Workbooks.Close()

  33. ' exl.Quit()

  34. ' System.Runtime.InteropServices.
    Marshal.ReleaseComObject(exl)

  35. 'End If

  36. 'If Not ws Is Nothing Then

  37. ' System.Runtime.InteropServices.
    Marshal.ReleaseComObject(ws)

  38. 'End If

  39. 'ws = Nothing

  40. 'exl = Nothing

  41. 'GC.Collect()

  42. End Sub

  43. End Class

以上是"VB.NET如何操作Excel"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0