In daily office automation or system report development, it's often necessary to convert Excel files to PDF for archiving, printing, or sharing. A common challenge developers face is: how to achieve high-fidelity Excel to PDF conversion without installing Microsoft Office?
This article introduces how to use Spire.XLS for .NET to accomplish this. The library provides standalone Excel processing capabilities, accurately preserving cell styles, table layouts, charts, and images, allowing high-quality Excel to PDF conversion without relying on Office.
1. Setup
First, install the Spire.XLS library in your project via NuGet:
Install-Package Spire.XLS
Then, include the namespace in your code:
using Spire.Xls;
Spire.XLS provides the Workbook
class for loading, editing, and exporting Excel files, supporting .xls
, .xlsx
, .csv
, and more.
2. Basic Excel to PDF Conversion
The following example demonstrates the simplest way to convert an entire Excel file to PDF:
using Spire.Xls;
namespace ExcelToPDFDemo
{
class Program
{
static void Main(string[] args)
{
// Create a Workbook object and load the Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("Example.xlsx");
// Export the entire Excel file to PDF
workbook.SaveToFile("Output.pdf", FileFormat.PDF);
}
}
}
Notes
-
Workbook.LoadFromFile()
loads the Excel file. -
SaveToFile("Output.pdf", FileFormat.PDF)
saves it as a PDF. - All worksheets are exported in order into a single PDF file.
This method is ideal for quickly exporting the full Excel document.
3. Adjust Page Settings for Better Output
To ensure proper pagination and layout, you can adjust page orientation, scaling, and margins using the PageSetup
object:
Workbook workbook = new Workbook();
workbook.LoadFromFile("Example.xlsx");
Worksheet sheet = workbook.Worksheets[0];
// Set page orientation to landscape
sheet.PageSetup.Orientation = PageOrientationType.Landscape;
// Fit content width to one page
sheet.PageSetup.FitToPagesWide = 1;
sheet.PageSetup.FitToPagesTall = 0;
// Set page margins (in inches)
sheet.PageSetup.LeftMargin = 0.3;
sheet.PageSetup.RightMargin = 0.3;
workbook.SaveToFile("LandscapeOutput.pdf", FileFormat.PDF);
Tip: For wide Excel sheets, use landscape orientation and fit-to-page width to avoid content being cut off.
4. Export Specific Worksheet or Cell Range
Sometimes you only need a specific worksheet or cell range, such as a summary report. Spire.XLS allows precise control over the export range:
using Spire.Xls;
namespace WorksheetOrCellRangeToPdf
{
internal class Program
{
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Example.xlsx");
Worksheet sheet = workbook.Worksheets[0];
// Set print area (export only a specific cell range)
sheet.PageSetup.PrintArea = "B1:E6";
// Export the selected range to PDF
sheet.SaveToPdf("CellRange.pdf");
workbook.Dispose();
}
}
}
Notes
-
PrintArea
specifies the range to export (e.g.,"B1:E6"
). -
SaveToPdf()
exports the selected range. - If
PrintArea
is not set, the entire worksheet is exported.
5. Load Password-Protected Excel Files
If the Excel file is encrypted, you can provide a password when loading it:
Workbook workbook = new Workbook();
workbook.LoadFromFile("Protected.xlsx", "YourPassword");
workbook.SaveToFile("ProtectedOutput.pdf", FileFormat.PDF);
- The second parameter in
LoadFromFile()
is the password for the protected Excel file. - After loading, you can perform all standard operations, including PDF export.
6. Summary
This article demonstrates how to achieve high-fidelity Excel to PDF conversion in C# without requiring Microsoft Office using Spire.XLS for .NET. Key takeaways include:
- Quickly export entire Excel files to PDF.
- Adjust page orientation and scaling with
PageSetup
. - Export specific worksheets or cell ranges.
- Preserve charts and images with high quality.
- Load and convert password-protected Excel files.
With these techniques, you can efficiently handle Excel to PDF conversion in various development scenarios while maintaining consistent formatting and clarity.
Top comments (0)