2个回答
2013-07-22
展开全部
Excel单元格用VB来编辑,还不如直接用Excel编辑,如果如果作为数据存放或者数据导入导出的话。使用编程来做才有意义,我是这么觉得。我这里有个Excel的定义和TXT文件读出并写入Excel单元格,你可以参考下Imports Excel
Imports System.IO
Imports System
Imports System.TextModule ExlApp
Dim exlApp As Application '定义excel应用程序
Dim exlBook As Workbook '定义excel工作簿
Dim exlSheet As Worksheet '定义工作表
Dim exlRange As Range '定义工作区域
Dim rowIndex As Integer Sub Main()
rowIndex = 1
exlApp = New Application()
exlApp.Visible = True
exlBook = exlApp.Workbooks.Add()
exlSheet = exlBook.Sheets(1)
If Not File.Exists(My.Computer.FileSystem.CurrentDirectory & "\充值记录.txt") Then
Console.WriteLine("文件不存在,即将退出")
Console.ReadKey()
Exit Sub
Else
Dim strArray As String() = File.ReadAllLines(My.Computer.FileSystem.CurrentDirectory & "\充值记录.txt", Encoding.Default) For intLN As Integer = 0 To strArray.Length - 1
Dim strSplit, strLine As String
Dim strSub() As String = New String() {}
strSplit = ","
strLine = strArray(intLN)
'test(strLine)
Console.WriteLine(strLine)
strSub = strLine.Split(strSplit)
For i As Integer = 1 To strSub.Length
exlSheet.Cells(rowIndex, i) = strSub(i - 1)
'Console.WriteLine("填充Cell(" & rowIndex & "," & i & "),值为:" & strSub(i - 1))
exlRange = exlSheet.Cells(rowIndex, i)
With exlRange
.Borders.LineStyle = 1
.HorizontalAlignment = 3
.EntireColumn.AutoFit()
End With
Next
rowIndex += 1
Next
End If If File.Exists(My.Computer.FileSystem.CurrentDirectory & "\充值记录.xls") Then
File.Delete(My.Computer.FileSystem.CurrentDirectory & "\充值记录.xls")
End If
exlSheet.SaveAs(My.Computer.FileSystem.CurrentDirectory & "\充值记录.xls")
Console.ReadKey()
End Sub Private Sub test(ByVal str As String)
Dim txt, txtsub As String
For i As Integer = 0 To str.Length - 1
txtsub = str.Substring(i, 1)
txt = Asc(txtsub) & " "
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.CurrentDirectory & "\log.txt", txt, True)
Next
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.CurrentDirectory & "\log.txt", vbCrLf, True)
End Sub
End Module
Imports System.IO
Imports System
Imports System.TextModule ExlApp
Dim exlApp As Application '定义excel应用程序
Dim exlBook As Workbook '定义excel工作簿
Dim exlSheet As Worksheet '定义工作表
Dim exlRange As Range '定义工作区域
Dim rowIndex As Integer Sub Main()
rowIndex = 1
exlApp = New Application()
exlApp.Visible = True
exlBook = exlApp.Workbooks.Add()
exlSheet = exlBook.Sheets(1)
If Not File.Exists(My.Computer.FileSystem.CurrentDirectory & "\充值记录.txt") Then
Console.WriteLine("文件不存在,即将退出")
Console.ReadKey()
Exit Sub
Else
Dim strArray As String() = File.ReadAllLines(My.Computer.FileSystem.CurrentDirectory & "\充值记录.txt", Encoding.Default) For intLN As Integer = 0 To strArray.Length - 1
Dim strSplit, strLine As String
Dim strSub() As String = New String() {}
strSplit = ","
strLine = strArray(intLN)
'test(strLine)
Console.WriteLine(strLine)
strSub = strLine.Split(strSplit)
For i As Integer = 1 To strSub.Length
exlSheet.Cells(rowIndex, i) = strSub(i - 1)
'Console.WriteLine("填充Cell(" & rowIndex & "," & i & "),值为:" & strSub(i - 1))
exlRange = exlSheet.Cells(rowIndex, i)
With exlRange
.Borders.LineStyle = 1
.HorizontalAlignment = 3
.EntireColumn.AutoFit()
End With
Next
rowIndex += 1
Next
End If If File.Exists(My.Computer.FileSystem.CurrentDirectory & "\充值记录.xls") Then
File.Delete(My.Computer.FileSystem.CurrentDirectory & "\充值记录.xls")
End If
exlSheet.SaveAs(My.Computer.FileSystem.CurrentDirectory & "\充值记录.xls")
Console.ReadKey()
End Sub Private Sub test(ByVal str As String)
Dim txt, txtsub As String
For i As Integer = 0 To str.Length - 1
txtsub = str.Substring(i, 1)
txt = Asc(txtsub) & " "
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.CurrentDirectory & "\log.txt", txt, True)
Next
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.CurrentDirectory & "\log.txt", vbCrLf, True)
End Sub
End Module
2013-07-22
展开全部
VBA可以达到预想的效果.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询