没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
原创|其它|编辑:郝浩|2011-12-22 20:53:11.000|阅读 3680 次
概述:本文主要利用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@evget.com
文章转载自:慧都控件网面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
专业的电子表格控件,无需MS Excel也可满足一切Excel表格功能。
Aspose.PDF for .NETPDF文档创建组件,无需Adobe Acrobat,也可以在任何平台上操作PDF文档。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号