解决方案 »
- silverlight 实现类似word的特殊字符输入工具
- 如何从文本文件里 指定行数开始读,从最后开始读几行。。。
- 运行时线程无故退出!!窗体构造函数设断点居然没有运行到。
- 怎样实现网页跳到另外一个页面背景音乐不重复播放
- 请教:如何在XML文件中删除一子节点及其其下内容,并在相同位置再加一子节点进去
- 急急急 新手:想要winform窗体呈现后,再运行一段代码,有什么方法可以实现?
- c#中有没有类似于vb中redim的函数可以动态改变数组大小的?
- 一个关于插件的问题
- SiteMapPath不显示呀
- usb设备安全停止问题
- 请教:模式窗口先隐藏,显示下一个窗口时,主窗口会被覆盖
- 三层架构相关问题
以流的方式导入到word中#region 导出当前页DataGridView中的数据到Word中 public void ExportDataGridViewToWord(DataGridView srcDgv, ProgressBar progreesBar, SaveFileDialog sfile)
{
if (srcDgv.Rows.Count == 0)
{
MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
sfile.AddExtension = true;
sfile.DefaultExt = ".doc";
sfile.Filter = "(*.doc)|*.doc";
if (sfile.ShowDialog() == DialogResult.OK)
{
progreesBar.Visible = true;
object path = sfile.FileName;
Object none = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
//建立表格
Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count, srcDgv.Columns.Count, ref none, ref none);
try
{
for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
table.Cell(0, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
}
for (int i = 1; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{
table.Cell(i + 1, j + 1).Range.Text = srcDgv[j, i - 1].Value.ToString();
}
progreesBar.Value += 100 / srcDgv.RowCount;
}
document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
document.Close(ref none, ref none, ref none); progreesBar.Value = 100;
MessageBox.Show("数据已经成功导出到:" + sfile.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
progreesBar.Value = 0;
progreesBar.Visible = false;
}
catch(Exception e)
{
MessageBox.Show(e.Message, "友情提示", MessageBoxButtons.OK);
}
finally
{
wordApp.Quit(ref none, ref none, ref none);
}
}
}
}
#endregion