Important alert: (current site time 7/16/2013 7:32:07 AM EDT)
 

VB icon

Convert Each Worksheet of an Excel File to a Different PDF File

Email
Submitted on: 8/24/2011 11:33:35 AM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2711
 
     This technical tip shows how use Aspose.Cells for converting XLS files (that contain images, charts etc.) to PDF documents. Aspose.Cells for .NET can work independently to convert a spreadsheet to Pdf document and you do not need to use Aspose.Pdf for .NET for the conversion any longer. The conversion does not require to create / use any temporary file(s) too as the whole process can be done in the memory. If you need to save each worksheet in your template Excel file to generate different pdf files. This can be achieved easily. You may try to hide sheets in the file and make one sheet visible at a time based on which you would render PDFs.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Convert Each Worksheet of an Excel File to a Different PDF File 
// Description:This technical tip shows how use Aspose.Cells for converting XLS files (that contain images, charts etc.) to PDF documents. Aspose.Cells for .NET can work independently to convert a spreadsheet to Pdf document and you do not need to use Aspose.Pdf for .NET for the conversion any longer. The conversion does not require to create / use any temporary file(s) too as the whole process can be done in the memory. 
 
If you need to save each worksheet in your template Excel file to generate different pdf files. This can be achieved easily. You may try to hide sheets in the file and make one sheet visible at a time based on which you would render PDFs.
// By: aspose_seo
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8479&lngWId=10//for details.//**************************************

[C#]
 
//Get the Excel file path
string filePath = @"e:\test\Book1.xls";
 
//Instantiage a new workbook and open the Excel
//File from its location
Workbook workbook = new Workbook(filePath);
 
//Get the count of the worksheets in the workbook
int sheetCount = workbook.Worksheets.Count;
 
//Make all sheets invisible except first worksheet
for (int i = 1; i < workbook.Worksheets.Count; i++)
{
 workbook.Worksheets[i].IsVisible = false;
}
 
//Take Pdfs of each sheet
for (int j = 0; j < workbook.Worksheets.Count; j++)
{
Worksheet ws = workbook.Worksheets[j];
workbook.Save("e:\\test2\\worksheet-" + ws.Name + ".pdf");
 
if (j < workbook.Worksheets.Count - 1)
{
workbook.Worksheets[j + 1].IsVisible = true;
workbook.Worksheets[j].IsVisible = false;
}
}
 
[VB]
 
'Get the Excel file path
Dim filePath As String = "e:\test\Book1.xls"
 
'Instantiage a new workbook and open the Excel
'File from its location
Dim workbook As New Workbook(filePath)
 
'Get the count of the worksheets in the workbook
Dim sheetCount As Integer = workbook.Worksheets.Count
 
'Make all sheets invisible except first worksheet
For i As Integer = 1 To workbook.Worksheets.Count - 1
 workbook.Worksheets(i).IsVisible = False
Next i
 	
'Take Pdfs of each sheet
For j As Integer = 0 To workbook.Worksheets.Count - 1
 Dim ws As Worksheet = workbook.Worksheets(j)
 workbook.Save("e:\test2\worksheet-" & ws.Name & ".pdf")
 
 If j < workbook.Worksheets.Count - 1 Then
 workbook.Worksheets(j + 1).IsVisible = True
 workbook.Worksheets(j).IsVisible = False
 End If
Next j
 


Other 16 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.