加载或导入具有公式的CSV文件
Contents
[
Hide
]
CSV文件通常包含文本数据,不包含任何公式。然而,有时候CSV文件也会包含公式。此类CSV文件应通过将**TxtLoadOptions.HasFormula**设置为**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
// 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); | |
TxtLoadOptions opts = new TxtLoadOptions(); | |
opts.Separator = ','; | |
opts.HasFormula = true; | |
// Load your CSV file with formulas in a Workbook object | |
Workbook workbook = new 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 worksheet = workbook.Worksheets[0]; | |
worksheet.Cells.ImportCSV(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。
![]() |
---|