Linking Excel In C# -
so i'm working in microsoft visual studio 2015 , want able link project excel spreadsheet.
what want achieve this; able create program can use number in specific cell , calculations it. example, take cell d4 , create poisson distribution using value mean.
you need add reference microsoft.office.core , microsoft.office.interop.excel in projects references. add references class.
using microsoft.office.interop.excel; using excel = microsoft.office.interop.excel;
then can open/create/manipulate excel sheets in code. lots of info on web.
update comment--
this example opening new worksheet save when done.
excel.application xlapp; excel.workbook myworkbook; excel.worksheet myworksheet; object misvalue = system.reflection.missing.value; xlapp = new excel.applicationclass(); myworkbook = xlapp.workbooks.add(misvalue); myworksheet = (excel.worksheet)myworkbook.worksheets.get_item(1);
done work on sheet
myworksheet.cells[curxlrow, 5] = "pf"; myworksheet.cells[curxlrow, 6] = "pa";
then save file
string filename = "c:\\myfilename.xlsx"; myworkbook.saveas(filename, excel.xlfileformat.xlworkbookdefault, misvalue, misvalue, misvalue, misvalue, excel.xlsaveasaccessmode.xlexclusive, misvalue, misvalue, misvalue, misvalue, misvalue); myworkbook.close(true, misvalue, misvalue); xlapp.quit(); releaseobject(myworksheet); releaseobject(myworkbook); releaseobject(xlapp);
hope helps. dont have example open existing excel file. up:
xlapp.workbooks.open(lots of parameters...);
Comments
Post a Comment