找不到路径excel错误的一部分

时间:2016-04-12 11:42:38

标签: c# asp.net excel

我想将datatable绑定到excel。我已经为此编写了以下代码

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_Overdue.TableName = "Daily Lead";
     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";
     ExcelLibrary.DataSetHelper.CreateWorkbook("~/Attachment/Employee_lwpc_Details.xls", DS_lwpc);

}

但我收到错误

  

无法找到路径'C:\ Program Files(x86)\ Common Files \ Microsoft Shared \ DevServer \ 10.0 \〜\ Attachment \ Employee_lwpc_Details.xls'的一部分。

在最后一行。我不知道为什么

1 个答案:

答案 0 :(得分:1)

用户Server.MapPath并获取与您的网站运行位置相关的目录。

var directory = Server.MapPath("~/Attachment/");
ExcelLibrary.DataSetHelper.CreateWorkbook(directory + "Employee_lwpc_Details.xls", DS_lwpc);

这假设Attachment目录存在,否则您可能需要创建它。