简体   繁体   中英

Is there a way to programmatically insert formulas into a csv file using vb.net?

I need to add formulas to a CSV file that already exists. Is this possible to do this using VB.Net?

The idea is I already have a CSV file and I need one column to be populated in each cell with a custom formula. This has to be done programmatically because these files are being created dynamically.

Does anyone know how to do this? Thanks.

1,2,3,=SUM(A1:C1)

Surprised me when I tried it, but Excel will keep the formula.

You can even export formulas into a CSV by first displaying them on screen. (Ctrl-`)

While I stand by that my original answer is technically correct, I have been getting a lot of downvotes on this. Apparently popular spreadsheet software such as Microsoft Excel, Open Office, Libre Office Calc will calculate formulas that are entered into a CSV file. However, I would still not recommend relying in this capability.

Original answer:
The CSV format does not support formulas. It is a plain text only format.

You can import formula's into excel via a text file using comma separated values. Just remember to make sure it is named with the suffix .txt.

Then do the import.

My example import ( Data table, From Text)

Column1,Column2,ResultColumn

1,2,=A2+B2

It imported and computed just fine

Are you generating the CSV file? If so, consider writing an actual Excel file. (I'm assuming you're importing into Excel, since you used the term "cell", which has no meaning in CSV.)

Here's a link on how to do it: Create Excel (.XLS and .XLSX) file from C#

If you aren't generating the CSV, and if all you want is to add a new, calculated value,(rather than a formula that will change dynamically as cells change values) you can do this easily enough by reading in the CSV file, parsing each line enough to get the values you need for your formula, calculating the result, and appending it (after a comma) to each line before writing the line out to a new file.

You could open the csv in Excel and then add the formulas to Excel and save back out to csv. You would do this by using the Microsoft Excel 11.0 Object Library. Then

dim wb as Excel.Workbook
dim exApp as New Excel.Application
dim exSheet as Excel.Worksheet
dim rowNum as integer = 1

wb = System.Runtime.InteropServices.Marshal.BindToMoniker(pathAndFileNameOfCSV)
exApp = wb.Parent

exApp.DisplayAlerts = False
exApp.AlertBeforeOverwriting = False

exSheet = exApp.ActiveWorkbook.Sheets.Item(1)

'do your functions in a loop here i.e.
exSheet.Range("A" & rowNum).Value = "=SUM($A$1:$D$4)"
rowNum += 1

wb.Close (True) 'closes and saves

Saving the workbook should convert the formulas back to the values when it is closed.

In Excel, to import formulas with commas the formula must be encapsulated by double quotes to prevent the formula being spread across cells. For example:

2,4,6,13,=sum(A1:C1),"=if(A1=C1,D1-A1,D1+A1)"

Another quirk of Excel is that if you have a string consisting entirely of numbers, you must present it as a formula to retain leading zeros. "00012345" imports as 12345, ignoring the quotes. To import as text, the .CSV file must present this as ="00012345".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM