I'm trying to copy certain calculated values from MS excel to notepad. But its fetching empty values. can someone please check and help me resolve this issue?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
workbook = xls.Workbooks.Open(filereader & "excelfilename.xls")
worksheet = workbook.Worksheets(1)
Dim Datawriter As New StreamWriter(filereader & "FiletoCopy.txt")
Dim range1 As Excel.Range
Dim range2 As Excel.Range
Dim range3 As Excel.Range
Dim range4 As Excel.Range
Dim range5 As Excel.Range
Dim CellValue1 As String
Dim CellValue2 As String
Dim CellValue3 As String
Dim CellValue4 As String
Dim CellValue5 As String
range1 = CType(worksheet, Excel.Worksheet).Range(“A1”)
range2 = CType(worksheet, Excel.Worksheet).Range(“A2”)
range3 = CType(worksheet, Excel.Worksheet).Range(“A3”)
range4 = CType(worksheet, Excel.Worksheet).Range(“A4”)
range5 = CType(worksheet, Excel.Worksheet).Range(“A5”)
CellValue1 = Math.Round((range1.Value), 2)
CellValue2 = Math.Round((range2.Value), 2)
CellValue3 = Math.Round((range3.Value), 2)
CellValue4 = Math.Round((range4.Value), 2)
CellValue5 = Math.Round((range5.Value), 2)
Datawriter.WriteLine(CellValue1, vbCrLf)
Datawriter.WriteLine(CellValue2, vbCrLf)
Datawriter.WriteLine(CellValue3, vbCrLf)
Datawriter.WriteLine(CellValue4, vbCrLf)
Datawriter.WriteLine(CellValue5, vbCrLf)
Datawriter.Close()
MessageBox.Show("Output Generated Sucessfully", "Success", MessageBoxButtons.OK)
End Sub
I changed the names of your worksheet and workbook. I just don't like variables named the same as the class name.
I used Path.Combine
so I don't have to worry about whether the backslash is there or not.
I created and filled a list of doubles containing the values from you spreadsheet.
I am using a string builder to build the text to be saved. A StringBuilder
is mutable (changeable) unlike a String. Each string would have to be discarded and a new one created otherwise.
I used the File
class in System.IO
to create and write the file.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim filereader = "C:\SomeFolder\"
Dim wbook = xls.Workbooks.Open(Path.Combine(filereader, "excelfilename.xls"))
Dim wsheet As Excel.Worksheet = CType(wbook.Worksheets(1), Excel.Worksheet)
Dim lstDoubles As New List(Of Double) From {
CDbl(wsheet.Range("A1").Value),
CDbl(wsheet.Range("A2").Value),
CDbl(wsheet.Range("A3").Value),
CDbl(wsheet.Range("A4").Value),
CDbl(wsheet.Range("A5").Value)
}
'StringBuilder requires Imports System.Text
Dim sb As New StringBuilder
For Each d In lstDoubles
Dim roundedString = Math.Round(d, 2).ToString
sb.AppendLine(roundedString)
Next
File.WriteAllText(Path.Combine(filereader, "FiletoCopy.txt"), sb.ToString)
MessageBox.Show("Output Generated Sucessfully", "Success", MessageBoxButtons.OK)
End Sub
You can also try the following methods. I just slightly modified some of your code.
Imports System.IO
Imports Microsoft.Office.Interop.Excel
Public Class Form1
Dim filereader As String = "E:\Julie\Case\AccessDB\"
Dim xls As Application = New Application()
Dim Workbook As Workbook
Dim Worksheet As Worksheet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Workbook = xls.Workbooks.Open(filereader & "exceltest.xlsx")
Worksheet = Workbook.Worksheets(1)
Dim Datawriter As New StreamWriter(filereader & "FiletoCopy.txt")
For i = 1 To 5
Dim temp As String = (CType(Worksheet.Cells(1, i), Range)).Text
Datawriter.WriteLine(temp, vbCrLf)
Next
Datawriter.Close()
MessageBox.Show("Output Generated Sucessfully", "Success", MessageBoxButtons.OK)
End Sub
End Class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.