利用Aspose.Pdf将Excel表单转换成PDF文档

原创|其它|编辑:郝浩|2011-12-22 20:53:11.000|阅读 3680 次

概述:本文主要利用Aspose.Pdf和Aspose.Cells将Excel表单转换成PDF文档。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  使用Aspose.PdfAspose.Cells可将Excel表单转换成PDF文档。Excel表单中的所有内容都能被转换成PDF文件格式。此外,如果你只需要提取Excel表单中的内容,并将其以表的方式展示于PDF文档中,这些都是可以的。通过这种方法,你甚至可以指定PDF文档中表的格式,例如,设置表中行的背景颜色,指定单元格字体大小和颜色等等。

  在下面的例子中,我将使用到一个包含了5行3列的Excel表单。DataTable类提供了将表单第一行作为ColumnName的功能。在下面指定的代码中,我们将使用Aspose.Cells来打开Excel表单。在下面的例子中,我们将从工作薄的第一张表单中读取数据,通过Aspose.Cells的ExportDataTable(….)方将数据导出到DataTable对象。

  创建一个PDF文件,然后创建一个表对象,并把它添加到PDF部分的Paragraphs collection。从DataTable对象中导入数据之前,首先,我们将定义表的格式,然后使用Aspose.Pdf 中的ImportDataTable(…)方法将数据从DataTable中导入到PDF表中。

  你也可以将对象数组和DataView的数据导入到由Aspose.Pdf生成的表中。下图便是Excel中的数据展现。

  我们将使用下面的代码片段从Excel表中提取数据,并将数据展示在PDF文档的表中。

[C#]

Workbook workbook = new Workbook();

//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("d:\\pdftest\\newBook1.xls", FileMode.Open);
//Opening the Excel file through the file stream
workbook.Open(fstream);

//Accessing the first worksheet in the Excel file

Worksheet worksheet = workbook.Worksheets[0];

//Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable

DataTable dataTable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, true);

//Closing the file stream to free all resources

fstream.Close();

 

//Instantiate a Pdf instanc

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();

//Create a section in the Pdf instance

Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();

 

//Create a Table object

Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table();

 

//Add the Table object in the paragraphs collection of the section

sec1.Paragraphs.Add(tab1);

 

//Set column widths of the table. We need to specify the ColumnCount manually.

// As the curent excel worksheet has three columsn, so we are specifying the same count

tab1.ColumnWidths = "40 100 100";

 

//Set default cell border of the table using BorderInfo object

tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int) Aspose.Pdf.Generator.BorderSide.All,0.1F);

 

//Import data into the Table object from the DataTable created above

tab1.ImportDataTable(dataTable, true, 0, 0, dataTable.Rows.Count+1, dataTable.Columns.Count);

 

//Get 1st row from the table

Aspose.Pdf.Generator.Row row1 = tab1.Rows[0];

 

//Iterate through all cells in the 1st row and set their background color to blue

foreach (Aspose.Pdf.Generator.Cell curCell in row1.Cells)

{

// set the background of all the cells in 1st row of the table.

curCell.BackgroundColor = new Aspose.Pdf.Generator.Color("Blue");

// set the font face for the cells of 1st row in the table.

curCell.DefaultCellTextInfo.FontName = "Helvetica-Oblique";

// set the font Color of all the cells in 1st row of the table.

curCell.DefaultCellTextInfo.Color = new Aspose.Pdf.Generator.Color("Yellow");

// Set the text allignment for the cells of 1st row as Center.

curCell.DefaultCellTextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;

}

 

for (int All_Rows = 1; All_Rows <= dataTable.Rows.Count; All_Rows++)

{

//Iterate through all cells in the 1st row and set their background color to blue

foreach (Aspose.Pdf.Generator.Cell curCell in tab1.Rows[All_Rows].Cells)

{

// set the background color of all the cells except of the 1st row.

curCell.BackgroundColor = new Aspose.Pdf.Generator.Color("Gray");

// set the Text color of all the cells except the 1st row.

curCell.DefaultCellTextInfo.Color = new Aspose.Pdf.Generator.Color("White");

}

}

 

//Save the Pdf

pdf1.Save(@"d:\pdftest\Exceldata_toPdf_table.pdf");

[VB.NET]

Dim workbook As Workbook = New Workbook()

'Creating a file stream containing the Excel file to be opened

Dim fstream As FileStream = New FileStream("d:\\pdftest\\newBook1.xls", FileMode.Open)

'Opening the Excel file through the file stream

workbook.Open(fstream)

'Accessing the first worksheet in the Excel file

Dim worksheet As Worksheet = workbook.Worksheets(0)

'Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable

Dim dataTable As DataTable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, True)

'Closing the file stream to free all resources

fstream.Close()

 

'Instantiate a Pdf instanc

Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()

'Create a section in the Pdf instance

Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()

 

'Create a Table object

Dim tab1 As Aspose.Pdf.Generator.Table = New Aspose.Pdf.Generator.Table()

 

'Add the Table object in the paragraphs collection of the section

sec1.Paragraphs.Add(tab1)

 

'Set column widths of the table. We need to specify the ColumnCount manually.

' As the curent excel worksheet has three columsn, so we are specifying the same count

tab1.ColumnWidths = "40 100 100"

 

'Set default cell border of the table using BorderInfo object

tab1.DefaultCellBorder = New Aspose.Pdf.Generator.BorderInfo(Aspose.Pdf.Generator.BorderSide.All, 0.1F)

 

'Import data into the Table object from the DataTable created above

tab1.ImportDataTable(dataTable, True, 0, 0, dataTable.Rows.Count + 1, dataTable.Columns.Count)

 

'Get 1st row from the table

Dim row1 As Aspose.Pdf.Generator.Row = tab1.Rows(0)

 

'Iterate through all cells in the 1st row and set their background color to blue

 

For Each curCell As Aspose.Pdf.Generator.Cell In row1.Cells

 

' set the background of all the cells in 1st row of the table.

curCell.BackgroundColor = New Aspose.Pdf.Generator.Color("Blue")

' set the font face for the cells of 1st row in the table.

curCell.DefaultCellTextInfo.FontName = "Helvetica-Oblique"

' set the font Color of all the cells in 1st row of the table.

curCell.DefaultCellTextInfo.Color = New Aspose.Pdf.Generator.Color("Yellow")

' Set the text allignment for the cells of 1st row as Center.

curCell.DefaultCellTextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center

Next

Dim All_Rows As Integer

For All_Rows = 1 To dataTable.Rows.Count

 

'Iterate through all cells in the 1st row and set their background color to blue

For Each curCell As Aspose.Pdf.Generator.Cell In tab1.Rows(All_Rows).Cells

 

' set the background color of all the cells except of the 1st row.

curCell.BackgroundColor = New Aspose.Pdf.Generator.Color("Gray")

' set the Text color of all the cells being added to the table except the 1st row.

curCell.DefaultCellTextInfo.Color = New Aspose.Pdf.Generator.Color("White")

Next

Next All_Rows

'Save the Pdf

pdf1.Save("d:\pdftest\Exceldata_toPdf_table.pdf")


(慧都控件网版权所有,转载请注明出处,否则追究法律责任)
标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP