ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Read specific region from Text file and write in existing Excel using C# and VB.Net<p>Hey <a class="username" rel="Vikash21">Vikash21</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Reflection; using Excel = Microsoft.Office.Interop.Excel;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports Excel = Microsoft.Office.Interop.Excel Imports System.Reflection</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { string text = File.ReadAllText(Server.MapPath(&#34;~/Test.txt&#34;)); int start = text.IndexOf(&#34;Control1&#34;); int end = text.IndexOf(&#34;Control1 End&#34;, start); string result = text.Substring(start, end - start).Replace(&#34;Control1&#34;, &#34;&#34;).Replace(&#34;--&#34;, &#34;&#34;).Trim(); ReadExistingExcel(result); } public static void ReadExistingExcel(string result) { string path = @&#34;C:\Users\anand\Desktop\excelfolder\Book1.xls&#34;; Excel.Application application = new Microsoft.Office.Interop.Excel.Application(); application.Visible = true; application.DisplayAlerts = false; Excel.Workbook workbook = application.Workbooks.Open(path); Excel.Sheets sheets = workbook.Worksheets; Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(&#34;Sheet1&#34;); Excel.Range range = workSheet.UsedRange; // Inserted in the cell position. workSheet.Cells[2, 2] = result; workbook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); workbook.Close(Missing.Value, Missing.Value, Missing.Value); workSheet = null; workbook = null; application.Quit(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim text As String = File.ReadAllText(Server.MapPath(&#34;~/Test.txt&#34;)) Dim start As Integer = text.IndexOf(&#34;Control1&#34;) Dim [end] As Integer = text.IndexOf(&#34;Control1 End&#34;, start) Dim result As String = text.Substring(start, [end] - start).Replace(&#34;Control1&#34;, &#34;&#34;).Replace(&#34;--&#34;, &#34;&#34;).Trim() ReadExistingExcel(result) End Sub Public Shared Sub ReadExistingExcel(ByVal result As String) Dim path As String = &#34;C:\Users\anand\Desktop\excelfolder\Book1.xls&#34; Dim application As Excel.Application = New Microsoft.Office.Interop.Excel.Application() application.Visible = True application.DisplayAlerts = False Dim workbook As Excel.Workbook = application.Workbooks.Open(path) Dim sheets As Excel.Sheets = workbook.Worksheets Dim workSheet As Excel.Worksheet = CType(sheets.Item(&#34;Sheet1&#34;), Microsoft.Office.Interop.Excel.Worksheet) Dim range As Excel.Range = workSheet.UsedRange workSheet.Cells(2, 2) = result workbook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value) workbook.Close(Missing.Value, Missing.Value, Missing.Value) workSheet = Nothing workbook = Nothing application.Quit() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshots</strong></span></p> <p><span style="text-decoration: underline;">Existing Excel</span></p> <p><img src="https://i.imgur.com/sePWaYo.jpg" alt="" width="401" height="370" /></p> <p><span style="text-decoration: underline;">After Inserting text file data in excel</span></p> <p><img src="https://i.imgur.com/ei4rXsr.jpg" alt="" width="472" height="289" /></p>https://www.aspforums.net:443/Threads/118044/Read-specific-region-from-Text-file-and-write-in-existing-Excel-using-C-and-VBNet/https://www.aspforums.net:443/Threads/118044/Read-specific-region-from-Text-file-and-write-in-existing-Excel-using-C-and-VBNet/Tue, 20 Nov 2018 00:46:23 GMT