Printing File Excel using C#

1.3k views Asked by At
// 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;
}

I am try for printing Excel file But this error occur ( system.argumentexception width and height must be non-negative in this line (ExcelFile.Load(filename).Print()) like this attachment belowenter image description here

thanks for help me!

1

There are 1 answers

2
Afzaal Ahmad Zeeshan On BEST ANSWER

The main problem here occurs to be, that the file is invalid. Look in the Stack Trace information (Right side in your Visual Studio Window, check the Exceptions). Which in turn tries to throw an exception because the Width and Height of the document are (either null or) negative.

To process the execution, the Width and Height properies in your file must be a valid (and positive, greater than zero) value. ArgumentException is raised when the parameter passed (in your case filename is the parameter) is invalid and not conforming to the laws of the language (or the API). Make sure that the properties of the file that is being passed as filename are according to the requirements for the parameter for ExcelFile.Load() method.