Salvataggio di file Excel in CSV, PDF e altri formati

Diversi modi per salvare i tuoi file

Aspose.Cells API fornisce una classe denominataCartella di lavoroche rappresenta un file Excel e fornisce tutte le proprietà e i metodi necessari di cui gli sviluppatori potrebbero aver bisogno per lavorare con i propri file Excel. IlCartella di lavoro la classe fornisce aSalva metodo utilizzato per salvare i file Excel. IlSalva ha molti overload che vengono utilizzati per salvare i file Excel in modi diversi.

Gli sviluppatori possono anche specificare il formato di file in cui devono essere salvati i loro file. I file possono essere salvati in diversi formati come XLS, SpreadsheetML, CSV, delimitato da tabulazioni, valori separati da tabulazioni TSV, XPS e molti altri. Questi formati di file vengono specificati utilizzando l’estensioneSalvaFormato enumerazione.

SalvaFormatoenumeration contiene molti formati di file predefiniti (che possono essere scelti dall’utente) come segue:

Tipi di formati di file Descrizione
AUTO API tenta di rilevare il formato appropriato dall’estensione file specificata nel primo parametro al metodo di salvataggio
CSV Rappresenta un file CSV
XLSX Rappresenta un file Office Open XML SpreadsheetML
XLSM Rappresenta il file XLSM basato su XML
XLTX Rappresenta un file modello di Excel
XLTM Rappresenta un file modello abilitato per le macro di Excel
XLAM Rappresenta un file Excel XLAM
TSV Rappresenta un file di valori separati da tabulazioni
TAB_DELIMITED Rappresenta un file di testo delimitato da tabulazioni
HTML Rappresenta uno o più file HTML
M_HTML Rappresenta uno o più file MHTML
ODS Rappresenta un file OpenDocument Spreadsheet
EXCEL_97_TO_2003 Rappresenta un file XLS che è il formato predefinito per le revisioni di Excel da 1997 a 2003
FOGLIO DI CALCOLO_ML Rappresenta un file SpreadSheetML
XLSB Rappresenta un file binario XLSB di Excel 2007
SCONOSCIUTO Rappresenta un formato non riconosciuto, non può essere salvato.
PDF Rappresenta un documento PDF
XPS Rappresenta un file XML Paper Specification (XPS).
TIFF Rappresenta un file Tagged Image File Format (TIFF).
SVG Rappresenta un file Scalable Vector Graphics (SVG) basato su XML
DIF Rappresenta il formato di interscambio dati.
NUMERI Rappresenta un file di numeri.
MARKDOWN Rappresenta un documento markdown.
Normalmente, ci sono due modi per salvare i file Excel come segue:
  1. Salvataggio del file in una posizione
  2. Salvataggio del file in un flusso

Salvataggio del file in una posizione

Se gli sviluppatori devono salvare i propri file in una posizione di archiviazione, possono semplicemente specificare il nome del file (con il relativo percorso di archiviazione completo) e il formato del file desiderato (utilizzando ilSalvaFormato enumerazione) mentre si chiama ilSalva metodo diCartella di lavorooggetto.

Esempio:

// 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.");

Salvataggio della cartella di lavoro in formato testo o CSV

A volte, vuoi convertire o salvare una cartella di lavoro con più fogli di lavoro in formato testo. Per i formati di testo (ad esempio TXT, TabDelim, CSV ecc.), per impostazione predefinita sia Microsoft Excel che Aspose.Cells salvano solo il contenuto del foglio di lavoro attivo.

L’esempio di codice seguente spiega come salvare un’intera cartella di lavoro in formato testo. Carica la cartella di lavoro di origine che potrebbe essere qualsiasi file di foglio di calcolo Excel o OpenOffice Microsoft (quindi XLS, XLSX, XLSM, XLSB, ODS e così via) con qualsiasi numero di fogli di lavoro.

Quando il codice viene eseguito, converte i dati di tutti i fogli nella cartella di lavoro nel formato TXT.

È possibile modificare lo stesso esempio per salvare il file in CSV. Per impostazione predefinita,TxtSaveOptions.Separator è una virgola, quindi non specificare un separatore se si salva nel formato CSV.

Esempio:

// 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.");

Salvataggio di file di testo con separatore personalizzato

I file di testo contengono dati del foglio di calcolo senza formattazione. Il file è una sorta di file di testo semplice che può avere alcuni delimitatori personalizzati tra i suoi dati.

// 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.");

Salvataggio di file in un flusso

Se gli sviluppatori devono salvare i propri file in un fileFlusso quindi dovrebbero creare un fileFileOutputStream oggetto e quindi salvare il file in quelloFlusso oggetto chiamando ilSalva metodo diCartella di lavoro oggetto. Gli sviluppatori possono anche specificare il formato di file desiderato (utilizzando l’estensioneSalvaFormato enumerazione) mentre si chiama ilSalva metodo.

Esempio:

// 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();

Salvataggio del file in un altro formato

XLS Fascicoli

// 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 Fascicoli

// 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 Fascicoli

// 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.");

Imposta l’opzione ContentCopyForAccessibility

Con ilPdfSaveOptions classe, puoi ottenere o impostare lo PDFAccessibilità Estrai contenutoopzione per controllare l’accesso al contenuto nel PDF convertito. Significa che consente al software di lettura dello schermo di utilizzare il testo all’interno del file PDF per leggere il file PDF. Puoi disabilitarlo applicando una password di modifica dei permessi e deselezionando i due elementi nello screenshotqui.

// 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);

Esporta proprietà personalizzate in PDF

Con ilPdfSaveOptions class, è possibile esportare le proprietà personalizzate nella cartella di lavoro di origine nel file PDF.PdfCustomPropertiesExport enumeratore viene fornito per specificare il modo in cui le proprietà vengono esportate. Queste proprietà possono essere osservate in Adobe Acrobat Reader facendo clic su File e quindi sull’opzione proprietà come mostrato nell’immagine seguente. È possibile scaricare il file modello “sourceWithCustProps.xlsx”.quiper il test e l’output PDF è disponibile il file “outSourceWithCustProps”quiper analisi.

cose da fare:immagine_alt_testo

// 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);

Converti la cartella di lavoro di Excel in Markdown

Il Aspose.Cells API fornisce supporto per l’esportazione di fogli di calcolo in formato Markdown. Per esportare il foglio di lavoro attivo in Markdown, passareSaveFormat.Markdowncome secondo parametro diCartella di lavoro.Salva metodo. Puoi anche usareMarkdownSaveOptionsclass per specificare impostazioni aggiuntive per l’esportazione del foglio di lavoro in Markdown.

L’esempio di codice seguente illustra l’esportazione del foglio di lavoro attivo in Markdown utilizzandoSaveFormat.Markdownmembro di enumerazione. Si prega di consultare iloutput file Markdowngenerato dal codice per riferimento.

// 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);

Argomenti avanzati