保存文件的不同方式
不同的文件保存方式
Aspose.Cells提供了Workbook,代表Microsoft Excel文件,并提供必要的属性和方法来处理Excel文件。Workbook类提供了用于保存Excel文件的Save方法。Save方法有许多重载,用于以不同的方式保存文件。
保存文件的文件格式由SaveFormat枚举决定
文件格式类型 | 描述 |
---|---|
CSV | 表示 CSV 文件 |
Excel97To2003 | 表示Excel 97-2003文件 |
Xlsx | 表示Excel 2007 XLSX文件 |
Xlsm | 表示Excel 2007 XLSM文件 |
Xltx | 表示Excel 2007模板XLTX文件 |
Xltm | 表示Excel 2007启用宏的XLTM文件 |
Xlsb | 表示Excel 2007二进制XLSB文件 |
SpreadsheetML | 表示一种Spreadsheet XML文件 |
TSV | 表示制表符分隔数值文件 |
TabDelimited | 代表分隔符文本文件 |
ODS | 表示 ODS 文件 |
Html | 表示HTML文件 |
MHtml | 表示一个MHTML文件 |
表示一个PDF文件 | |
XPS | 表示一个XPS文档 |
TIFF | 表示Tagged Image File Format (TIFF) |
如何将文件保存为不同的格式
要将文件保存到存储位置,请在调用Workbook对象的Save方法时指定文件名(包括存储路径)和所需的文件格式(从SaveFormat枚举中)。
// 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.xls"; | |
// Load your source workbook | |
Workbook workbook = new Workbook(filePath); | |
// Save in Excel 97 to 2003 format | |
workbook.Save(dataDir + ".output.xls"); | |
// OR | |
workbook.Save(dataDir + ".output.xls", new XlsSaveOptions()); | |
// Save in Excel2007 xlsx format | |
workbook.Save(dataDir + ".output.xlsx", SaveFormat.Xlsx); | |
// Save in Excel2007 xlsb format | |
workbook.Save(dataDir + ".output.xlsb", SaveFormat.Xlsb); | |
// Save in ODS format | |
workbook.Save(dataDir + ".output.ods", SaveFormat.Ods); | |
// Save in Pdf format | |
workbook.Save(dataDir + ".output.pdf", SaveFormat.Pdf); | |
// Save in Html format | |
workbook.Save(dataDir + ".output.html", SaveFormat.Html); | |
// Save in SpreadsheetML format | |
workbook.Save(dataDir + ".output.xml", SaveFormat.SpreadsheetML); | |
如何将工作簿保存为PDF
便携式文档格式(PDF)是Adobe于1990年代创建的一种文档类型。该文件格式的目的是引入一种标准,以在与应用软件、硬件和操作系统无关的格式中表示文档和其他参考材料。PDF文件格式具有完整的能力,可以包含文本、图像、超链接、表单字段、富媒体、数字签名、附件、元数据、地理空间特征和3D对象等信息,这些信息可以成为源文档的一部分。
以下代码显示了如何使用Aspose.Cells将工作簿保存为PDF文件:
// Instantiate the Workbook object | |
Workbook workbook = new Workbook(); | |
//Set value to Cell. | |
workbook.Worksheets[0].Cells["A1"].PutValue("Hello World!"); | |
workbook.Save("pdf1.pdf"); | |
// Save as Pdf format compatible with PDFA-1a | |
PdfSaveOptions saveOptions = new PdfSaveOptions(); | |
saveOptions.Compliance = PdfCompliance.PdfA1a; | |
workbook.Save("pdfa1a.pdf"); |
如何将工作簿保存为文本或CSV格式
有时,您希望将包含多个工作表的工作簿转换或保存为文本格式。对于文本格式(例如TXT、TabDelim、CSV等),默认情况下,Microsoft Excel和Aspose.Cells都仅保存活动工作表的内容。
以下代码示例说明如何将整个工作簿保存为文本格式。加载源工作簿,可以是任何Microsoft Excel或OpenOffice电子表格文件(例如XLS、XLSX、XLSM、XLSB、ODS等),并且可以具有任意数量的工作表。
执行代码后,将会将工作簿中所有工作表的数据转换为TXT格式。
您可以修改相同的示例将文件保存为CSV格式。默认情况下,TxtSaveOptions.Separator是逗号,因此如果保存为CSV格式,则不需要指定分隔符。请注意:如果您使用的是评估版本,即使TxtSaveOptions.ExportAllSheets属性设置为true,程序仍然只会导出一个工作表。
// 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); |
如何使用自定义分隔符将文件保存为文本文件
文本文件包含无格式的电子表格数据。该文件是一种纯文本文件,可以在其数据之间具有一些自定义分隔符。
// 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); | |
如何将文件保存到流中
要将文件保存到流,请创建MemoryStream或FileStream对象,并通过调用Workbook对象的Save方法将文件保存到该流对象。在调用Save方法时,使用SaveFormat枚举指定所需的文件格式。
public async Task<IActionResult> DownloadExcel() | |
{ | |
// 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"; | |
// Load your source workbook | |
Workbook workbook = new Workbook(filePath); | |
// Save the workbook to a memory stream | |
var stream = new MemoryStream(); | |
workbook.Save(stream, SaveFormat.Xlsx); | |
// Reset the position of the stream to 0 | |
stream.Position = 0; | |
// Set the content type and file name | |
var contentType = "application/octet-stream"; | |
var fileName = "output.xlsx"; | |
// Set the response headers | |
Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{fileName}\""); | |
Response.ContentType = contentType; | |
// Write the file contents to the response body stream | |
await stream.CopyToAsync(Response.Body); | |
// Close the file stream | |
stream.Dispose(); | |
// Return the response | |
return new EmptyResult(); | |
} |
如何将Excel文件保存为Html和Mht文件
Aspose.Cells可以简单地保存一个Excel文件、JSON、CSV或其他可以被Aspose.Cells加载的文件为.html和.mht文件。
// 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 file to mhtml format | |
workbook.Save("out.mht"); |
如何将Excel文件保存为OpenOffice(ODS,SXC,FODS,OTS)
我们可以将文件保存为开放办公格式:ODS、SXC、FODS、OTS等。
// 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文件保存为JSON或XML
JSON(JavaScript对象表示)是一种用于存储和传输数据的开放标准文件格式,它使用人类可读的文本。JSON文件存储为.json扩展名。JSON需要更少的格式化,是XML的一个很好的替代品。JSON源自JavaScript,但是是一种与语言无关的数据格式。许多现代编程语言都支持JSON的生成和解析。application/json是用于JSON的媒体类型。
Aspose.Cells支持将文件保存为JSON或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"); | |
// convert the workbook to json file. | |
workbook.Save(dir + "book1.json"); |