c# - Why does the supposedly-saved file not really get saved? -
the following code taken this tutorial create , save excel file c#:
using excel = microsoft.office.interop.excel; namespace windowsformsappexceltest { public partial class form1 : form { public form1() { initializecomponent(); } private void buttoncreateexcelfile_click(object sender, eventargs e) { excel.application xlapp; excel.workbook xlworkbook; excel.worksheet xlworksheet; object misvalue = system.reflection.missing.value; xlapp = new excel.application(); xlworkbook = xlapp.workbooks.add(misvalue); xlworksheet = (excel.worksheet)xlworkbook.worksheets.get_item(1); xlworksheet.cells[1, 1] = "http://csharp.net-informations.com"; xlworkbook.saveas("csharp-excel.xls", excel.xlfileformat.xlworkbooknormal, misvalue, misvalue, misvalue, misvalue, excel.xlsaveasaccessmode.xlexclusive, misvalue, misvalue, misvalue, misvalue, misvalue); xlworkbook.close(true, misvalue, misvalue); xlapp.quit(); releaseobject(xlworksheet); releaseobject(xlworkbook); releaseobject(xlapp); messagebox.show("excel file created , can find file c:\\csharp-excel.xls"); } private void releaseobject(object obj) { try { system.runtime.interopservices.marshal.releasecomobject(obj); obj = null; } catch (exception ex) { obj = null; messagebox.show("exception occured while releasing object " + ex.tostring()); } { gc.collect(); } } } // class } // namespace
it seems run fine -- see messagebox msg, , can step through no signs of problems. file not saved hard drive, should. why not?
saving specific non-root location (a subfolder) works, e.g. this:
string savefullpath = @"c:\misc\csharpexceltest.xls"; xlworkbook.saveas(savefullpath, excel.xlfileformat.xlworkbooknormal, misvalue, misvalue, misvalue, misvalue, excel.xlsaveasaccessmode.xlexclusive, misvalue, misvalue, misvalue, misvalue, misvalue);
the file indeed saved spot.
apparently problem trying save c/root. attempting explicitly save root, this:
string savefullpath = @"c:\csharpexceltest.xls";
...(as opposed providing bare filename) fails ignominiously, telling me:
system.runtime.interopservices.comexception unhandled helplink=c:\program files (x86)\microsoft office\office12\1033\xlmain11.chm hresult=-2146827284 message=microsoft office excel cannot access file 'c:'. there several possible reasons: • file name or path not exist. • file being used program. • workbook trying save has same name open workbook. . . .
so: save somewhere other root avoid problem.
update
also, seems being saved "documents library" when not providing folder/full path, , assuming save c:. happened folder morning, , saw file there.
Comments
Post a Comment