How to Save Workbook with Formulas instead of calculated values

I am trying to convert excel to text. But I need to get formulas instead of calculated text. How is it possible. Please check the code I am using

Workbook oWorkbook = new Workbook(txtPath.Text);
WorksheetCollection wsc = oWorkbook.Worksheets;
FileStream fsFile;

TxtSaveOptions opts = new TxtSaveOptions();
opts.Separator = ’ ';
for (int i = 0; i < wsc.Count; i++)
{
fsFile = new FileStream(txtPath.Text + “.txt”, FileMode.Append);
try
{
wsc.ActiveSheetIndex = i;
oWorkbook.Save(fsFile, opts);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, “Sheet Error”);
}
finally
{
fsFile.Flush();
fsFile.Close();
}
}

Can you please let me know how to achieve the output.

Examples attached.
Input: Test.xls
Output: Test.xls.txt
Required Output: Test.xls_RequiredOutput.txt
<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-IN</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0cm; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri",sans-serif; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-fareast-language:EN-US;}

<![endif]–>

Hi,


Thanks for the template file and sample code.

Well. by default, Aspose.Cells would render calculated values against formulas in the cells in Excel to PDF or Excel to TXT conversions, it will not render formulas instead of values at all. For your specific requirements, you need to paste formula strings as values for those cells (you may update the formula cells in the sheet to replace their values with the underlying formulas). I have updated your code segment by adding a few lines to accomplish your task accordingly, kindly refer to it and you may update your code segment accordingly for your needs:
e.g
Sample code:

Workbook oWorkbook = new Workbook(“e:\test2\test.xls”);
WorksheetCollection wsc = oWorkbook.Worksheets;
FileStream fsFile;
Worksheet worksheet;
TxtSaveOptions opts = new TxtSaveOptions();
opts.Separator = ’ ';
for (int i = 0; i < wsc.Count; i++)
{
fsFile = new FileStream(“e:\test2\outtest1” + “.txt”, FileMode.Append);
try
{
worksheet = wsc[i];
Cells cells = worksheet.Cells;
//Loop through each cell in the sheet which is initialized only to save time.
foreach (Cell cell in cells)
{
if (cell.IsFormula)
{
string cellVal = cell.Formula;
cell.PutValue(cellVal);

}
}

wsc.ActiveSheetIndex = i;
oWorkbook.Save(fsFile, opts);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, “Sheet Error”);
}
finally
{
fsFile.Flush();
fsFile.Close();
}
}


Hope, this helps a bit.

Thank you.

Thanks. This works!

Hi,


Good to know that it figures out your issue now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Thank you.