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

假装没事ソ 提交于 2019-12-07 04:20:24

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/12184042/how-do-i-export-directly-to-a-word-document-in-report-viewer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!