C# 用C语言打印Excel文件# //通过调用ShowDialog方法显示OpenFileDialog 可为空的结果=dlg.ShowDialog(); //获取所选文件名并显示在文本框中 如果(结果==真) { 字符串文件名=dlg.filename; xpsFilePath=System.Environment.CurrentDirectory+“\\”+dlg.SafeFileName+“.xps”; SourceUrl.Text=文件名; SpreadsheetInfo.SetLicense(“免费有限密钥”); 加载(文件名).Print(); } var convertResults=OfficeToXps.ConvertToXps(SourceUrl.Text,ref-xpsFilePath); 开关(convertResults.Result) { case ConversionResult.OK: xpsDoc=new System.Windows.Xps.Packaging.XpsDocument(xpsFilePath,FileAccess.ReadWrite); documentViewer1.Document=xpsDoc.GetFixedDocumentSequence(); officeFileOpen_状态=真; 打破 案例转换结果.InvalidFilePath: //处理错误的文件路径或文件丢失 打破 案例转换结果。意外错误: //只有当代码修改不当时,才会发生这种情况 打破 案例转换结果.ErrorUnableToInitializeOfficeApp: //未安装Handle Office 2007(Word | Excel | PowerPoint) 打破 案例转换结果.ErrorUnableToOpenOfficeFile: //处理源文件被锁定或权限无效 打破 案例转换结果。错误无法访问Office Interop: //未安装Handle Office 2007(Word | Excel | PowerPoint) 打破 案例转换结果.ErrorUnableToExportToXps: //2007年缺少处理Microsoft另存为PDF或XPS加载项 打破 }

C# 用C语言打印Excel文件# //通过调用ShowDialog方法显示OpenFileDialog 可为空的结果=dlg.ShowDialog(); //获取所选文件名并显示在文本框中 如果(结果==真) { 字符串文件名=dlg.filename; xpsFilePath=System.Environment.CurrentDirectory+“\\”+dlg.SafeFileName+“.xps”; SourceUrl.Text=文件名; SpreadsheetInfo.SetLicense(“免费有限密钥”); 加载(文件名).Print(); } var convertResults=OfficeToXps.ConvertToXps(SourceUrl.Text,ref-xpsFilePath); 开关(convertResults.Result) { case ConversionResult.OK: xpsDoc=new System.Windows.Xps.Packaging.XpsDocument(xpsFilePath,FileAccess.ReadWrite); documentViewer1.Document=xpsDoc.GetFixedDocumentSequence(); officeFileOpen_状态=真; 打破 案例转换结果.InvalidFilePath: //处理错误的文件路径或文件丢失 打破 案例转换结果。意外错误: //只有当代码修改不当时,才会发生这种情况 打破 案例转换结果.ErrorUnableToInitializeOfficeApp: //未安装Handle Office 2007(Word | Excel | PowerPoint) 打破 案例转换结果.ErrorUnableToOpenOfficeFile: //处理源文件被锁定或权限无效 打破 案例转换结果。错误无法访问Office Interop: //未安装Handle Office 2007(Word | Excel | PowerPoint) 打破 案例转换结果.ErrorUnableToExportToXps: //2007年缺少处理Microsoft另存为PDF或XPS加载项 打破 },c#,asp.net,wpf,gembox-spreadsheet,C#,Asp.net,Wpf,Gembox Spreadsheet,我正在尝试打印Excel文件,但出现了此错误(system.argumentexception width and height必须为非负,如下面的附件所示(ExcelFile.Load(filename).Print()) 感谢您的帮助!这里出现的主要问题是,文件无效。请查看堆栈跟踪信息(在Visual Studio窗口的右侧,检查异常)。由于文档的宽度和高度(null或)为负数,因此它会尝试引发异常 若要处理执行,文件中的宽度和高度属性必须是有效值(且为正值,大于零)。当传递的参数(在您的情

我正在尝试打印Excel文件,但出现了此错误(system.argumentexception width and height必须为非负,如下面的附件所示(ExcelFile.Load(filename).Print()


感谢您的帮助!

这里出现的主要问题是,文件无效。请查看堆栈跟踪信息(在Visual Studio窗口的右侧,检查异常)。由于文档的宽度和高度(null或)为负数,因此它会尝试引发异常


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

这是屏幕截图错误,我在问题中添加了该图像,因为它是必需的。不是在注释中。简短的回答是,您正在设置的宽度和高度是多少?@Pshtiwan,我建议您阅读此API的文档,我无法在MSDN上找到此资源,您有吗是否有第三方库?这是API
// 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;
}