我使用Web窗体。我尝试使用Spire.XLS将ClosedXML excel工作簿转换为pdf并由客户端下载。 这是我的代码:
protected void btnExport_OnClick(object sender, EventArgs e)
{
var excel = Reporting.GenerateExcel(); //custom, return XLWorkbook object
string myName = Server.UrlEncode(121 + "_" + DateTime.Now.ToShortDateString() + ".xlsx");
MemoryStream stream = GetStream(excel);
MemoryStream stream1 = new MemoryStream();
Workbook book = new Workbook();
book.LoadFromStream(stream);
book.SaveToStream(stream1, FileFormat.PDF);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + myName);
Response.ContentType = "application/pdf";
Response.BinaryWrite(stream1.ToArray());
Response.End();
}
public MemoryStream GetStream(XLWorkbook excelWorkbook)
{
MemoryStream fs = new MemoryStream();
excelWorkbook.SaveAs(fs);
fs.Position = 0;
return fs;
}
好像我收到了不正确的pdf。感谢