2
// Display OpenFileDialog by calling ShowDialog method 
Nullable<bool> result = dlg.ShowDialog();

// Get the selected file name and display in a TextBox 
if (result == true)
{
    string filename = dlg.FileName;
    xpsFilePath = System.Environment.CurrentDirectory + "\\" + dlg.SafeFileName + ".xps";
    SourceUrl.Text = filename;
    SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

    ExcelFile.Load(filename).Print();
}

var convertResults = OfficeToXps.ConvertToXps(SourceUrl.Text, ref xpsFilePath);
switch (convertResults.Result)
{
    case ConversionResult.OK:
        xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(xpsFilePath, FileAccess.ReadWrite);
        documentViewer1.Document = xpsDoc.GetFixedDocumentSequence();
        officeFileOpen_Status = true;
        break;

    case ConversionResult.InvalidFilePath:
        // Handle bad file path or file missing
        break;
    case ConversionResult.UnexpectedError:
        // This should only happen if the code is modified poorly
        break;
    case ConversionResult.ErrorUnableToInitializeOfficeApp:
        // Handle Office 2007 (Word | Excel | PowerPoint) not installed
        break;
    case ConversionResult.ErrorUnableToOpenOfficeFile:
        // Handle source file being locked or invalid permissions
        break;
    case ConversionResult.ErrorUnableToAccessOfficeInterop:
        // Handle Office 2007 (Word | Excel | PowerPoint) not installed
        break;
    case ConversionResult.ErrorUnableToExportToXps:
        // Handle Microsoft Save As PDF or XPS Add-In missing for 2007
        break;
}

我正在尝试打印Excel 文件但出现此错误( system.argumentexception 宽度和高度在此行中必须为非负数( ExcelFile.Load(filename).Print()),如下面的附件在此处输入图像描述

谢谢你的帮助!

4

1 回答 1

0

这里发生的主要问题是文件无效。查看堆栈跟踪信息(在 Visual Studio 窗口的右侧,检查异常)。这反过来又试图抛出异常,因为文档的宽度和高度为(或为空或)负数。

要处理执行,文件中的 Width 和 Height 属性必须是有效的(并且是正的,大于零)值。当传递的参数(在您的情况下filename是参数)无效并且不符合语言(或 API)的法律时,将引发 ArgumentException。确保作为文件名传递的文件的属性符合ExcelFile.Load()方法参数的要求。

于 2014-12-25T08:26:09.730 回答