Microsoft Office Tutorials and References
In Depth Information
Opening a text file as a workbook using VSTO
// Text data in Sheet1 cells
Globals.Sheet1.Cells[3, 3] = "Set my data";
}
We can also read the worksheet and write data to the cells by using the Range object.
In this case, you are creating the range and setting the text data for the range in the
Excel worksheet.
Open Visual Studio 2008 and create a new solution, as before. Write the following
code inside the ThisWorkbook.cs file. In this demonstration, you read the worksheet
through the range of cells and set the value by reading through cell ranges.
private void ThisWorkbook_Startup(object sender,
System.EventArgs e)
{
// Setting value in ExcelSheet cells through reading range object
Excel.Range AuthorExcelSheetRange = Globals.Sheet1.Range["A2",
"B2"];
// Text data for the range A2 to B2
AuthorExcelSheetRange.Value2 = "Set my data";
}
Let's see a demonstration of how to read data from an external data file and display
this inside our Excel cells. In this demonstration, you will see how the data from the
text ( .txt ) file is displayed in the spreadsheet.
Opening a text file as a workbook using VSTO
We'll now see how to open the text file as a workbook by using VSTO and C#
programming. This saves time and makes the user more comfortable in accessing
the text file while processing the data. Open Visual Studio 2008 and create a new
solution, as before. Write the following code inside the ThisWorkbook.cs file:
// Opening Text file as workbook
private void ThisWorkbook_Startup(object sender,
System.EventArgs e)
{
// In the workbook objects, file path as parameter in Opentext
property this.Application.Workbooks.OpenText(@"C:\TechBooks.txt",
// Value 1 is, the row from which it will read data in the text
file missing, 1,
 
Search JabSto ::




Custom Search