保存整个工作簿PDF格式的Excel 2010(C#) [英] Save Entire Workbook as PDF Excel 2010 (C#)

查看:385
本文介绍了保存整个工作簿PDF格式的Excel 2010(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反正有你整个工作簿保存为Excel中的一个PDF文件。我发现这个, http://msdn.microsoft。 COM / EN-US /库/ bb407651(v = office.12)的.aspx ,但它并没有确切地告诉你,如果它保存整个工作簿为PDF或只是活动工作表。如果没有办法保存整个工作簿为PDF,打印将整个工作簿是最好的选择,或在C#中甚至可能吗?下面是我到目前为止我只需要它来保存为PDF格式,所以我可以通过电子邮件发送。感谢您的帮助。



 使用Excel =的Microsoft.Office.Interop.Excel; // Excel引用

//获取Excel和得到Activeworkbook和工作表

Excel.Application OXL;
Excel.Workbook OWB;
Excel.Worksheet oSheet;

//在Excel
OXL =新Excel.Application创建新实例();
oXL.Visible = TRUE;

//打开Excel工作簿
OWB = oXL.Workbooks.Open();
OWB =(Excel.Workbook)oXL.ActiveWorkbook;
oSheet =(Excel.Worksheet)oWB.ActiveSheet;基于表单
oSheet.Cells

//修改Excel表格[6,4] = maskedTextBox1.Text; //改变数值的细胞,细胞的位置[Y轴,X轴]

//保存工作簿为
oWB.SaveAs();

//保存工作簿为PDF


//关闭工作簿
oWB.Close();

//退出Excel
oXL.Quit();


解决方案

在2010你可以保存整个工作簿中的PDF使每个片的活动表。
听起来很奇怪,但如果你当你做一个PDF注意到打印选项有对工作簿的选项。为了解决这个问题打开一个Excel文件,并在2-3个工作表的一些数据填充。现在,握住你的 CTRL 键,然后点击对方工作簿时,它就会变成一个集团。



您会注意到 [组] 的名字出现在Excel文件的顶部,现在当您打印Excel文件,将打印整个工作簿。



试试这个为自己。在代码中,你只需要使每个工作表的活动工作表。我没有太多的工作与Excel对象模型,但它可能是值得做这一个宏,看代码。



我录一个宏,这里是VBA:



表(阵列( 工作表Sheet1,Sheet2的,工作表Sheet 3))。选择



看起来好像你只需要在每个表存储在一个阵列然后简单地



表(MYARRAY)。选择



这将使得随后的所有图纸主动和[分组],然后您可以运行一个打印输出为PDF。通过录制宏也提供的选项打印到PDF:

 `ActiveSheet.ExportAsFixedFormat类型:= xlTypePDF,文件名:= _ 
C:\Users\MyAccount\Desktop\test.pdf,质量:= xlQualityStandard,_
IncludeDocProperties:= TRUE,IgnorePrintAreas:=假,OpenAfterPublish:= _
TRUE`

在这种情况下,活动表是工作表的组已存储在阵列


Is there anyway to save you entire workbook as a pdf in excel. I found this, http://msdn.microsoft.com/en-us/library/bb407651(v=office.12).aspx, but it does not exactly tell you if it saves the entire workbook as a pdf or just the active sheet. If there is no way to save the entire workbook to pdf, would printing the entire workbook be the best option, or even possible in C#? Below is what I have thus far I just need it to save as pdf so I can send in an email. Thanks for the help.

using Excel = Microsoft.Office.Interop.Excel; //Excel Reference

//Gets Excel and gets Activeworkbook and worksheet

Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;

//Create New Instance in Excel
oXL = new Excel.Application();
oXL.Visible = true;

//Open Excel Workbook
oWB = oXL.Workbooks.Open("");
oWB = (Excel.Workbook)oXL.ActiveWorkbook;
oSheet = (Excel.Worksheet)oWB.ActiveSheet;

//Modify Excel Spreadsheet Based on Form
oSheet.Cells[6, 4] = maskedTextBox1.Text; //Change Value in Cell, Cell Location [y-axis, x-axis]

//Save Workbook As
oWB.SaveAs("");

//Save Workbook As PDF


//Close Workbook
oWB.Close("");

//Quit Excel
oXL.Quit();

解决方案

In 2010 you can save the entire workbook in PDF by making each sheet an "Active" sheet. Sounds strange but if you notice the print options when you do a pdf there is no option for workbook. To get around this open an excel file and fill in some data in 2-3 work sheets. Now hold your ctrl key and click on each other workbook, it will then become a "Group".

You will notice the [GROUP] name appear at the top of the excel file and now when you print the excel file it will print the entire workbook.

Try this out for yourself. In code, you just need to make each work sheet an active worksheet. I don't work much with the excel object model but it might be worth doing a macro for this and looking at the code.

I recorded a macro and here is the VBA:

Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select

Looks as though you just need to store each sheet in an array and then simply

Sheets(MyArray).Select

This will then make all sheets active and [grouped] and then you can run a print out to pdf. By recording the macro it also presented the options to print to pdf:

`ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\MyAccount\Desktop\test.pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
    True`

In this case active sheet is your group of sheets that you have stored in an array.

这篇关于保存整个工作簿PDF格式的Excel 2010(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆