将Excel文件保存为CSV、PDF等格式
保存文件的不同方法
Aspose.Cells API 提供了一个名为工作簿表示一个 Excel 文件并提供开发人员处理其 Excel 文件可能需要的所有必要属性和方法。这工作簿类提供了[救球](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.io.OutputStream,%20com.aspose.cells.SaveOptions)方法,用于保存 Excel 文件。这[救球](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.io.OutputStream,%20com.aspose.cells.SaveOptions)方法有许多重载,用于以不同方式保存 Excel 文件。
开发人员还可以指定保存文件的文件格式。文件可以保存为多种格式,例如 XLS、SpreadsheetML、CSV、制表符分隔、制表符分隔值 TSV、XPS 等等。这些文件格式使用保存格式枚举。
保存格式枚举包含许多预定义的文件格式(可以由您选择),如下所示:
文件格式类型 | 描述 |
---|---|
汽车 | API 尝试检测从第一个参数中指定的文件扩展名到保存方法的适当格式 |
CSV | 代表一个CSV文件 |
XLSX | 表示 Office Open XML SpreadsheetML 文件 |
XLSM | 表示基于 XML 的 XLSM 文件 |
XLTX | 代表一个Excel模板文件 |
XLTM | 表示启用 Excel 宏的模板文件 |
XLAM | 表示一个 Excel XLAM 文件 |
TSV | 表示制表符分隔值文件 |
TAB_DELIMITED | 表示制表符分隔的文本文件 |
HTML | 代表一个 HTML 文件 |
M_HTML | 代表一个 MHTML 文件 |
ODS | 表示 OpenDocument 电子表格文件 |
EXCEL_97_TO_2003 | 表示 XLS 文件,该文件是 Excel 1997 到 2003 修订版的默认格式 |
电子表格_ML | 表示一个 SpreadSheetML 文件 |
XLSB | 表示 Excel 2007 二进制 XLSB 文件 |
未知 | 表示无法识别的格式,无法保存。 |
代表一个PDF文件 | |
XPS | 表示 XML 纸张规范 (XPS) 文件 |
TIFF | 表示标记图像文件格式 (TIFF) 文件 |
SVG | 表示基于 XML 的可缩放矢量图形 (SVG) 文件 |
DIF | 表示数据交换格式。 |
数字 | 代表数字文件。 |
MARKDOWN | 代表降价文件。 |
通常情况下,保存Excel文件的方法有以下两种: |
- 将文件保存到某个位置
- 将文件保存到流
将文件保存到某个位置
如果开发人员需要将他们的文件保存到某个存储位置,那么他们可以简单地指定文件名(及其完整的存储路径)和所需的文件格式(使用保存格式枚举),同时调用[救球](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.io.OutputStream,%20com.aspose.cells.SaveOptions)) 的方法工作簿目的。
例子:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SavingFiletoSomeLocation.class) + "loading_saving/"; | |
String filePath = dataDir + "Book1.xls"; | |
// Creating an Workbook object with an Excel file path | |
Workbook workbook = new Workbook(filePath); | |
// Save in Excel 97 – 2003 format | |
workbook.save(dataDir + "SFTSomeLocation_out.xls"); | |
// OR | |
// workbook.save(dataDir + ".output..xls", new | |
// XlsSaveOptions(SaveFormat.Excel97To2003)); | |
// Save in Excel2007 xlsx format | |
workbook.save(dataDir + "SFTSomeLocation_out.xlsx", FileFormatType.XLSX); | |
// Save in Excel2007 xlsb format | |
workbook.save(dataDir + "SFTSomeLocation_out.xlsb", FileFormatType.XLSB); | |
// Save in ODS format | |
workbook.save(dataDir + "SFTSomeLocation_out.ods", FileFormatType.ODS); | |
// Save in Pdf format | |
workbook.save(dataDir + "SFTSomeLocation_out.pdf", FileFormatType.PDF); | |
// Save in Html format | |
workbook.save(dataDir + "SFTSomeLocation_out.html", FileFormatType.HTML); | |
// Save in SpreadsheetML format | |
workbook.save(dataDir + "SFTSomeLocation_out.xml", FileFormatType.EXCEL_2003_XML); | |
// Print Message | |
System.out.println("Worksheets are saved successfully."); |
将工作簿保存为文本或 CSV 格式
有时,您希望将包含多个工作表的工作簿转换或保存为文本格式。对于文本格式(例如 TXT、TabDelim、CSV 等),默认情况下 Microsoft Excel 和 Aspose.Cells 仅保存活动工作表的内容。
下面的代码示例说明了如何将整个工作簿保存为文本格式。加载源工作簿,它可以是任何 Microsoft Excel 或 OpenOffice 电子表格文件(例如 XLS、XLSX、XLSM、XLSB、ODS 等)和任意数量的工作表。
代码执行时,将工作簿中所有工作表的数据转换为TXT格式。
您可以修改相同的示例以将文件保存到 CSV。默认情况下,TxtSaveOptions.分隔符是逗号,所以保存为CSV格式时不要指定分隔符。
例子:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SaveWorkbookToTextCSVFormat.class) + "loading_saving/"; | |
// Load your source workbook | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Text save options. You can use any type of separator | |
TxtSaveOptions opts = new TxtSaveOptions(); | |
opts.setSeparator('\t'); | |
opts.setExportAllSheets(true); | |
//Save entire workbook data into file | |
workbook.save(dataDir + "SWTTextCSVFormat-out.txt", opts); | |
// Print message | |
System.out.println("Excel to Text File Conversion performed successfully."); |
使用自定义分隔符保存文本文件
文本文件包含未格式化的电子表格数据。该文件是一种纯文本文件,其数据之间可以有一些自定义的分隔符。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SavingTextFilewithCustomSeparator.class) + "loading_saving/"; | |
// Creating an Workbook object with an Excel file path | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
TxtSaveOptions toptions = new TxtSaveOptions(); | |
// Specify the separator | |
toptions.setSeparator(';'); | |
workbook.save(dataDir + "STFWCSeparator_out.csv"); | |
// Print Message | |
System.out.println("Worksheets are saved successfully."); |
将文件保存到流
如果开发人员需要将他们的文件保存到溪流那么他们应该创建一个文件输出流对象,然后将文件保存到那个溪流通过调用对象[救球](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.io.OutputStream,%20com.aspose.cells.SaveOptions)) 的方法工作簿目的。开发人员还可以指定所需的文件格式(使用保存格式枚举),同时调用[救球](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.io.OutputStream,%20com.aspose.cells.SaveOptions)) 方法。
例子:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SavingFiletoStream.class) + "loading_saving/"; | |
// Creating an Workbook object with an Excel file path | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
FileOutputStream stream = new FileOutputStream(dataDir + "SFToStream_out.xlsx"); | |
workbook.save(stream, FileFormatType.XLSX); | |
// Print Message | |
System.out.println("Worksheets are saved successfully."); | |
stream.close(); |
将文件保存为其他格式
XLS 文件
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SaveXLSFile.class) + "loading_saving/"; | |
// Creating an Workbook object with an Excel file path | |
Workbook workbook = new Workbook(); | |
// Save in xls format | |
workbook.save(dataDir + "SXLSFile_out.xls", FileFormatType.EXCEL_97_TO_2003); | |
// Print Message | |
System.out.println("Worksheets are saved successfully."); |
XLSX 文件
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SaveXLSXFile.class) + "loading_saving/"; | |
// Creating an Workbook object with an Excel file path | |
Workbook workbook = new Workbook(); | |
// Save in xlsx format | |
workbook.save(dataDir + "SXLSXFile_out.xlsx", FileFormatType.XLSX); | |
// Print Message | |
System.out.println("Worksheets are saved successfully."); |
PDF 文件
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SaveInPdfFormat.class) + "loading_saving/"; | |
// Creating an Workbook object with an Excel file path | |
Workbook workbook = new Workbook(); | |
// Save in PDF format | |
workbook.save(dataDir + "SIPdfFormat_out.pdf", FileFormatType.PDF); | |
// Print Message | |
System.out.println("Worksheets are saved successfully."); |
设置 ContentCopyForAccessibility 选项
随着Pdf保存选项类,您可以获取或设置 PDF辅助功能提取内容选项来控制转换后的 PDF 中的内容访问。这意味着它允许屏幕阅读器软件利用 PDF 文件中的文本来阅读 PDF 文件。您可以通过应用更改权限密码并取消选择屏幕截图中的两项来禁用它这里.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Load excel file containing some data | |
Workbook workbook = new Workbook("book1.xlsx"); | |
// Create an instance of PdfSaveOptions and pass SaveFormat to the constructor | |
PdfSaveOptions pdfSaveOpt = new PdfSaveOptions(SaveFormat.PDF); | |
// Create an instance of PdfSecurityOptions | |
PdfSecurityOptions securityOptions = new PdfSecurityOptions(); | |
// Set AccessibilityExtractContent to true | |
securityOptions.setAccessibilityExtractContent(false); | |
// Set the securityoption in the PdfSaveOptions | |
pdfSaveOpt.setSecurityOptions(securityOptions); | |
// Save the workbook to PDF format while passing the object of PdfSaveOptions | |
workbook.save("outFile.pdf", pdfSaveOpt); |
将自定义属性导出到 PDF
随着Pdf保存选项类,您可以将源工作簿中的自定义属性导出到 PDF.PdfCustomPropertiesExport枚举器用于指定导出属性的方式。可以在 Adobe Acrobat Reader 中通过单击“文件”然后单击“属性”选项来观察这些属性,如下图所示。可以下载模板文件“sourceWithCustProps.xlsx”这里用于测试和输出 PDF 文件“outSourceWithCustProps”可用这里进行分析。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Load excel file containing custom properties | |
Workbook workbook = new Workbook("sourceWithCustProps.xlsx"); | |
// Create an instance of PdfSaveOptions and pass SaveFormat to the constructor | |
PdfSaveOptions pdfSaveOpt = new PdfSaveOptions(SaveFormat.PDF); | |
// Set CustomPropertiesExport property to PdfCustomPropertiesExport.Standard | |
pdfSaveOpt.setCustomPropertiesExport(PdfCustomPropertiesExport.STANDARD); | |
// Save the workbook to PDF format while passing the object of PdfSaveOptions | |
workbook.save("outSourceWithCustProps.pdf", pdfSaveOpt); |
将 Excel 工作簿转换为 Markdown
Aspose.Cells API 提供了将电子表格导出为Markdown格式的支持。要将活动工作表导出到 Markdown,请通过保存格式.Markdown作为第二个参数[工作簿.保存](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.lang.String,%20int)) 方法。您也可以使用Markdown 保存选项类以指定将工作表导出到 Markdown 的其他设置。
下面的代码示例演示了通过使用将活动工作表导出到 Markdown保存格式.Markdown枚举成员。请参阅输出 Markdown 文件生成的代码供参考。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(ConvertExcelFileToMarkdown.class) + "LoadingSavingConvertingAndManaging/"; | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Save as Markdown | |
workbook.save(dataDir + "Book1.md", SaveFormat.MARKDOWN); |