How to handle multiple files, to Excel

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 6
Reputation: BULK is an unknown quantity at this point 
Solved Threads: 0
BULK BULK is offline Offline
Newbie Poster

How to handle multiple files, to Excel

 
0
  #1
Feb 6th, 2008
Hi
I have a progg that opens a file containing text in a Richtextbox, the textbox content is manipulated and sent to an other Richtextbox. Then I create a new excel workbook and a sheet, select some of the lines and sent to the excel sheet. This is probably a odd way to make a report, but it's working so I'm happy so far. Can somebody help me how to handle if I want to open several files, and loop through adding new sheets for every new file? Without creating new workbook's. This is an example of my code.

  1. Imports system.IO
  2.  
  3. Imports Microsoft.Office.Core
  4.  
  5. Public Class Form1
  6. Private Filename As String
  7. Dim sr As StreamReader
  8.  
  9.  
  10. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  11.  
  12. Try
  13. With OpenFileDialog1
  14. 'OpenFileDialog1
  15. .Filter = "Test files (prept*)|prept*|" & "All files|*.*"
  16. If .ShowDialog() = DialogResult.OK Then
  17. Filename = .FileName
  18. sr = New StreamReader(.OpenFile)
  19. 'using streamreader to read the opened text file
  20. RichTextBox1.Text = sr.ReadToEnd()
  21.  
  22. 'displaying text from streamreader in richtextbox
  23. End If
  24. End With
  25. Catch es As Exception
  26. MessageBox.Show(es.Message)
  27. Finally
  28. If Not (sr Is Nothing) Then
  29. sr.Close()
  30. End If
  31. End Try
  32. End Sub
  33.  
  34. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  35.  
  36. End Sub
  37.  
  38. Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
  39. RichTextBox1.Text = Replace(RichTextBox1.Text, Chr(35), String.Empty)
  40.  
  41. For Each line As String In RichTextBox1.Lines
  42. Dim equalsPos As Integer = line.IndexOf("=") + 1
  43. If equalsPos = 0 Then equalsPos = line.IndexOf(":") + 1
  44. RichTextBox2.AppendText(line.Substring(equalsPos, line.Length - equalsPos).Trim() & vbCrLf)
  45. Dim txt As String
  46. txt = RichTextBox2.Text
  47. Next
  48. End Sub
  49.  
  50.  
  51. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  52. Dim oXL As Excel.Application
  53. Dim oWB As Excel.Workbook
  54. Dim oSheet As Excel.Worksheet
  55. Dim oRng As Excel.Range
  56.  
  57. ' Start Excel and get Application object.
  58. oXL = CreateObject("Excel.Application")
  59. oXL.Visible = False
  60.  
  61. ' Get a new workbook.
  62. oWB = oXL.Workbooks.Add
  63. oSheet = oWB.ActiveSheet
  64. Dim first As String
  65. first = DateString
  66.  
  67.  
  68. ' Add data cell by cell.
  69. oSheet.Cells(2, 4).Value = "TEST REPORT " & first
  70. With oSheet.Range("B2", "D2")
  71. .Font.Underline = True
  72. .Font.Name = "Times New Roman"
  73. .Font.Bold = True
  74. .Font.Size = 15
  75. .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
  76. End With
  77. oSheet.Range("c2", "l2").Merge()
  78.  
  79. oSheet.Cells(2, 1).Value = " BULK Excel Report"
  80. oSheet.Range("a2", "b2").Merge()
  81. With oSheet.Range("a2", "b2")
  82.  
  83. .Font.Bold = True
  84. .Font.Name = "Times New Roman"
  85. .Font.Size = 10
  86. .VerticalAlignment = Excel.XlVAlign.xlVAlignTop
  87. End With
  88. '----------------------------------------------------------------
  89.  
  90.  
  91. oSheet.Range("a1", "k60").Interior.ColorIndex = 2 'white
  92. oSheet.Name = "Report" & (RichTextBox2.Lines(6))
  93. '----------------------------------------------------------------
  94.  
  95. oXL.Visible = True
  96.  
  97. oRng = Nothing
  98. oSheet = Nothing
  99. oWB = Nothing
  100. oXL.Quit()
  101. oXL = Nothing
  102. RichTextBox1.Text = vbNullString
  103. RichTextBox2.Text = vbNullString
  104. Exit Sub
  105. Err_Handler:
  106. MsgBox(Err.Description, vbCritical, "Error: " & Err.Number)
  107. End Sub

So if I select 10 files I want 10 sheet's to be created. I guess I have to build an Array and a loop, but I don't know how. My skills in VB.Net programming is not so good.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC