// 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()) 点赞下面的附件
谢谢你的帮助!
最佳答案
这里的主要问题是文件无效。查看堆栈跟踪信息(在 Visual Studio 窗口的右侧,检查异常)。这反过来又试图抛出异常,因为文档的宽度和高度为(空或)负数。
要处理执行,文件中的 Width 和 Height 属性必须是有效的(并且是正数,大于零)值。当传递的参数(在您的情况下 filename
是参数)无效且不符合语言(或 API)的法律时,会引发 ArgumentException。确保作为文件名传递的文件的属性符合 ExcelFile.Load()
方法的参数要求。
关于c# - 使用 C# 打印文件 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27644771/