原創|其它|編輯:郝浩|2011-12-22 20:53:11.000|閱讀 3697 次
概述:本文主要利用Aspose.Pdf和Aspose.Cells將Excel表單轉換成PDF文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
使用Aspose.Pdf和Aspose.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@fc6vip.cn
文章轉載自:慧都控件網