The method is as follows:
C #
public virtual void SaveAs ( [OptionalAttribute] Object Filename, [OptionalAttribute] Object FileFormat, [OptionalAttribute] Object Password, [OptionalAttribute] Object WriteResPassword, [OptionalAttribute] Object ReadOnlyRecommended, [OptionalAttribute] Object CreateBackup, [OptionalAttribute] XlSaveAsAccessMode AccessMode, [OptionalAttribute] Object ConflictResolution, [OptionalAttribute] Object AddToMru, [OptionalAttribute] Object TextCodepage, [OptionalAttribute] Object TextVisualLayout, [OptionalAttribute] Object Local)
Parameters
-
Filename
-
The name of the file to save. It can contain the full path. Otherwise, Microsoft Office Excel saves the file in the current folder.
-
Fileformat
-
The format used to save the file. For a list of valid options, see the fileformat attribute. For existing files, the default format is the last specified file format. For new files, the default format is the Excel version used.
-
Password
-
A case-sensitive string (up to 15 characters) that specifies the password to protect the file.
-
Writerespassword
-
File write protection password. If a password is specified when the file is saved, but the password is not provided when the file is opened, the file is read-only.
-
Readonlyrecommended
-
IfTrue, A message is displayed when the file is opened. We recommend that you open the file in read-only mode.
-
Createbackup
-
IfTrueTo create a backup file.
-
Accessmode
-
One of the xlsaveasaccessmode values.
-
Conflictresolution
-
Xlsaveconflictresolution value.
-
Addtomru
-
IfTrueTo add the workbook to the list of recently used files. The default value isFalse.
-
Textcodepage
-
It is not used in the US English version of Excel.
-
Textvisuallayout
-
It is not used in the US English version of Excel.
-
Local
-
IfTrueSave the file according to the language of Excel (including control panel settings. IfFalse(Default), save the file according to the language of Visual Basic for Applications (VBA.
The Code is as follows:
/// Excelfilename exce file path
/// Csvfilename CSV file path
Public static void saveasexcel (string excelfilename, string csvfilename)
{
// Define a com hollow object (similar to null)
Object missing = system. reflection. Missing. value;
// Create an Excel application object (which will help us start the Excel process)
Excel. Application APP = new excel. applicationclass ();
Workbook WB = app. application. workbooks. open (excelfilename, missing, missing );
// If no prompt is displayed, use the default option
App. application. displayalerts = false;
// Do not run the Excel Interface
App. application. Visible = false;
Excel. worksheet sheet = (Excel. worksheet) WB. worksheets ["sheet1"];
// Save it as a CSV job. Pay attention to the Excel. xlfileformat. xlcsv parameter. Save it as another format and set it here
Sheet. saveas (csvfilename, Excel. xlfileformat. xlcsv, missing, missing, false, missing, missing, false );
WB. Close (false, missing, missing );
App. Quit ();
}