将 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文件。下面的代码示例显示了如何将tempalte转换为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已公开ImageOptions 用于HtmlSaveOptions 类,允许开发人员在将电子表格保存为HTML格式时指定图像首选项。
以下是可以应用的一些图像设置的详细信息。
- ImageType:指定图像类型。请注意,所有形状,包括图表,在输出HTML中呈现为图像。
- SmoothingMode:指定线条,曲线和填充区域边缘的抗锯齿。
- TextRenderingHint:指定文本呈现的质量。
- Quality:在指定Jpeg时,指定图像质量为0到100之间的值。
- VerticalResolution:获取或设置图像的垂直分辨率(每英寸点数)。
- HorizontalResolution:获取或设置图像的水平分辨率(每英寸点数)。
- TiffCompression:在指定Tiff时,获取或设置图像的压缩类型。
- Transparent:指示当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,请将SaveFormat.Markdown作为Workbook.Save方法的第二个参数传递。您还可以使用MarkdownSaveOptions类来指定导出工作表到Markdown的其他设置。
以下代码示例演示了如何通过使用SaveFormat.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对象表示)文件。
下面的代码示例演示了通过使用SaveFormat.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,请将 SaveFormat.Docx 作为 Workbook.Save 方法的第二个参数。您还可以使用 DocxSaveOptions 类来指定导出工作表到 DOCX 的附加设置。
下面的代码示例演示了通过使用SaveFormat.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,请将 SaveFormat.Pptx 作为 Workbook.Save 方法的第二个参数。您还可以使用 PptxSaveOptions 类来指定导出工作表到 PPTX 的附加设置。
以下代码示例演示了使用SaveFormat.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); |