保存文件的不同方法
保存文件的不同方法
Aspose.Cells 提供了**工作簿**它表示一个 Microsoft Excel 文件并提供处理 Excel 文件所需的属性和方法。这**工作簿**类提供了**保存**用于保存 Excel 文件的方法。这**保存**方法有许多用于以不同方式保存文件的重载。
文件保存的文件格式由**保存格式**枚举
文件格式类型 | 描述 |
---|---|
CSV | 代表一个CSV文件 |
Excel97To2003 | 表示 Excel 97 - 2003 文件 |
Xlsx | 表示一个 Excel 2007 XLSX 文件 |
Xlsm | 表示一个 Excel 2007 XLSM 文件 |
Xlx | 表示 Excel 2007 模板 XLTX 文件 |
Xltm | 代表 Excel 2007 启用宏的 XLTM 文件 |
XLSB | 表示 Excel 2007 二进制 XLSB 文件 |
SpreadsheetML | 表示电子表格 XML 文件 |
TSV | 表示制表符分隔值文件 |
TabDelimited | 表示制表符分隔的文本文件 |
ODS | 代表一个ODS文件 |
HTML | 代表 HTML 个文件 |
HTML | 代表一个 MHTML 文件 |
PDF格式 | 代表一个PDF文件 |
XPS | 代表一个XPS文件 |
TIFF | 表示标记图像文件格式 (TIFF) |
将文件保存为不同的格式
要将文件保存到存储位置,请指定文件名(包括存储路径)和所需的文件格式(来自**保存格式**枚举)调用时**工作簿**对象的**保存**方法。
// 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 格式,请不要指定分隔符。
// 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); | |
将文件保存到流
要将文件保存到流中,请创建一个内存流要么文件流对象并通过调用将文件保存到该流对象**工作簿**对象的**保存**方法。使用指定所需的文件格式**保存格式**调用时的枚举**保存**方法。
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(); | |
} |
将文件另存为 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"); |
另存为 OpenOffice (ODS, SXC, FODS, OTS)
我们可以将文件保存为 open offce 格式: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 Object Notation)是一种用于共享数据的开放标准文件格式,它使用人类可读的文本来存储和传输数据。 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"); |