我正在使用外部DLL来创建Excel文件。它在网页中非常有效,但是当我通过Web服务使用它时,它不会给任何错误,但也没有创建任何文件。这是Web服务和Excel导出函数的代码。
[WebMethod, System.Web.Script.Services.ScriptMethod]
public static string DownloadProfile(string profileids)
{
Result pro = new Result();
if (profileids == "")
pro = new Result(0, false, "No Profile to download", "");
else
{
candidate_search cs = new candidate_search();
string SqlProfile = "query";
try
{
cs.ExportExcel(SqlProfile);
pro = new Result(0, true, "CV Downloaded Successfully !", "");
}
catch {
//pro = new Result(0, false, "No Profile to download", "");
}
}
string json = JsonConvert.SerializeObject(pro, Newtonsoft.Json.Formatting.Indented);
return json;
}
public void ExportExcel(string SQLqueryProfile)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection ConProfile = new SqlConnection(constr);
SqlCommand CmdProfile = new SqlCommand(SQLqueryProfile);
SqlDataAdapter SdaProfile = new SqlDataAdapter();
CmdProfile.Connection = ConProfile;
SdaProfile.SelectCommand = CmdProfile;
DataTable DtProfile = new DataTable();
SdaProfile.Fill(DtProfile);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(DtProfile, "Profile");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=dataExport.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
由于没有错误,因此可以做两件事:
- 调试。也许有一个错误正在被扔和吞咽?
- 我看到您将文件写入响应。将其写入磁盘,看看是否有效。