将文件保存到特定路径
我有使用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?
using(StreamWriter output = new StreamWriter("path\to\your\file")) {
provider.Export(document, output);
}
将做同样的事情,但具体路径。你可以阅读更多关于file access on MSDN。
谢谢你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
你确定吗? StreamWriter从Stream继承。虽然你可以尝试一个明确的演员。 – Ryan 2011-05-20 15:00:10
using (var output = new FileStream("path", FileMode.Create, FileAccess.Write))
{
provider.Export(document, output);
}
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);
}
我想这:
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”被拒绝。这怎么能解决?
这是什么语言? – 2011-05-20 02:38:53
这是C#。重新标记。 – Ryan 2011-05-20 02:40:09