【教程】Excel组件Spire.XLS 教程:如何在C#,VB.NET将CSV转换为PDF

时间:2022-08-12 14:01:06

Spire.XLS支持加载CSV文件并保存为Excel或PDF文件。本教程将介绍如何使用带有C#和VB.NET的Spire.XLS将CSV转换为PDF。

示例CSV文件如下所示:

【教程】Excel组件Spire.XLS 教程:如何在C#,VB.NET将CSV转换为PDF

Step 1:

Workbook wb = new Workbook();
wb.LoadFromFile("SampleCSVFile.csv", ",",1,1);

Step 2:

wb.ConverterSetting.SheetFitToPage = true;

Step 3:

Worksheet sheet = wb.Worksheets[0];
for (int i = 1; i < sheet.Columns.Length; i++)
{
sheet.AutoFitColumn(i);
}

Step 4:

sheet.SaveToPdf("toPDF.pdf");

输出:

【教程】Excel组件Spire.XLS 教程:如何在C#,VB.NET将CSV转换为PDF

完整代码:

[C#]

Workbook wb = new Workbook();
wb.LoadFromFile("SampleCSVFile.csv", ",",1,1);
wb.ConverterSetting.SheetFitToPage = true;
Worksheet sheet = wb.Worksheets[0];
for (int i = 1; i < sheet.Columns.Length; i++)
{
sheet.AutoFitColumn(i);
}
sheet.SaveToPdf("toPDF.pdf");

[VB.NET]

Dim wb As New Workbook()
wb.LoadFromFile("SampleCSVFile.csv", ",", 1, 1)
wb.ConverterSetting.SheetFitToPage = True
Dim sheet As Worksheet = wb.Worksheets(0)
For i As Integer = 1 To sheet.Columns.Length - 1
sheet.AutoFitColumn(i)
Next
sheet.SaveToPdf("toPDF.pdf")

点击下载Spire.XLS