Konvertera Excel till CSV,TSV och Txt
Spara arbetsbok till text- eller CSV-format
Ibland vill du konvertera eller spara en arbetsbok med flera kalkylblad till textformat. För textformat (till exempel TXT, TabDelim, CSV, etc.) sparar både Microsoft Excel och Aspose.Cells endast innehållet i det aktiva kalkylbladet som standard.
Följande kodexempel förklarar hur man sparar en hel arbetsbok i textformat. Ladda källarbetsboken som kan vara valfri Microsoft Excel- eller OpenOffice-kalkylarksfil (såsom XLS, XLSX, XLSM, XLSB, ODS och så vidare) med valfritt antal kalkylblad.
När koden exekveras konverterar den data från alla ark i arbetsboken till formatet TXT.
Du kan ändra samma exempel för att spara din fil till CSV. Som standard,**TxtSaveOptions.Separator**är komma, så ange inte en avgränsare om du sparar i formatet CSV.
// 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); | |
// 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.Separator = '\t'; | |
opts.ExportAllSheets = true; | |
// Save entire workbook data into file | |
workbook.Save(dataDir + "out.txt", opts); |
Spara textfiler med anpassad separator
Textfiler innehåller kalkylbladsdata utan formatering. Filen är en sorts vanlig textfil som kan ha några anpassade avgränsare mellan sina data.
// 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); | |
string filePath = dataDir + "Book1.xlsx"; | |
// Create a Workbook object and opening the file from its path | |
Workbook wb = new Workbook(filePath); | |
// Instantiate Text File's Save Options | |
TxtSaveOptions options = new TxtSaveOptions(); | |
// Specify the separator | |
options.Separator = Convert.ToChar(";"); | |
// Save the file with the options | |
wb.Save(dataDir + "output.csv", options); | |