我想用VB编一个程序,能够提取EXCEL文件,然后对这个文件里面的东西进行排序,求大神帮忙。
2个回答
展开全部
读取文件数据,存入数组;
按指定条件对数组排序;
把数组元素输入Excel后保存。
下面是VB操作Excel示例
'工程-部件-引用Microsoft Excel 11.0 Object Library
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
'写文件
Private Sub Command1_Click()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add '新建EXCEL工件簿文件
Set xlSheet = xlBook.Worksheets(1)
For i = 1 To 10 '读取10行
For j = 1 To 10 '读取10列
xlSheet.Cells(i, j) = i * j
Next j
Next i
xlSheet.SaveAs "d:\test.xls" '按指定文件名存盘
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Sub
'读文件
Private Sub Command2_Click()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Application.Workbooks.Open("d:\test.xls")
Set xlSheet = xlBook.Worksheets(1)
For i = 1 To 10 '读取10行
For j = 1 To 10 '读取10列
Text1 = Text1 & xlSheet.Cells(i, j) & Space(5)
Next j
Text1 = Text1 & vbNewLine '另起一行
Next i
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Sub
数组排序不会的可以Hi我,或再次提问
按指定条件对数组排序;
把数组元素输入Excel后保存。
下面是VB操作Excel示例
'工程-部件-引用Microsoft Excel 11.0 Object Library
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
'写文件
Private Sub Command1_Click()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add '新建EXCEL工件簿文件
Set xlSheet = xlBook.Worksheets(1)
For i = 1 To 10 '读取10行
For j = 1 To 10 '读取10列
xlSheet.Cells(i, j) = i * j
Next j
Next i
xlSheet.SaveAs "d:\test.xls" '按指定文件名存盘
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Sub
'读文件
Private Sub Command2_Click()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Application.Workbooks.Open("d:\test.xls")
Set xlSheet = xlBook.Worksheets(1)
For i = 1 To 10 '读取10行
For j = 1 To 10 '读取10列
Text1 = Text1 & xlSheet.Cells(i, j) & Space(5)
Next j
Text1 = Text1 & vbNewLine '另起一行
Next i
xlApp.Quit '结束EXCEL对象
Set xlApp = Nothing '释放xlApp对象
End Sub
数组排序不会的可以Hi我,或再次提问
追问
比如说XCH66-01-00-000这个啊,我就要先按01这个为首要的,其次按00,最后按000,开头不是XCH66的排到后面。EXCEL里面有数据的,直接提取就行,排列完成了再保存下。
追答
例如:a()是读取后得到的信息,新建数组b()存放处理后数据
Dim a(50) As String, b(50) As String, k As Integer
For i = 0 To UBound(a)
If Left(a(i), 5) = "XCH66" Then b(k) = a(i)
k = k + 1
Next i
For i = 0 To UBound(a)
If Left(a(i), 2) = "01" Then b(k) = a(i)
k = k + 1
Next i
For i = 0 To UBound(a)
If Left(a(i), 5) "XCH66" Then b(k) = a(i)
k = k + 1
Next i
b()为处理后数据,存入Excel
这只是示例,不代表实际操作,还有不会的Hi我,在此追问多会扣我们的分
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询