简体   繁体   中英

Microsoft Office Interop Excel - Specify name of Excel file

I use Microsoft Office Interop Excel for create excel file. My problem is that I dont know how specify name of excel file.

Variables:

// Excel object references.
private Application _excelApp;
private Workbooks _books;
private _Workbook _book;
private Sheets _sheets;
private _Worksheet _sheet;

Create excel file:

_excelApp = new Application();
_books = _excelApp.Workbooks;

_book = _books.Add(_optionalValue);

_sheets = _book.Worksheets;
_sheet = (_Worksheet)(_sheets.Item[1]);
_sheet.Name = sheetName;

How can I specify name of excel file during creating excel file? Sorry for my english.

EDIT:

I know that I can use

_book.SaveAs(fileName)

Once you have created the Excel workbook and copies the data you want, you can simply save it as such

_book.SaveAs(outputPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, 
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

The Type.Missing parameters were appropriate to my code that I took this snippet from, but you may have need for inclusion.

i think you should check this . From the linked page:

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
)

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