c#WinForm程序将DataGridView数据写入WORD文档问题!

zhougang86 2009-05-04 10:30:13
我想把DataGridView中的数据导出到一个WORD文档 在文档中创立一个Table 然后为table的每个单元格写值 行是行 但是速度实在是太慢了 100多行的数据 跑的CPU都到100%了 希望高手帮忙搞定下!

/// <summary>
/// 导出Word
/// </summary>
/// <param name="dt">导出的数据DataTable</param>
/// <param name="isColname">是否显示列名</param>
public static void OutPutWordDT(DataTable dt, bool isColname)
{
Object Nothing = System.Reflection.Missing.Value;
Word.Application oword = new Word.Application();//word Application
Word.Document odoc = oword.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//文档
odoc.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;

try
{
//在word以表格形式存储数据
Word.Table otable = odoc.Tables.Add(oword.Selection.Range, dt.Rows.Count + 1, dt.Columns.Count, ref Nothing, ref Nothing);
otable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphThaiJustify;//设置对其方式
otable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;//设置表格边框样式

if (isColname)//列名称
{
int intcol = 0;
for (int ii = 0; ii < dt.Columns.Count; ii++)
{
intcol += 1;
otable.Cell(1, intcol).Range.Text = dt.Columns[ii].ColumnName;
otable.Cell(1, intcol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
}
}

//写表格内容
int intRow = 1;
for (int ii = 0; ii < dt.Rows.Count; ii++)
{
intRow += 1;
int intCol = 0;
for (int jj = 0; jj < dt.Columns.Count; jj++)
{
intCol += 1;
otable.Cell(intRow, intCol).Range.Text = dt.Rows[ii][jj].ToString();
otable.Cell(intRow, intCol).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;//设置单元格样式
}
}
oword.Visible = true;
}
catch (Exception) { }
finally
{
System.Diagnostics.Process[] CurrentProcess = System.Diagnostics.Process.GetProcessesByName("WINWORD");
for (int i = 0; i < CurrentProcess.Length; i++)
{
if (CurrentProcess[i].MainWindowHandle.ToInt32() == 0)
{
try
{
CurrentProcess[i].Kill();
}
catch
{
}
}
}
}
}
...全文
675 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianx02 2012-03-28
  • 打赏
  • 举报
回复
一行数据段太多,在WORD文档中显示时怎么才能换行
fychit 2011-02-17
  • 打赏
  • 举报
回复
是非常慢啊
volkswageos 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jingshuaizh 的回复:]
把datagridview的数据输出为Excel,Word的简单应用

C# code
public static void ExportData(DataGridView srcDgv,string fileName)//导出数据,传入一个datagridview和一个文件路径
{
string type = fileName.Substring(fileName.IndexOf(”.……
[/Quote]

我也使用这个方法,但是出现这个警告,你是怎么解决的啊?
volkswageos 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhougang86 的回复:]
我想把DataGridView中的数据导出到一个WORD文档 在文档中创立一个Table 然后为table的每个单元格写值 行是行 但是速度实在是太慢了 100多行的数据 跑的CPU都到100%了 希望高手帮忙搞定下!

C# code

/// <summary>
/// 导出Word
/// </summary>
/……
[/Quote]

原来速度这么慢啊,我也用了类似的方法,不知道你现在解决没有啊,我以后也会有大量的数据处理的
陌上花花 2009-05-04
  • 打赏
  • 举报
回复
学习了,帮顶。
i0876 2009-05-04
  • 打赏
  • 举报
回复
用线程慢慢处理了。。
sxmonsy 2009-05-04
  • 打赏
  • 举报
回复
这个嘛,写WORD其实本来就不快的。。。。
Z_L_H 2009-05-04
  • 打赏
  • 举报
回复
ding
蓝海D鱼 2009-05-04
  • 打赏
  • 举报
回复
把datagridview的数据输出为Excel,Word的简单应用

public static void ExportData(DataGridView srcDgv,string fileName)//导出数据,传入一个datagridview和一个文件路径
{
string type = fileName.Substring(fileName.IndexOf(”.”)+1);//获得数据类型
if (type.Equals(”xls”,StringComparison.CurrentCultureIgnoreCase))//Excel文档
{
Excel.Application excel = new Excel.Application();
try
{
excel.DisplayAlerts = false;
excel.Workbooks.Add(true);
excel.Visible = false;

for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
excel.Cells[2, i+1] = srcDgv.Columns[i].HeaderText;
}

for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{
excel.Cells[i + 3, j + 1] = srcDgv[j, i].Value;
}
}

excel.Workbooks[1].SaveCopyAs(fileName);//保存
}
finally
{
excel.Quit();
}
return;
}

//保存Word文件

if (type.Equals(”doc”, StringComparison.CurrentCultureIgnoreCase))
{

object path = fileName;

Object none=System.Reflection.Missing.Value;
Word.Application wordApp = new Word.Application();
Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
//建立表格
Word.Table table= document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count+1, srcDgv.Columns.Count, ref none, ref none);

try
{

for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
{
table.Cell(1, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
}

for (int i = 0; i < srcDgv.Rows.Count; i++)//填充数据
{
for (int j = 0; j < srcDgv.Columns.Count; j++)
{

table.Cell(i + 2, j + 1).Range.Text = srcDgv[j, i].Value.ToString();
}
}

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

}
finally
{
wordApp.Quit(ref none, ref none, ref none);
}

}

}
kbtjh 2009-05-04
  • 打赏
  • 举报
回复
学习一下
zzxap 2009-05-04
  • 打赏
  • 举报
回复
protected void Button3_Click(object sender, EventArgs e)
{
ExportDataGrid("application/ms-excel", "城市品牌报表.xls");
}

private void ExportDataGrid(string FileType, string FileName) //从DataGrid导出
{
Response.Charset = "GB2312";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);


this.GV_city.RenderControl(hw);

Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)//重载该方法 确保正常保存
{
//base.VerifyRenderingInServerForm(control);
}
zzxap 2009-05-04
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20060420/15/4701240.html
我不懂电脑 2009-05-04
  • 打赏
  • 举报
回复
写word的速度是慢
xzsnj 2009-05-04
  • 打赏
  • 举报
回复
估计很少人用word,控制各式研究较少
llsen 2009-05-04
  • 打赏
  • 举报
回复
用水晶报表吧
水晶报表导出到excel不大好,没有格式,但是导出为word格式还是蛮好的
速度也还可以
leiminlovesoft 2009-05-04
  • 打赏
  • 举报
回复
我以前用过,只是把相关的数据从程序中读出来,写入到WORD,速度是相当慢的!!
这个很大一部分都是用宏的知识!要对VBA比较熟悉~~~
tinalucky 2009-05-04
  • 打赏
  • 举报
回复
可以试一下用异步线程处理
tinalucky 2009-05-04
  • 打赏
  • 举报
回复
一方面可能是代码的效率,另一方面就是写WORD时要转化成WORD自身的编码,速度自然会慢些

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧