简体   繁体   English

使用 vb.net 将值从 Excel 单元格写入记事本

[英]Writing values from excel cells to notepad using vb.net

I'm trying to copy certain calculated values from MS excel to notepad.我正在尝试将某些计算值从 MS excel 复制到记事本。 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.我使用了Path.Combine所以我不必担心反斜杠是否存在。

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.与 String 不同, StringBuilder是可变的(可变的)。 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.我使用System.IOFile类来创建和写入文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM