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 ウィンドウの右側で、例外を確認します)。ドキュメントの幅と高さが (null または) 負であるため、例外をスローしようとします。

実行を処理するには、ファイルの Width プロパティと Height プロパティが有効な (ゼロより大きい正の) 値である必要があります。渡されたパラメーター (この場合filenameはパラメーター) が無効であり、言語 (または API) の法則に準拠していない場合、ArgumentException が発生します。ファイル名として渡されるファイルのプロパティが、ExcelFile.Load()メソッドのパラメーターの要件に従っていることを確認してください。

于 2014-12-25T08:26:09.730 に答える