4

I am trying to create an Excel file and write some data to it. Here is the code I am using.

using excel = Microsoft.Office.Interop.Excel;
excel.Application xlapp;
excel.Workbook xlworkbook;
excel.Worksheet xlworksheet;
object misvalue = System.Reflection.Missing.Value;

xlapp = new excel.ApplicationClass();
xlworkbook = xlapp.Workbooks.Add(misvalue);

xlworksheet = (excel.Worksheet)xlworkbook.Worksheets.get_Item(1);
xlworksheet.Cells[1, 1] = "Muneeb Hassan Soomro";
xlworkbook.SaveAs("csharp-excelwrite.xls",excel.XlFileFormat.xlWorkbookNormal,misvalue,misvalue,misvalue,misvalue,excel.XlSaveAsAccessMode.xlExclusive,misvalue,misvalue,misvalue,misvalue,misvalue);
//xlworkbook.SaveAs("csharp-Excel.xls", excel.XlFileFormat.xlWorkbookNormal);
xlworkbook.Close(true, misvalue, misvalue);
xlapp.Quit();

I get an exception on the xlworkbook.saveas() call. says:

The file name or path doesn't exist or used by other program

What i am doing wrong here?

8
  • So, do you get any exception or what? Commented Jul 11, 2013 at 6:41
  • Is there a reason you are not just making a csv?
    – Meiyoki
    Commented Jul 11, 2013 at 6:41
  • You forgot to ask a question. Commented Jul 11, 2013 at 6:43
  • yes i am getting exception on the function of saveas. I am actually learning how to write a file in excel! Commented Jul 11, 2013 at 6:45
  • @Meiyoki i have already exported the data to csv file. But want to learn excel file writing using interop library. Commented Jul 11, 2013 at 6:57

4 Answers 4

2

So from one of your comments on another answer I finally got the exception text (This information should have been included in the question!)

The file name or path doesn't exist or used by other program

The solution should be simple: Specify a full path in the SaveAs call, not only a file name. How should Excel know in which folder it should save the file otherwise?

4
  • you are right @cremon
    – Rezoan
    Commented Jul 11, 2013 at 8:06
  • @cremor you are right, i gave the path and now it is making an excel file! Commented Jul 11, 2013 at 8:35
  • You should mark it as answer so that other having the same issue can find it helpful @Muneeb Hassan
    – Rezoan
    Commented Jul 11, 2013 at 9:22
  • i was about to mark it but went away! Although thanks for reminding! Commented Jul 11, 2013 at 10:37
1

Try to change this line

xlworkbook = xlapp.Workbooks.Add(misvalue);

to this line:

xlworkbook = xlapp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);

and the SaveAs:

workBook.SaveAs("csharp-excelwrite.xls", XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
3
  • still getting exception The file name or path doesn't exist or used by other program Commented Jul 11, 2013 at 7:29
  • Maybe your file is open by Excel, in task panel in the processes tab close all of the open tasks and change this line: xlworkbook.Close(true, misvalue, misvalue); to this: workBook.Close(true, "csharp-excelwrite.xls", null);
    – dvjanm
    Commented Jul 11, 2013 at 7:37
  • this solution is working - tested just now
    – sqladmin
    Commented Jul 11, 2013 at 7:38
0

So i suggest you to use excellibrary for Excel file writing

You can find a details on Create Excel (.XLS and .XLSX) file from C#

2
  • i have to add this project to my project to use its functionality? Commented Jul 11, 2013 at 6:59
  • you just need to add the reference of this dll in your project.
    – Rezoan
    Commented Jul 11, 2013 at 7:15
0

try this

object format = excel.XlFileFormat.xlWorkbookNormal;
object sv = excel.XlSaveAsAccessMode.xlExclusive;
object filename = "csharp-excelwrite.xls";

xlworkbook.SaveAs(ref filename,ref format,
ref misvalue,ref misvalue,ref misvalue,
ref misvalue,ref sv,ref misvalue,ref misvalue,
ref misvalue,ref misvalue,ref misvalue);

it is working for me with Word application for example

object readOnly = isReadonly;
object isVisible = true;
object missing = WordConst.Missing;
wordDoc = wordApp.Documents.Open(ref fileName, ref missing,
                          ref readOnly, ref missing, ref missing, ref missing,
                          ref missing, ref missing, ref missing, ref missing,
                          ref missing, ref isVisible, ref missing, ref missing, 
                          ref missing, ref missing);
2
  • giving error. Argument should not be passed with the ref key word. Commented Jul 11, 2013 at 7:22
  • well, it works with MS Word in my application
    – sqladmin
    Commented Jul 11, 2013 at 7:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.