Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Open a Notepad and Excel to read-write in .NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 141
    Comment on it

    Open a Notepad and Excel to read-write in .NET

    Suppose, if we required in our application to save the data in text format or in an Excel format for the analysis purpose, then there will be a need to open the Notepad and Excel and read-write data in them.

    At this point in .net there are components that enables developers to work on it easily to read and write in those files.

    For Notepad:

    We can try in this way,

    System.IO.File.WriteAllText(@"C:\example.txt", textBox.Text);

    Here in the above example

    The File.WriteAllText Method will perform the task of opening the text file and writing the content in it and closing the same.

    The Process.Start Method will perform associating the Process component by starting the Process Resource.

    the example.txt file will get open and if it is already there it will get appended, the textBox.text will write the content in the file.

    For Excel:

    We can use a GemBox.Spreadsheet component by .net which helps in opening, writing and saving the files in different formats from our .net applications.

    We can try in this way,

    ' Loading the Excel file.
    Dim wrkbk = ExcelFile.Load("Workbook.xls")
    ' Selecting active worksheet.
    Dim wrksht = wrkbk.Worksheets.ActiveWorksheet
    ' Changing the value of the cell "A1".
    wrksht.Cells("A1").Value = "Hello!"
    ' Saving the file in XLS format.

    In the above code of example, The Worksheets.ActiveWorksheet sets the active work sheet's object of the excel. Using that object "wrkbk" we can alter or write some value on the cell by selecting the required one like as done in "A1".

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: