Hello Community,
I was wondering if there is a way to make a program that will copy text from an excel spreatsheet and place it in another using visualbasic.net not vba?
Please Help
Hello Community,
I was wondering if there is a way to make a program that will copy text from an excel spreatsheet and place it in another using visualbasic.net not vba?
Please Help
Brief overview of practical approaches and a minimal, copy-ready example.
Common choices
Minimal Interop example (copies values from a source range to a destination workbook). Add a reference to "Microsoft Excel XX.X Object Library" and import the Interop namespace.
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Public Sub CopyValuesFromExcel(sourcePath As String, destPath As String, Optional sheetIndex As Integer = 1, Optional sourceRange As String = "A1:C10", Optional destAddress As String = "A1")
Dim xl As Excel.Application = Nothing
Dim srcWb As Excel.Workbook = Nothing
Dim dstWb As Excel.Workbook = Nothing
Dim srcWs As Excel.Worksheet = Nothing
Dim dstWs As Excel.Worksheet = Nothing
Dim rng As Excel.Range = Nothing
Try
xl = New Excel.Application()
xl.Visible = False
srcWb = xl.Workbooks.Open(sourcePath, ReadOnly:=True)
dstWb = xl.Workbooks.Open(destPath)
srcWs = CType(srcWb.Worksheets(sheetIndex), Excel.Worksheet)
dstWs = CType(dstWb.Worksheets(sheetIndex), Excel.Worksheet)
rng = srcWs.Range(sourceRange)
Dim values = rng.Value2
If values IsNot Nothing AndAlso TypeOf values Is System.Array Then
Dim arr = CType(values, Object(,))
dstWs.Range(destAddress).Resize(arr.GetLength(0), arr.GetLength(1)).Value2 = arr
Else
dstWs.Range(destAddress).Value2 = values
End If
dstWb.Save()
Finally
If srcWb IsNot Nothing Then srcWb.Close(False)
If dstWb IsNot Nothing Then dstWb.Close(True)
If xl IsNot Nothing Then xl.Quit()
If rng IsNot Nothing Then Marshal.ReleaseComObject(rng)
If srcWs IsNot Nothing Then Marshal.ReleaseComObject(srcWs)
If dstWs IsNot Nothing Then Marshal.ReleaseComObject(dstWs)
If srcWb IsNot Nothing Then Marshal.ReleaseComObject(srcWb)
If dstWb IsNot Nothing Then Marshal.ReleaseComObject(dstWb)
If xl IsNot Nothing Then Marshal.ReleaseComObject(xl)
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub Practical notes and troubleshooting
Copy/PasteSpecial for formats or formulas.Jump to Post— Pgmer 50you need to write a program to read excel file using .net and use the same application to write back into another excel.
Jump to Post— Pgmer 50you mean you want the complete code? or just an idea?
you need to write a program to read excel file using .net and use the same application to write back into another excel.
I know that. I'm asking how?
you mean you want the complete code? or just an idea?
Maby a bit of the code just to start me off
Hi,
Do you mean manipulate Excel without it being installed on the machine or do you just want a way to do it in .net as opposed to vba?
If you don't mind excel being installed on the machine, then you can use Interop. Here is a sample:
As for the other, I'm not 100% sure, I did do a project where I was trying to manipulate newer versions of excel as they are basically compressed XML files but I didn't get very far before we had to pull the plug.
I posted code in Retrieve and save to another excel workbook a while back which should get you started.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.