using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
//In references, right click, add reference
//Choose the COM tab
//Choose Microsoft Excel 14.0 Object Library
namespace excel
{
class Program
{
static void Main(string[] args)
{
Excel.Application excelApp = new Excel.Application();
string myPath = @"E:\temp\Book1.xls";
excelApp.Workbooks.Open(myPath);
excelApp.Visible = true;
int rowIndex = 1; int colIndex = 1;
excelApp.Sheets[1].Cells[rowIndex, colIndex] = "First";
excelApp.Sheets[1].Cells[3, 2] = 12345;
int a = Convert.ToInt32((excelApp.Sheets[2].Cells[2, 2] as Excel.Range).Value);
int b = Convert.ToInt32((excelApp.Sheets[2].Cells[3, 3] as Excel.Range).Value);
string c = (excelApp.Sheets[2].Cells[4, 4] as Excel.Range).Value;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
excelApp.Sheets[2].Cells[5, 5] = a + b;
int d = Convert.ToInt32((excelApp.Sheets[2].Cells[5, 5] as Excel.Range).Value);
Console.WriteLine(d);
Console.ReadLine();
}
}
}
http://social.msdn.microsoft.com/Forums/vstudio/en-US/ef11a193-54f3-407b-9374-9f5770fd9fd7/writing-to-excel-using-c?forum=csharpgeneral
No comments:
Post a Comment