加载或导入具有公式的CSV文件
Contents
[
Hide
]
CSV文件通常包含文本数据,它们不包含任何公式。但有时会出现CSV文件也包含公式的情况。应该将这样的CSV文件加载时设置TxtLoadOptions.has_formula属性为true。一旦将此属性设置为true,Aspose.Cells将不会将公式视为简单文本。它们将被视为公式,Aspose.Cells的公式计算引擎将像往常一样处理它们。
以下代码说明了如何加载和导入带有公式的CSV文件。您可以使用任何CSV文件。为了演示目的,我们使用包含此数据的**简单csv文件**。如您所见,它包含一个公式。
300,500,=Sum(A1:B1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import TxtLoadOptions, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
opts = TxtLoadOptions() | |
opts.separator = ',' | |
opts.has_formula = True | |
# Load your CSV file with formulas in a Workbook object | |
workbook = Workbook(dataDir + "sample.csv", opts) | |
# You can also import your CSV file like this | |
# The code below is importing CSV file starting from cell D4 | |
worksheet = workbook.worksheets[0] | |
worksheet.cells.import_csv(dataDir + "sample.csv", opts, 3, 3) | |
# Save your workbook in Xlsx format | |
workbook.save(dataDir + "output_out.xlsx") |
代码首先加载CSV文件,然后将其再次导入到单元格D4。最后,它将工作簿对象另存为XSLX格式。**输出的XLSX文件**看起来是这样的。如您所见,单元格C3和F4包含公式及其结果800。
![]() |
---|