[英]Save an excel file as values using C#
I have an excel file that has formulas in them. 我有一个excel文件,其中包含公式。 The formulas uses some functions that requires some third party add ins. 公式使用一些需要第三方添加的函数。 I am trying to save the excel file as csv using the following code in C#. 我试图使用C#中的以下代码将excel文件保存为csv。 But the formula fields show up as an error. 但公式字段显示为错误。 How can I save the formula fields as values and then save it in a csv. 如何将公式字段保存为值,然后将其保存在csv中。
public static void SaveExcelFileAsCsvAllSheets(string fileName)
{
var app = new Microsoft.Office.Interop.Excel.Application();
try
{
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(fileName);
app.DisplayAlerts = false;
app.ScreenUpdating = false;
int sheetCount = 1;
foreach (Microsoft.Office.Interop.Excel.Worksheet sht in wb.Worksheets)
{
if (sht.Visible == XlSheetVisibility.xlSheetVisible)
{
sht.Select();
wb.SaveAs(Path.GetDirectoryName(fileName) + @"\" + Path.GetFileNameWithoutExtension(fileName) + "-" + sheetCount.ToString() + ".csv", Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV, AccessMode: XlSaveAsAccessMode.xlNoChange);
sheetCount++;
}
}
wb.Close(false);
app.DisplayAlerts = true;
app.ScreenUpdating = true;
}
catch (Exception ex)
{
throw;
}
finally
{
app.Quit();
}
}
Select the range that contains your data, then copy and paste the values in place: 选择包含数据的范围,然后复制并粘贴值:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Range targetRange = (Excel.Range)CurrentSheet.UsedRange;
targetRange.Copy(Type.Missing);
targetRange.Paste(Excel.XlPasteType.xlPasteValues,
Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.