XLS到pdf转换器-意外错误

本文关键字:意外 错误 转换器 pdf XLS | 更新日期: 2023-09-27 17:54:14

我正在使用SautinSoft.XlsToPdf.dll将xml转换为pdf。它在控制台应用程序中正常工作,但在web应用程序中抛出错误"对象引用未设置为对象的实例",我不知道为什么。

 protected void Page_Init(object sender, EventArgs e)
    {
        byte[] pdfBytes = FromExcel(Server.MapPath("~/Doc/PlanningGame.xlsx"));
        Response.AppendHeader("content-disposition", "attachment;    filename=PlanningGame.pdf");
        Response.ContentType = "application/octet-stream";
        Response.Write(pdfBytes);
        Response.Flush();
        Response.End();
    }

    public static byte[] FromExcel(string xlsFile)
    {
        XlsToPdf xtop = new SautinSoft.XlsToPdf();
        byte[] pdfBytes = null;
        xtop.ConvertBytes(File.ReadAllBytes(xlsFile), ref pdfBytes);//"Object reference not set to an instance of an object." 
        string tempFileName = string.Format("{0}{1}", Config.TempDirectory, Guid.NewGuid().ToString());
        var bw = new BinaryWriter(new FileStream(tempFileName, FileMode.CreateNew));
        bw.Write(pdfBytes);
        bw.Close();
        return pdfBytes;
    }

File.ReadAllBytes(xlsFile)返回一个字节数组,所以它不是空的。Xtop也不为空

更新:如果xlsFile位于web应用程序目录中,则抛出错误。如果我用这样的

 byte[] pdfBytes = null;
 xtop.ConvertBytes(File.ReadAllBytes("c:''ExcelSheet.xls"), ref pdfBytes);

错误不会抛出。

任何想法?

XLS到pdf转换器-意外错误

安全?我猜你的服务器被阻止访问包含文档的目录。


EDIT首先让我们检查一下我的猜测:

public static byte[] FromExcel(string xlsFile)
{
    XlsToPdf xtop = new SautinSoft.XlsToPdf();
    byte[] xlsBytes = File.ReadAllBytes(xlsFile);
    // is xlsBytes null? put a break-point here, or debug-print statement if you can't debug a running server.
    byte[] pdfBytes = null; 
    xtop.ConvertBytes(xlsBytes, ref pdfBytes);
    string tempFileName = string.Format("{0}{1}", Config.TempDirectory, Guid.NewGuid().ToString());
    var bw = new BinaryWriter(new FileStream(tempFileName, FileMode.CreateNew));
    bw.Write(pdfBytes);
    bw.Close();
    return pdfBytes;
}