2014-12-25 73 views
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(文件名)。打印())像enter image description here打印文件的Excel C#

感謝幫助下這個附件我!

+0

這是屏幕截圖錯誤 http://i.stack.imgur.com/Kg9Z8.png – Pshtiwan

+0

我在問題中添加了該圖像,因爲它在那裏是必需的。不在評論中。對此的簡短回答是,您設置的寬度和高度是多少? –

回答

0

的主要問題發生在這裏是,該文件是無效的。期待中的堆棧跟蹤信息(右側在Visual Studio中無線ndow,檢查例外)。這反過來又會嘗試拋出異常,因爲文檔的寬度和高度都是(或者爲空)或負值。

要處理執行,文件中的寬度和高度屬性必須是有效的(且爲正數,大於零)值。參數傳遞時引發ArgumentException(在您的案例中爲filename是參數)無效且不符合語言(或API)的規律。確保作爲文件名傳遞的文件的屬性符合ExcelFile.Load()方法的參數要求。

+0

謝謝你Afzall艾哈邁德:) – Pshtiwan

+0

@Pshtiwan,我想建議你閱讀這個API的文檔,我無法在MSDN上找到這個資源,你使用了一些第三方庫嗎? –

+0

這是API http://www.gemboxsoftware.com/SampleExplorer/Spreadsheet/AdvancedFeatures/PrintandViewOptions?tab=cs – Pshtiwan