c# - 使用 C# 打印文件 Excel

标签 c# asp.net wpf gembox-spreadsheet

// 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 width and height must be non-negative (ExcelFile.Load (文件名).Print()) 点赞下面的附件 enter image description here

谢谢你的帮助!

最佳答案

这里的主要问题是文件无效。查看堆栈跟踪信息(在 Visual Studio 窗口的右侧,检查异常)。这反过来又试图抛出异常,因为文档的宽度和高度为(空或)负数。

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

关于c# - 使用 C# 打印文件 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644771/

相关文章:

c# - 如何从 asp.net 中的数据库填充 'select'

c# - 将 'var' 传递给另一个方法

c# - 如何从 BeginInvoke 返回 T 值?

c# - 使用 JQuery 和 Cookie 插件创建一个非 url 编码值的 cookie

c# - WPF 文本 block 不会在运行时显示属性更改

WPF ObservableCollection.Remove 抛出 NullReferenceException

c# - Microsoft.NETCore.UniversalWindowsPlatform 引用的用途是什么?

c# - 为什么 DropDownList.SelectedItem.Value 不显示赋值

c# - 如何在 VS2010 的新 web.config 配置中使用 applicationSettings?

WPF TreeView,在 PreviewMouseDown 事件中获取 TreeViewItem