silverlight5中如何将字符串导出成Word(注:字符串中带有word样式,例如字体,字号等)。
1个回答
展开全部
这里有一个利用Telerik导出到Word的思路,你可以结合下自己的情况进行少许改进!
1,添加导出和打印按钮
<StackPanel Orientation="Horizontal" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="20,0">
<Button x:Name="ExportButton" Content="导出" Click="ExportButton_Click"/>
<Button x:Name="PrintButton" Content="打印" Click="ExportButton_Click"/>
</StackPanel>
2,定义
private RadGridView grid = new RadGridView();
private GridViewExportOptions options = new GridViewExportOptions();
3,初始化
grid.ElementExported += new EventHandler<GridViewElementExportedEventArgs>(MainDataGridView_ElementExported);
grid.AutoGenerateColumns = false;
options.ShowColumnHeaders = true;
options.Format = ExportFormat.Html;
options.Encoding = System.Text.Encoding.UTF8;
4,利用Telerik的RadGridView进行导出
public T_Bas_EnterList Model
{
get { return this.DataContext as T_Bas_EnterList; }
}
private void ExportButton_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "doc";
dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "doc", "Word");
dialog.FilterIndex = 1;
if (dialog.ShowDialog() == true)
{
using (Stream stream = dialog.OpenFile())
{
if (Model != null)
{
grid.ItemsSource = new List<T_Bas_EnterList>() { Model };
grid.Columns.Add(new GridViewDataColumn()
{
Header = "企业详细信息", DataMemberBinding = new System.Windows.Data.Binding("entername")
});
}
grid.Export(stream, options);
}
}
}
void DataGridView_ElementExported(object sender, GridViewElementExportedEventArgs e)
{
if (e.Element == ExportElement.Row)
{
if (Model != null)
{
e.Writer.Write(String.Format(@"<tr><td style=""background-color:#CCC;"" colspan=""{0}"">",
((IEnumerable<Telerik.Windows.Controls.GridViewColumn>)((RadGridView)sender).Columns).Count()));
e.Writer.Write("<h2>基本信息</h2>");
e.Writer.Write(String.Format(@"<b>污染源:</b> {0} <br />", Model.code_pollute));
e.Writer.Write(String.Format(@"<b>企业名称:</b> {0} <br />", Model.entername));
e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />", Model.code_enterrelation));
e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />", Model.code_qualification));
e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />", Model.code_enterrelation));
e.Writer.Write("<h2>监测数据</h2>");
MemoryStream memoryStream = new MemoryStream();
DataGridView.Export(memoryStream, options);
memoryStream.Flush();
e.Writer.Write(System.Text.Encoding.UTF8.GetChars(memoryStream.ToArray()));
e.Writer.Write("</td></tr>");
}
}
}
1,添加导出和打印按钮
<StackPanel Orientation="Horizontal" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="20,0">
<Button x:Name="ExportButton" Content="导出" Click="ExportButton_Click"/>
<Button x:Name="PrintButton" Content="打印" Click="ExportButton_Click"/>
</StackPanel>
2,定义
private RadGridView grid = new RadGridView();
private GridViewExportOptions options = new GridViewExportOptions();
3,初始化
grid.ElementExported += new EventHandler<GridViewElementExportedEventArgs>(MainDataGridView_ElementExported);
grid.AutoGenerateColumns = false;
options.ShowColumnHeaders = true;
options.Format = ExportFormat.Html;
options.Encoding = System.Text.Encoding.UTF8;
4,利用Telerik的RadGridView进行导出
public T_Bas_EnterList Model
{
get { return this.DataContext as T_Bas_EnterList; }
}
private void ExportButton_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "doc";
dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "doc", "Word");
dialog.FilterIndex = 1;
if (dialog.ShowDialog() == true)
{
using (Stream stream = dialog.OpenFile())
{
if (Model != null)
{
grid.ItemsSource = new List<T_Bas_EnterList>() { Model };
grid.Columns.Add(new GridViewDataColumn()
{
Header = "企业详细信息", DataMemberBinding = new System.Windows.Data.Binding("entername")
});
}
grid.Export(stream, options);
}
}
}
void DataGridView_ElementExported(object sender, GridViewElementExportedEventArgs e)
{
if (e.Element == ExportElement.Row)
{
if (Model != null)
{
e.Writer.Write(String.Format(@"<tr><td style=""background-color:#CCC;"" colspan=""{0}"">",
((IEnumerable<Telerik.Windows.Controls.GridViewColumn>)((RadGridView)sender).Columns).Count()));
e.Writer.Write("<h2>基本信息</h2>");
e.Writer.Write(String.Format(@"<b>污染源:</b> {0} <br />", Model.code_pollute));
e.Writer.Write(String.Format(@"<b>企业名称:</b> {0} <br />", Model.entername));
e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />", Model.code_enterrelation));
e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />", Model.code_qualification));
e.Writer.Write(String.Format(@"<b>企业类型:</b> {0} <br />", Model.code_enterrelation));
e.Writer.Write("<h2>监测数据</h2>");
MemoryStream memoryStream = new MemoryStream();
DataGridView.Export(memoryStream, options);
memoryStream.Flush();
e.Writer.Write(System.Text.Encoding.UTF8.GetChars(memoryStream.ToArray()));
e.Writer.Write("</td></tr>");
}
}
}
追问
这个方法我已经在网上看过了。不行的,因为他用到的是Telerik里面的DataGirid控件。这个控件会把字符串中的Word格式按照字符串原样输出。而不是转换成黑体,20号字体等样式显示。我就是想找个能直接输出字符串到word,然后在word里面把字符串里面的粗体,斜体之类的样式显示出来。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询