1

My requirement is to open an excel file which is under Attachment folder. So I took the reference from here and used abatishchev answer for my requirement. So I tried that in my below code.

public void ExportExcel()
{
   string str_lwpc_query = string.Empty;
   str_lwpc_query = "select company_name 'COMPANY NAME',Deputed_Company_Name 'DEPUTED COMPANY NAME',emp_card_no 'EMP CODE',emp_name 'EMPLOYEE NAME',LWP,'' Remarks,  " +
                    "Adj_Days Gain_Loss_LOP_Days, VAL_DAY LOP_Days_Desc, month, year from XXACL_EMP_INFO_LWP_OTDAYS_HRS_V " +
                     "where emp_type='C' and month = '3' and year = '2015' ";
    DataTable Dt_lwpc = new DataTable();
    Dt_lwpc = CF.ExecuteDT(str_lwpc_query);
    DataSet DS_lwpc = new DataSet();
    DS_lwpc.Tables.Add(Dt_lwpc);
    DS_lwpc.Tables[0].TableName = "Employee loss of pay for consultant Details";
    var directory = Server.MapPath("~/Attachment/");
    ExcelLibrary.DataSetHelper.CreateWorkbook(directory + "Employee_lwpc_Details.xls", DS_lwpc);

    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
    ExcelLibrary.Office.Excel.Workbook wb = excel.Workbooks.Open(ExcelLibrary.DataSetHelper.CreateWorkbook(directory + "Employee_lwpc_Details.xls", DS_lwpc));
}

but I get error in the last line as

The best overloaded method match for Microsoft.Office.Interop.Excel.Workbooks.Open(string, object, object, object, ...........) has some invalid arguments

kindly suggest what is wrong

10
  • @DmitryRotay: tried got error as cannot implicitly convert type Microsoft.Office.Interop.Excel to ExcelLibrary.Office.Excel.Workbook an explicit conversion exists ( are you missing a cast) Commented Apr 13, 2016 at 6:14
  • Sorry, deleted my comment accidentally. You are trying to open file with Microsoft's Excel interop library - the result returned is of type Microsoft.Office.Interop.Excel.Workbook. but are expecting result type to be of some custom library - ExcelLibrary.Office.Excel.Workbook. Commented Apr 13, 2016 at 6:18
  • @DmitryRotay: so what's the solution for this ? Commented Apr 13, 2016 at 6:19
  • If you really want to use excel interop to open workbook and work with it, then you need to replace the last line with this: Microsoft.Office.Interop.Excel.Workbook wb = excel.Workbooks.Open(ExcelLibrary.DataSetHelper.CreateWorkbook(directory + "Employee_lwpc_Details.xls", DS_lwpc)); Commented Apr 13, 2016 at 6:25
  • i get the same error as mentioned in the question now Commented Apr 13, 2016 at 6:26

2 Answers 2

2

If you want users of your website to open an excel file that you've created on server, than you don't need to open it there - just send it to the user. Replace the last two lines of code with this:

string filePath = directory + "Employee_lwpc_Details.xls"; 
Response.ContentType = "Application/vnd.ms-excel"; 
Response.AppendHeader("content-disposition", 
"attachment; filename=" + "Employee_lwpc_Details.xls"); 
Response.TransmitFile(filePath); 
Response.End();
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot Dmitry for the patience and resolving the issue step by step. Happy coding :)
0

Microsoft.Office.Interop.Excel.Workbooks.Open takes a filename and a number of additional parameters, see the MSDN documentation here. The CreateWorkbook method that you're calling is not returning the required arguments for Open.

7 Comments

so waht would be my code according to this ? kindly update
Have you tried just using ExcelLibrary.Office.Excel.Workbook wb = excel.Workbooks.Open(directory + "Employee_lwpc_Details.xls"));? If I'm understanding your code correctly, that would open the Workbook you created a few lines up.
the above code just places the file in the attachment folder but it doesn't opens the file
ExcelLibrary.DataSetHelper.CreateWorkbook(directory + "Employee_lwpc_Details.xls", DS_lwpc); places the file in the attachment folder, but ExcelLibrary.Office.Excel.Workbook wb = excel.Workbooks.Open(directory + "Employee_lwpc_Details.xls")); opens that file and assigns a local variable to it.
that's what I also know,but why it is not opening the file then ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.