Reputation: 4406
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?
Upvotes: 5
Views: 15064
Reputation: 483
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.
Upvotes: 12
Reputation: 2603
The LocalReport.Render for Word only exports to the older Word format (version 6, I believe). The newer, open formats based on XML (docx extension) are not supported. So by having a docx extension, Word is expecting the newer format, not the old one, so it interprets everything as rubbish. As an aside, change the docx extension on a Word document into .zip, extract the contents and have a poke around the resulting folders. It is quite interesting what is now visible.
Upvotes: 3
Reputation: 4406
So after alot of work on this thing I found that changing this line:
saveFileDialog.Filter = “Word Doc (*.docx)|*.docx|PDF (*.pdf)| *.pdf”;
to
saveFileDialog.Filter = “Word Doc (*.doc)|*.doc|PDF (*.pdf)| *.pdf”;
fixes my problem. For whatever reason saving to a .docx file corrupts the data.
Upvotes: 1