简体   繁体   中英

How to change saveAs to be Download file [Microsoft.Office.Interop.Excel] C#

I have success create excel file using interop from dataset. But I create save as using hardcode Url. I want make saveAs Url to be download file showing popup windows openWith and save file file. And when saveAs file save to download folder.

....
xlWorkBook.SaveAs("d:\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();
....

Please help me to solve this.

You can use the 'Path' property to get the right path.

private void WorkbookSaveAs()
{
    if (this.FileFormat == Excel.XlFileFormat.xlWorkbookNormal)
    {
        this.SaveAs(this.Path + @"\XMLCopy.xls",
        Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
        false, false, Excel.XlSaveAsAccessMode.xlNoChange,
        missing, missing, missing, missing, missing);
}
}

Hope this helps!

private void button1_Click(object sender, EventArgs e)
{
    SaveFileDialog savefile = new SaveFileDialog();
    if (savefile.ShowDialog() == DialogResult.OK)
    {
        xlWorkBook.SaveAs("" + savefile.FileName + ".xlsx");
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
        xlWorkSheet = null;
        xlWorkBook = null;
        xlApp = null;
        GC.Collect();
    }
}

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