Print Embedded Excel sheet - c#

Edit:
I have a user control with embedded excel sheet and gridviews ( WinForm). I have a form which has that user control. I have a print button on top and I want to convert the whole form into printable format. Any suggestion on how to move ahead?

Printing an Excel document can be done using the Workbook.ExportAsFixedFormat method:
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF
FileName:=“sales.pdf”
Quality:=xlQualityStandard
DisplayFileAfterPublish:=True
This method should be preferred over using SaveAs because it also allows specifying all PDF / XPS options.
Note: This method has been added to the Excel object model with Excel 2007 and requires the Save as PDF or XPS Add-in for 2007 Microsoft Office programs to be installed.
See this related question: What is the FileType number for PDF in Excel 2007 that is needed to save a file as PDF through the API?

For versions earlier than 2007 you can embed/install a PDF printer and use the PrintOut method, specifying ActivePrinter as required.

Related

Save as pdf using ClosedXML

Does ClosedXML have any function that can be used to save an excel file as PDF?
Currently i am using ClosedXML to create and populate the excel file and then using Interop to save as PDF.
But since Interop depends on the MS Office installed on the syatem the formatting changes on every version of MS Office. And since ClosedXML doesnt require having MS office installed it would be nice to be able to export or save as PDF directly from it without using Interops.
Does ClosedXML have any function that can be used to save an excel file as PDF?
No.
Instead you can use the free version of GemBox.Spreadsheet.
Also look at the top 10 libraries which allows us to manipulate excel.
However GemBox was the only free and featured one that I found with the purpose of creating an invoice report for a website, by converting excel to pdf.
you can use the ClosedXML.Excel and iTextSharp to convert Excel to PDF file.
Here a link with example:
Convert Excel file to Pdf in ASP.Net

Loading spreadsheet template file doesn't retain form button

I am using SpreadsheetGear 2012 in my application to load, modify then save a new copy of a template workbook.
My code takes invoicing information from a database and formats it for the workbook. Then the user will print an invoice from the formatted information using a button on the workbook.
I use a template workbook with some formatting already provided to make my life easier. Included on the original template workbook is a button that runs a VBA macro in the spreadsheet. The VBA macro loads successfully but the button just disappears in the new workbook.
Some of the steps I have tried to rectify the issue: I've added a new button, changed the VBA macro code from a function to a sub, saved the template file as a macro-enabled spreadsheet (.xlsm) and saved the revised copy as a macro-enabled spreadsheet file.
Has anyone experienced this issue and do you have a solution?
If you are using the Open XML file formats (XLSX/XLSM), then this is a known limitation:
http://www.spreadsheetgear.com/support/help/spreadsheetgear.net.7.0/#SpreadsheetGear_2012_Limitations.html
In short, SpreadsheetGear 2012 does not support reading/writing Form Controls (like buttons), as well as Cell Comments, when working with the Open XML file formats. So your button is getting dropped when the file is initially read into SpreadsheetGear.
If you want to preserve these objects through SpreadsheetGear, you will need to use the XLS (FileFormat.Excel8) file format, which does support them.

OPEN XML to Export 2007/2010

In my application I already have functionality to export into 2003 format. Where I am constructing a string out of the template and write using System.IO.File.WriteAllText.
But it does not work with excel 2007/2010, that's why I wanted to convert it to Openxml in order to support 2007 and 2010.
I have string ready with creation of cells and rows from template.
I want advice on how to achieve or any body has reference link.
Please let me know.
Regards
WriteAllText class will save you a text file. You are saving an html(text) based file with xls extension. This is not a real xls file (that is actually a binary file), but MS Excel recognize and interpret the html format.
An xlsx file is a binary file. You can use OpenXML Office library ( Excel.XlFileFormat.xlOpenXMLWorkbook ) or another .NET Excel library like EasyXLS. Check this sample of code for more details.

Need to convert Excel sheets into Text (tab delimited ) as in MS Excel 2007 programatically using C#

Ok, here is the problem
MS Excel allows me to save the whole sheet as text tab delimited
I'm using this text in a program , but the user needs to make the previous step manually
I want to automate this step so the user only import the Excel Book into my program , and shows how many sheets inside it letting the user select the desired sheet and then the program will convert this sheet into text (tab delimited ) , any ideas ?
Well... one of the easiest to learn methods would be the use Excel Interop. The downside of this method is it (1.) requires Excel to be installed and (2.) Can get messy very quickly... but generally it makes sense.
I suggest familiarizing yourself with Excel Interop. Now, take note of the SaveAs method for the Workbook object.
Loop through the Worksheets property of the opened Workbook.
Display the sheet names for the user to choose.
Get the desired sheet by name and access the Worksheet object
Use the desired Worksheet's Select method
Use the Workbook's SaveAs method. Be sure to use FileFormat option to make it tab delimited.
Hope that helps. Just remember, it's doable.
There are several options:
OpenXML 2.0 from MS - http://msdn.microsoft.com/en-us/library/bb448854.aspx...
It is a free library that does NOT require office to be installed... you can read + write Office files including Excel and Word (Version 2007 and up...).
VSTO/Office Interop from MS - http://msdn.microsoft.com/en-US/office/hh128771.aspx
this need Office to be installed and works only in desktop apps (NOT services or ASP.NET..) and let's you automate whatever you want...

Programmatically convert Excel to XPS

I am looking for a way to programmatically convert Excel reports to XPS format. Is this supported anywhere in the Microsoft framework, or should we look for a third party tool?
Yes currently we are programmatically creating Excel reports using ExcelWriter and need to produce XPS reports for a client. So we either go direct to XPS which seems to be a larger learning curve, or convert the Excel report to XPS.
There is an add on for Office 2007 that gives you the ability to export to XPS or PDF. Invoke Excel via Microsoft.Office.interop.Excel and export to XPS.
From my own code (Workbook is an instance but providing full namespace instead):
Microsoft.Office.Interop.Excel.Workbook.ExportAsFixedFormat(
Excel.XlFixedFormatType.xlTypeXPS,
pdfpath, Excel.XlFixedFormatQuality.xlQualityStandard,
true, true,
fpage, tpage,
false,
oMissing
);
There is an MSDN article on how to do this.
I would prefer the method suggested by Colin, but you could also use SaveAs on the workbook with the FileFormat constant of 18 for XPS as described in this related question:
What is the FileType number for PDF in Excel 2007 that is needed to save a file as PDF through the API?
You can also print from Excel to XPS. Not technically converting but may be just what you need.
Dim ws As Worksheet
Set ws = ActiveSheet
Call ws.PrintOut(ActivePrinter:="Microsoft XPS Document Writer", _
PrintToFile:=True, PrToFileName:="S:\Temp\Test3.xps")

Categories

Resources