how to specify path and filename while creating a

2019-09-02 21:47发布

i am able to create an excel file by using this:

protected void excldwnlbtn4_Click(object sender, EventArgs e)
{
    write_on_excel();
}

public void write_on_excel()
{
    Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
    try
    {

        Excel.Workbook workbook = (Excel.Workbook)excelApp.Workbooks.Add(Missing.Value);
        Excel.Worksheet worksheet;






        // Get first Worksheet
        worksheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);

        // Setting cell values

        ((Excel.Range)worksheet.Cells[1, "A"]).Value2 = "Vendor Code";
        ((Excel.Range)worksheet.Cells[1, "B"]).Value2 = "Vendor Password";
        ((Excel.Range)worksheet.Cells[1, "C"]).Value2 = "Vendor Name";


        workbook.Save();
        workbook.Close(0, 0, 0);
    }
    catch (Exception)
    {
        lblmsg4.Text = "File not Downloaded!!";
    }
    finally
    {
        excelApp.Quit();
    }
}

by this i am creating a .xlsx file in my c:\documents as book1.xlsx but i want to specify the folder path and file name how can i do this?

标签: c# asp.net excel
1条回答
forever°为你锁心
2楼-- · 2019-09-02 22:22

Use a Save As

object missing = System.Reflection.Missing.Value;

workbook.SaveAs(@"c:\documents\book1.xlsx",
            Excel.XlFileFormat.xlOpenXMLWorkbook, missing, missing,
            false, false, Excel.XlSaveAsAccessMode.xlNoChange,
            missing, missing, missing, missing, missing);
查看更多
登录 后发表回答