SaveFileDialog将文件保存到文档目录

时间:2018-06-23 22:02:38

标签: c# excel saving-data

我有一个奇怪的问题,我正在使用SaveFileDialog将数据保存在excel文件中。我将RestoreDirectory设置为true,将InitialDirectory设置为Environemnt.SpecialFolder.Desktop,但是该程序每次都将文件保存在Documents中。不管选择哪个目录,文件都以Documents结尾。任何想法?

 private void btnExcellExport_Click(object sender, EventArgs e)
    {
        if (!(dataGridView1.RowCount == 0))
        {
            if (backgroundWorker1.IsBusy)
                return;
            using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Excel Workbook|*.xlsx", RestoreDirectory = true, InitialDirectory = Environment.SpecialFolder.Desktop.ToString()
        })
            {
                sfd.FileName = HelpMeClass.SearchString;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    progressBar1.Show();
                    progressBar1.Minimum = 0;
                    progressBar1.Value = 0;
                    backgroundWorker1.RunWorkerAsync();

                }
            }
        }
        else
        {
            MessageBox.Show("Oops! Nothing to export!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }
    }

BackgroundWorker DoWork:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
        Workbook wb = excel.Workbooks.Add(XlSheetType.xlWorksheet);
        Worksheet ws = (Worksheet)excel.ActiveSheet;

        excel.Visible = true;
        int index = 0;

        int process = dataGridView1.Rows.Count;
        int process1 = dataGridView2.Rows.Count;
        int process2 = dataGridView3.Rows.Count;



        ws.get_Range("A1", "C1").Merge(); // Merge columns for header
        ws.Cells[1, 1] = "Keyword: " + HelpMeClass.SearchString;
        ws.Cells[1, 1].Font.Bold = true; // Bold font in header

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            backgroundWorker1.ReportProgress(index++ * 100 / process);

            foreach (DataGridViewCell cell in row.Cells)
            {
                ws.Cells[index + 1, 1] = cell.Value;
            }
        }

        index = 0;         
        foreach (DataGridViewRow row in dataGridView2.Rows)
        {
            backgroundWorker1.ReportProgress(index++ * 100 / process1);

            foreach (DataGridViewCell cell in row.Cells)
            {
                ws.Cells[index + 1, 2] = cell.Value;
            }
        }

        index = 0;
        foreach (DataGridViewRow row in dataGridView3.Rows)
        {
            backgroundWorker1.ReportProgress(index++ * 100 / process2);

            foreach (DataGridViewCell cell in row.Cells)
            {
                ws.Cells[index + 1, 3] = cell.Value;
            }
        }

        ws.Columns.AutoFit();



        try
        {
            ws.SaveAs(HelpMeClass.SearchString, XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Ooops! I can`t access the file. Make sure the excel file is closed and try again. " + ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
            return;
        }

1 个答案:

答案 0 :(得分:1)

关键是在调用Path时包括一个SaveAs,以便您完全控制文件的保存位置。参数称为Filename时,您可以按照docs传递完整路径:

  

一个字符串,指示要保存的文件的名称。 您可以   包括完整路径;如果不这样做,Microsoft Excel会将文件保存在   当前文件夹。

所以代替:

ws.SaveAs(HelpMeClass.SearchString, XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);

您会致电:

ws.SaveAs(Path.Combine(HelpMeClass.GetExcelDirectory, HelpMeClass.SearchString), XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, false, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);