Excel转Pdf、图片等格式
将 Excel 工作簿转换为 PDF
PDF 文件广泛用于组织、政府部门和个人之间交换文件。它是一种标准文档格式,软件开发人员经常被要求找到一种方法将 Microsoft Excel 文件转换为 PDF 文档。
Aspose.Cells 支持将 Excel 文件转换为 PDF,并在转换中保持高视觉保真度。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Instantiate the Workbook object | |
// Open an Excel file | |
Workbook workbook = new Workbook("Book1.xlsx"); | |
// Save the document in PDF format | |
workbook.Save("output.pdf"); |
将 Excel 工作簿转换为 JPG
Aspose.Cells 支持将Excel文件转换为JPG。 下面的代码示例显示了如何将工作簿另存为 JPG。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open a template excel file | |
Workbook book = new Workbook("Book1.xlsx"); | |
//Convert workbook to JPG image. | |
book.Save("Image1.jpg"); |
将 Excel 工作簿转换为图像
Aspose.Cells 支持Excel文件转图片。 下面的代码示例显示了如何将工作簿另存为图像。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open a template excel file | |
Workbook book = new Workbook("Book1.xlsx"); | |
//Convert workbook to BMP image. | |
book.Save("Image1.bmp"); | |
//Convert workbook to JPG image. | |
book.Save("Image1.jpg"); | |
//Convert workbook to Png image. | |
book.Save("Image1.png"); | |
//Convert workbook to EMF image. | |
book.Save("Image1.emf"); | |
//Convert workbook to GIF image. | |
book.Save("Image1.gif"); |
将 Excel 工作簿转换为 XPS
XPS 文档格式包含定义文档布局和每个页面的视觉外观的结构化 XML 标记,以及用于分发、存档、呈现、处理和打印文档的呈现规则。
XPS 的标记语言是 XAML 的一个子集,它允许它在文档中合并矢量图形元素,使用 XAML 标记 Windows Presentation Foundation (WPF) 原语。使用的元素根据路径和其他几何图元进行描述。
XPS 文件实际上是一个使用开放打包约定的 unicode ZIP 存档,其中包含构成文档的文件。其中包括每个页面的 XML 标记文件、文本、嵌入字体、光栅图像、2D 矢量图形以及数字版权管理信息。只需在支持 ZIP 文件的应用程序中将其打开,即可检查 XPS 文件的内容。
从 Aspose.Cells 6.0.0,支持 Microsoft Excel 到 XPS 的转换。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open an Excel file | |
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(dataDir + "Book1.xls"); | |
// Get the first worksheet | |
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0]; | |
// Apply different Image and Print options | |
Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions(); | |
// Set the Format | |
options.SaveFormat = SaveFormat.Xps; | |
// Render the sheet with respect to specified printing options | |
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, options); | |
// Save | |
sr.ToImage(0, dataDir + "out_printingxps.out.xps"); | |
// Export the whole workbook to XPS | |
workbook.Save(dataDir + "out_whole_printingxps.out.xps", new XpsSaveOptions()); |
将 Excel 转换为 Ods、Sxc 和 Fods(OpenOffice / LibreOffice Calc)
Aspose.Cells 支持将Excel文件转换为Ods、Sxc和Fods文件。下面的代码示例显示了如何将模板到 Ods、Sxc 和 Fods 文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load your source workbook | |
Workbook workbook = new Workbook("book1.xlsx"); | |
// Save as ods file | |
workbook.Save("Out.ods"); | |
// Save as sxc file | |
workbook.Save("Out.sxc"); | |
// Save as fods file | |
workbook.Save("Out.fods"); |
将 Excel 工作簿转换为 MHTML 文件
MHTML 将正常的 HTML 与外部资源(即通常链接进来的内容,如图像、动画、音频等)合并到一个文件中。它们用于文件扩展名为 .mht 的电子邮件。
Aspose.Cells支持读写MHTML文件。
下面的代码示例显示了如何将工作簿另存为 MHTML 文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Specify the file path | |
string filePath = dataDir + "Book1.xlsx"; | |
// Specify the HTML Saving Options | |
HtmlSaveOptions sv = new HtmlSaveOptions(SaveFormat.MHtml); | |
// Instantiate a workbook and open the template XLSX file | |
Workbook wb = new Workbook(filePath); | |
// Save the MHT file | |
wb.Save(filePath + ".out.mht", sv); |
将 Excel 工作簿转换为 HTML
Aspose.Cells API 提供了将电子表格导出为HTML格式的支持。为此,Aspose.Cells 使用**HtmlSaveOptions**类提供灵活性来控制输出 HTML 的几个方面。
下面的代码示例显示了如何将工作簿另存为 HTML 文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Specify the file path | |
string filePath = dataDir + "sample.xlsx"; | |
// Load your sample excel file in a workbook object | |
Workbook wb = new Workbook(filePath); | |
// Save it in HTML format | |
wb.Save(dataDir + "ConvertingToHTMLFiles_out.html", SaveFormat.Html); |
为 HTML 设置图像首选项
从8.0.2开始,Aspose.Cells暴露了**图像选项**为了**HtmlSaveOptions**类,允许开发人员在将电子表格保存为 HTML 格式时指定图像首选项。
以下是一些可以应用的图像设置的详细信息,
- 图像类型:指定图像类型。请注意,所有形状(包括图表)在输出 HTML 中呈现为图像。
- 平滑模式:指定填充区域的直线、曲线和边缘的抗锯齿。
- TextRenderingHint:指定文本呈现的质量。
- 质量 :指定图像的质量在 0 到 100 之间,当**图像类型**指定为 Jpeg。
- 垂直分辨率: 获取或设置图像的垂直分辨率(以每英寸点数为单位)。
- **水平分辨率**获取或设置图像的水平分辨率(以每英寸点数为单位)。
- TiffCompression : 获取或设置图片压缩类型**图像类型**指定为 Tiff。
- 透明: 指示当 ImageFormat 指定为 Png 时图像的背景是否应该透明。
下面的代码演示了如何使用**HtmlSaveOptions.ImageOptions**指定不同的首选项。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Specify the file path | |
string filePath = dataDir + "Book1.xlsx"; | |
// Load a spreadsheet to be converted | |
Workbook book = new Workbook(filePath); | |
// Create an instance of HtmlSaveOptions | |
HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.Html); | |
// Set the ImageFormat to PNG | |
saveOptions.ImageOptions.ImageType = Drawing.ImageType.Png; | |
// Set SmoothingMode to AntiAlias | |
saveOptions.ImageOptions.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; | |
// Set TextRenderingHint to AntiAlias | |
saveOptions.ImageOptions.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; | |
// Save spreadsheet to HTML while passing object of HtmlSaveOptions | |
book.Save( dataDir + "output.html", saveOptions); | |
将 Excel 工作簿转换为 Markdown
Aspose.Cells API 提供了将电子表格导出为Markdown格式的支持。要将活动工作表导出到 Markdown,请通过**保存格式.Markdown**作为第二个参数**工作簿.保存**方法。您也可以使用**MarkdownSaveOptions**类以指定将工作表导出到 Markdown 的其他设置。
下面的代码示例演示了通过使用将活动工作表导出到 Markdown**保存格式.Markdown**枚举成员。请参阅输出 Markdown 文件生成的代码供参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open the template file | |
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx"); | |
// Save as Markdown | |
workbook.Save(outputDir + "Book1.md", SaveFormat.Markdown); |
将 Excel 工作簿转换为 JSON
Aspose.Cells 支持将工作簿转换为 Json(JavaScript Object Notation)文件。
下面的代码示例演示了通过使用将活动工作表导出到 Json保存格式.Json枚举成员。请看代码转换源文件到输出Json文件生成的代码供参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load your source workbook | |
Workbook workbook = new Workbook("Book1.xlsx"); | |
// convert the workbook to json file. | |
workbook.Save(dir + "book1.json"); |
将 Excel 转换为 XML
Aspose.Cells 支持将工作簿转换为 Excel 2003 电子表格 XML 和纯 XML 数据。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Load your source workbook | |
Workbook workbook = new Workbook("Book1.xlsx"); | |
//Save as Excel 2003 Spreadsheet XML | |
workbook.Save("Spreadsheet.xml"); | |
//Save as plain XML data | |
XmlSaveOptions xmlSaveOptions = new XmlSaveOptions(); | |
workbook.Save("data.xml", xmlSaveOptions); |
将 Excel 工作簿转换为 TIFF
Aspose.Cells 支持将工作簿转换为 TIFF 文件。
下面的代码片段显示了如何将 Excel 转换为 TIFF:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open a template excel file | |
Workbook book = new Workbook("Book1.xlsx"); | |
//save file to tiff | |
book.Save("out.tiff"); |
将 Excel 工作簿转换为 DOCX
Aspose.Cells API 提供将电子表格转换为 DOCX 格式的支持。要将工作簿导出到 DOCX,请通过保存格式.docx作为第二个参数工作簿.保存方法。您也可以使用DocxSave选项类以指定将工作表导出到 DOCX 的其他设置。
下面的代码示例演示了通过使用将活动工作表导出到 DOCX保存格式.docx枚举成员。请参阅输出 DOCX 文件生成的代码供参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open the template file | |
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx"); | |
// Save as Markdown | |
workbook.Save(outputDir + "Book1.docx", SaveFormat.Docx); |
将 Excel 工作簿转换为 PPTX
Aspose.Cells API 提供将电子表格转换为 PPTX 格式的支持。要将工作簿导出到 PPTX,请通过保存格式.Pptx作为第二个参数工作簿.保存方法。您也可以使用PptxSave选项类以指定将工作表导出到 PPTX 的其他设置。
下面的代码示例演示了通过使用将活动工作表导出到 PPTX保存格式.Pptx枚举成员。请参阅输出 PPTX 文件生成的代码供参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Open the template file | |
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx"); | |
// Save as Markdown | |
workbook.Save(outputDir + "Book1.pptx", SaveFormat.Pptx); |