2014-12-25 72 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