Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I export directly to a word document in report viewer

I have created a report with some data in it. I do not want the user to have to click on the forms export button and export the data to a word document. The file saves fine the problem is when I go to open the document in word its just a bunch of garbage instead of the report that was supposed to save.

my save button looks like this:

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = @“C:\”;
saveFileDialog.RestoreDirectory = true;
savefileDialog.Title = “Browse Text Files”;
saveFileDialog.DefaultExt = “docx”;

saveFileDialog.Filter = “Word Doc (*.docx)|*.docx|PDF (*.pdf)| *.pdf”;
saveFileDialog.checkFileExists = false;
saveFileDialog.CheckPathExists = true;

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

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

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
var filename = saveFileDialog.FileName;
System.IO.FileStream file = new FileStream(filename, FileMode.Create);
file.Write(bytes, 0, bytes.length);
file.close();
}

Any suggestions?

like image 439
Robert Avatar asked Aug 29 '12 18:08

Robert


1 Answers

I know this is old and already answered (sort of), but I've stumbled on this problem and you need to use "WORDOPENXML" instead of "Word" in the Render call. This way it will export to docx.

Use ListRenderingExtensions to see what extensions are available.

like image 190
Edgar Avatar answered Sep 19 '22 16:09

Edgar