微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用VB2008操作Excel范例

Imports Microsoft.Office.Interop.Excel

Module Module1

''' <summary>

''' Carriage组装件所包含的主要零件

''' </summary>

''' <remarks></remarks>

Public Structure CarriageAssy

Public AssyCarriage As String

Public BodyCarriage As String

Public Mirror As String

Public Lens As String

Public ClampMirror As String

Public CCD As String

End Structure

''' <summary>

''' BOM表里需要查询的项目

''' </summary>

''' <remarks></remarks>

Public Enum ExcelItem

Level = 1

Material = 7

Description = 8

Manufacturer = 16

vendor = 17

End Enum

Sub Main()

Dim CA As New CarriageAssy With {.AssyCarriage = "Assy,Carriage",_

.BodyCarriage = "Body,.Mirror = "Mirror,",_

.Lens = "Lens,.ClampMirror = "Clamp,Mirror",.CCD = "PCBA,CCD"}

Call DoSomething(CA)

Console.WriteLine("Finished,please go ahead.")

Console.ReadLine()

End Sub

''' <summary>

''' 按指定的零件来分类

''' </summary>

''' <remarks></remarks>

Sub DoSomething(ByVal part As String)

Dim xlApp As Application = CreateObject("Excel.Application")

xlApp.Visible = True

Dim wb As Workbook = xlApp.Workbooks.Add

Dim sht1 As Worksheet = wb.Sheets(1)

Dim wbSource As Workbook = xlApp.Workbooks.Open("D:/BOM List 2010.xls")

Dim sht As Worksheet = wbSource.Sheets(1)

With sht

Dim iRow As Integer = .Range("A65536").End(XlDirection.xlUp).Row

Dim i As Integer = 1

For r As Integer = 1 To iRow

Dim tmp As String = CStr(.Cells(r,ExcelItem.Description).value)

If tmp Is nothing Then Continue For

If tmp.StartsWith(part) Then

Console.WriteLine(.Cells(r,ExcelItem.Material).value)

sht1.Cells(i,1).value = .Cells(r,ExcelItem.Material).value

sht1.Cells(i,2).value = .Cells(r,ExcelItem.Description).value

sht1.Cells(i,3).value = .Cells(r,ExcelItem.Manufacturer).value

sht1.Cells(i,4).value = .Cells(r,ExcelItem.vendor).value

i += 1

End If

Next

End With

End Sub

''' <summary>

''' 按成品料号来分类

''' </summary>

''' <remarks></remarks>

Sub DoSomething(ByVal ca As CarriageAssy)

Dim xlApp As Application = CreateObject("Excel.Application")

xlApp.Visible = True

Dim wb As Workbook = xlApp.Workbooks.Add

Dim sht1 As Worksheet = wb.Sheets(1)

Dim wbSource As Workbook = xlApp.Workbooks.Open("D:/BOM List 2010.xls")

Dim sht As Worksheet = wbSource.Sheets(1)

With sht

Dim iRow As Integer = .Range("A65536").End(XlDirection.xlUp).Row

Dim i As Integer = 1

For r As Integer = 1 To iRow

Dim tmp As String = CStr(.Cells(r,ExcelItem.Description).value)

If tmp Is nothing Then Continue For

If tmp = ExcelItem.Description.ToString Then

i += 1

Console.WriteLine(.Cells(r - 1,1).value = .Cells(r - 1,2).value = .Cells(r - 1,ExcelItem.Description).value

sht1.Range(sht1.Cells(i,1),sht1.Cells(i,2)).Interior.ColorIndex = 35

sht1.Cells(i,3).value = .Cells(r - 1,4).value = .Cells(r - 1,ExcelItem.vendor).value

i += 1

ElseIf tmp.StartsWith(ca.AssyCarriage) _

OrElse tmp.StartsWith(ca.BodyCarriage) _

OrElse tmp.StartsWith(ca.Mirror) _

OrElse tmp.StartsWith(ca.Lens) _

OrElse tmp.StartsWith(ca.ClampMirror) _

OrElse tmp.StartsWith(ca.CCD) Then

sht1.Cells(i,ExcelItem.vendor).value

i += 1

End If

Next

End With

End Sub

End Module

原文地址:https://www.jb51.cc/vb/262171.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐