2012-08-29 34 views
5

我已經創建了一個包含一些數據的報表。我不希望用戶必須單擊表單導出按鈕並將數據導出到Word文檔。該文件可以很好地保存問題,當我打開文檔時,它只是一堆垃圾,而不是本來應該保存的報告。如何在報表查看器中直接導出到word文檔

我保存按鈕看起來是這樣的:

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(); 
} 

有什麼建議?

+0

什麼「東西沒有定義」?你能具體嗎? – codingbiz

+0

不要忽略你的錯誤。看看*它說什麼是未定義的。它試圖幫助你... –

+0

DropDownList1,httpContext.Current.Response.BinaryWrite(bytes)未定義 – Robert

回答

3

LocalReport.Render for Word只導出到舊的Word格式(我相信版本6)。不支持基於XML(docx擴展名)的更新的開放格式。因此,通過擴展docx,Word會期待更新的格式,而不是舊格式,因此它將所有內容解釋爲垃圾。另外,將Word文檔上的docx擴展名更改爲.zip,提取內容並對結果文件夾進行捅破。現在可以看到的是非常有趣的。

1

所以在這件事上的工作很多,我發現,改變這一行:

saveFileDialog.Filter = 「Word Doc (*.docx)|*.docx|PDF (*.pdf)| *.pdf」; 

saveFileDialog.Filter = 「Word Doc (*.doc)|*.doc|PDF (*.pdf)| *.pdf」; 

修復我的問題。無論出於何種原因,保存到.docx文件都會破壞數據。

10

我知道這是舊的,已經回答(有點),但我偶然發現了這個問題,你需要在渲染調用中使用「WORDOPENXML」而不是「Word」。這樣它會輸出到docx。

使用ListRenderingExtensions查看可用的擴展。

相關問題