将 Excel 文件保存为 CSV、PDF 和其他格式
保存文件的不同方式
Aspose.Cells API提供了一个名为Workbook的类,代表一个Excel文件,并提供了开发人员可能需要处理Excel文件的所有必要属性和方法。Workbook类提供一个save方法,用于保存Excel文件。save方法有许多重载,用来以不同的方式保存Excel文件。
开发人员还可以指定文件应保存的文件格式。文件可以以多种格式保存,如 XLS、SpreadsheetML、CSV、Tab Delimited、Tab-separated values TSV、XPS 等。这些文件格式使用 SaveFormat 枚举进行指定。
SaveFormat 枚举包含许多预定义的文件格式(可以由您选择),如下所示:
文件格式类型 | 描述 |
---|---|
AUTO | 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 | 表示开放文档电子表格文件 |
EXCEL_97_TO_2003 | 表示XLS文件,是Excel 1997到2003版本的默认格式 |
SPREADSHEET_ML | 表示SpreadSheetML文件 |
XLSB | 表示Excel 2007二进制XLSB文件 |
UNKNOWN | 表示无法识别的格式,无法保存 |
表示PDF文档 | |
XPS | 表示XML Paper Specification (XPS)文件 |
TIFF | 表示Tagged Image File Format (TIFF)文件 |
SVG | 表示基于XML的可伸缩矢量图形 (SVG) 文件 |
DIF | 表示数据交换格式 |
NUMBERS | 表示一个数字文件 |
MARKDOWN | 表示一个markdown文档。 |
通常,有两种方法可以保存Excel文件如下: |
- 将文件保存到某个位置
- 将文件保存到流中
将文件保存到某个位置
如果开发人员需要将文件保存到某个存储位置,他们只需在调用Workbook对象的save方法时指定文件名(完整的存储路径)和期望的文件格式(使用SaveFormat枚举)。
示例:
// 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.Separator是逗号,因此如果保存为CSV格式,则不需要指定分隔符。请注意:如果您使用的是评估版本,即使方法的参数TxtSaveOptions.setExportAllSheets(boolean value)设置为true,程序仍然只会导出一个工作表。
示例:
// 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."); |
将文件保存到流中
如果开发人员需要将文件保存到Stream中,则应创建一个FileOutputStream对象,然后通过调用Workbook对象的save方法将文件保存到该Stream对象中。开发人员还可以在调用save方法时指定期望的文件格式(使用SaveFormat枚举)。
示例:
// 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选项
通过PdfSaveOptions类,您可以获取或设置PDFAccessibilityExtractContent选项以控制转换后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
通过PdfSaveOptions类,您可以将源工作簿中的自定义属性导出到PDF中。提供了PdfCustomPropertiesExport枚举用于指定导出属性的方式。您可以通过单击“文件”,然后单击属性选项在Adobe Acrobat Reader中查看这些属性。
// 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,请在Workbook.Save方法的第二个参数处传入SaveFormat.Markdown。您还可以使用MarkdownSaveOptions类指定将工作表导出为Markdown的附加设置。
以下代码示例演示了如何使用SaveFormat.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); |