Search code examples
c#reportfilestreamsavefiledialog

How to save a report directly to .doc?


I have a report and i want to save it directly without a dialog.

Here is my code until now

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = @"C:\temp";
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = "Browse Text Files";
            saveFileDialog.DefaultExt = "doc";
            saveFileDialog.Filter = "Word Doc (*.doc)|*.doc|PDF (*.pdf)| *.pdf";
            saveFileDialog.CheckFileExists = false;
            saveFileDialog.CheckPathExists = true;

            Warning[] warnings;
            string[] streams;
            string mimeType;
            string encoding;
            string extension;

            byte[] bytes = reportTest.LocalReport.Render("Word", null, out mimeType, out encoding, out extension, out streams, out warnings);

            //if (saveFileDialog.ShowDialog() == DialogResult.OK)
            //{
                saveFileDialog.FileName = "123.doc";
                var filename = saveFileDialog.FileName;
                System.IO.FileStream file = new FileStream(filename, FileMode.Create);
                file.Write(bytes, 0, bytes.Length);
                file.Close();
            //}

Can someone help me how to save the file directly to c:\temp\123.doc (without dialog in the explorer).

Thanks a lot!


Solution

  • You can directly save this with path and file name

    filename="PATH" + "test.doc";
    System.IO.FileStream file = new FileStream(filename, FileMode.Create);