2011-05-20 165 views
1

我有使用savefiledialog將網格視圖保存爲html文件的代碼。我想將它保存到一個特定的路徑(不使用savefiledialog)...我該怎麼做?將文件保存到特定路徑

這裏是我的代碼:

SaveFileDialog dialog = new SaveFileDialog(); 
dialog.DefaultExt = "*.html"; 
dialog.Filter = "WORD Document (*.html)|*.html"; 

if (dialog.ShowDialog() == true) 
{ 
    RadDocument document = CreateDocument(rgvReportData); 

    document.LayoutMode = DocumentLayoutMode.Paged; 

    document.Measure(RadDocument.MAX_DOCUMENT_SIZE); 
    document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize)); 
    document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(2, 2, 2, 2); 
    document.SectionDefaultPageOrientation = PageOrientation.Landscape; 

    HtmlFormatProvider provider = new HtmlFormatProvider(); 

    using (Stream output = dialog.OpenFile()) 
    { 
     provider.Export(document, output); 
    } 
} 

我怎麼能SVE它不使用savefiledialog?

+0

這是什麼語言? – 2011-05-20 02:38:53

+0

這是C#。重新標記。 – Ryan 2011-05-20 02:40:09

回答

0
using(StreamWriter output = new StreamWriter("path\to\your\file")) { 
    provider.Export(document, output); 
} 

將做同樣的事情,但具體路徑。你可以閱讀更多關於file access on MSDN

+0

謝謝你minitech,但它沒有奏效。我試過這個:使用(StreamWriter output = new StreamWriter(@「C:\ myreport.html」)) { \t provider.Export(document,output); },錯誤是:無法從system.io.streamwriter轉換爲system.io.stream。 provider.export接受一個文檔和一個流 – loraine96 2011-05-20 05:30:50

+0

你確定嗎? StreamWriter從Stream繼承。雖然你可以嘗試一個明確的演員。 – Ryan 2011-05-20 15:00:10

0
using (var output = new FileStream("path", FileMode.Create, FileAccess.Write)) 
{ 
    provider.Export(document, output); 
} 
+0

謝謝你,但它沒有奏效。 Export()接受Stream作爲第二個參數。 – loraine96 2011-05-20 05:38:31

+0

@ lorane96 - 夠簡單。已更新以提供流。 – 2011-05-20 10:18:08

+0

@RitchMelton你能告訴我爲什麼它沒有將數據保存到特定的路徑'string FILE = @「F:\\」+ textBox1.Text;''System.IO.File.WriteAllLines(FILE +「.txt」,內容);' – AHF 2014-01-12 16:59:33

0
String fileName = "youfilename.html"; // give the full path if required 
    RadDocument document = CreateDocument(rgvReportData); 

    document.LayoutMode = DocumentLayoutMode.Paged; 

    document.Measure(RadDocument.MAX_DOCUMENT_SIZE); 
    document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize)); 
    document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(2, 2, 2, 2); 
    document.SectionDefaultPageOrientation = PageOrientation.Landscape; 

    HtmlFormatProvider provider = new HtmlFormatProvider(); 

    Stream output = File.Open(filename, FileMode.Open, FileAccess.ReadWrite); 
    provider.Export(document, output); 
} 
+0

謝謝Ajay,但它沒有工作... – loraine96 2011-05-20 05:26:54

+0

你看到任何錯誤?如果是的話,你能否提到你看到的錯誤 – AjayR 2011-05-20 08:57:34

0

我想這:

RadDocument document = CreateDocument(rgvReportData); 

document.LayoutMode = DocumentLayoutMode.Paged; 

document.Measure(RadDocument.MAX_DOCUMENT_SIZE); 
document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize)); 
document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(2, 2, 2, 2); 
document.SectionDefaultPageOrientation = PageOrientation.Landscape; 

HtmlFormatProvider provider = new HtmlFormatProvider(); 
Stream output = File.Open("C:\\SAMPLE.html", FileMode.Open, FileAccess.ReadWrite); 
provider.Export(document, output); 

但有一個安全異常錯誤。文件操作不允許。訪問路徑「C:\ SAMPLE.html」被拒絕。這怎麼能解決?

相關問題