Windows Form 程序内怎么把DataTable内的数据导入到Word模板内,在线等,十万火急! 现在要做一个报表,要按照word设计的格式,把数据写入word模板,并且还要分页,最好还能把word文件生成PDF文件。谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 的调用word的com组件,很容易出错的 word 里面有 域这个概念。 楼主可以搞下, 就是在指定的格式下加数据 ,比较简单 没用过,生成Excel已经够麻烦的啦.不过可以考虑直接生成PDF,,使用itextsharp引用这个Dll插件,再调用相应方法. 在word模板里边写好书签,在程序里直接给指定的书签赋值。 /// <summary> /// 添写WORD文档 /// </summary> /// <param name="dotpath">模板地址</param> /// <param name="struseid">资料ID</param> public void datoword(string dotpath, string struseid) { string strsvepath;//储存目录 object missing = System.Reflection.Missing.Value; if (App == null) App = new Word.ApplicationClass(); App.Visible = false; object oTemplate = dotpath; //试过真接输出到PDF打印机。效率更低 App.ActivePrinter = "MS Publisher Color Printer"; doc = App.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing); doc.Activate(); doc.ActiveWindow.View.ShowFieldCodes = true; Word.Books bms = doc.Books; //建立D目录 strsvepath =Server.MapPath(".").ToString() + "\\doc\\"+struseid+"\\"; if (Directory.Exists(strsvepath)) { } else { Directory.CreateDirectory(strsvepath); } strsvepath = Server.MapPath(".").ToString() + "\\pdf\\" + struseid + "\\"; if (Directory.Exists(strsvepath)) { } else { Directory.CreateDirectory(strsvepath); } //添写内容 inputdata(bms, struseid); oTemplate = Server.MapPath(".") + "\\doc\\"+struseid+ "\\" + struseid +".doc"; doc.SaveAs(ref oTemplate, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); App.Quit(ref missing, ref missing, ref missing); } /// <summary> /// 将数据添入指定WORD表区域,注意列数量保持和WORD一样不然出错。 /// </summary> /// <param name="wordtab">标签位置表对像</param> /// <param name="dts">要添加的数据源</param> /// <param name="rowscont">原行数量</param> /// <param name="tablehei">行高</param> /// <param name="x">开始添加的行</param> /// <param name="y">开始添加的列</param> private void inputtable(Word.Table wordtab,DataTable dts,float tablehei,int rowscont, int x,int y) { //Word.Table newTable = bm.Range.Tables[1]; //bm.Range.Tables[1].Rows.Height; //粘贴 //bm.Range.Paste(); //添加一行 if (dts.Rows.Count > 0) { object ff = 1; //得出控制高度 tablehei = tablehei * rowscont; //bm.Application.ActiveWindow.View.ShowFieldCodes = true; float rowhei = 0; if (dts.Rows.Count > rowscont) { ff = dts.Rows.Count - rowscont; App.Selection.InsertRows(ref ff); //获得总高度,以保持高度不变 tablehei = App.Selection.Cells.Height * rowscont; rowhei = tablehei / dts.Rows.Count; } for (int b = 0; b < dts.Columns.Count; b++) { for (int i = 0; i < dts.Rows.Count; i++) { if (rowhei != 0) { wordtab.Cell(i + x, b + y).Height = rowhei; } wordtab.Cell(i + x, b + y).Range.Text = dts.Rows[i][b].ToString(); } } } } 错误提示:当前上下文中不存在名称“App” 需要引用OFFICE.WORD的。类库。 liumj2001你的QQ号是多少?我能加你咨询吗? Ajax问题 怎么把stream转变为string 急:C#读取Access2003中OLE字段保存的swf文件的方法!!! 散分啦!简单问题!大家进来帮忙解释一下c#中invoke()方法如何使用~ 在线等!满意马上给分! 服务器端如何获取post请求? combobox数据加载的问题!请教各位!急! 初学者的小问题,请大哥哥们帮帮小女子 如果让PropertyGrid显示属性以外的东西 vs2010中.cs打不开是什么原因 C#、VC进程之间如何通信? C# 重载DefWndProc的问题 有一段代码看不懂,请哪位高人帮解释一下?
引用这个Dll插件,再调用相应方法.
/// 添写WORD文档
/// </summary>
/// <param name="dotpath">模板地址</param>
/// <param name="struseid">资料ID</param>
public void datoword(string dotpath, string struseid)
{
string strsvepath;//储存目录
object missing = System.Reflection.Missing.Value;
if (App == null)
App = new Word.ApplicationClass();
App.Visible = false; object oTemplate = dotpath;
//试过真接输出到PDF打印机。效率更低
App.ActivePrinter = "MS Publisher Color Printer";
doc = App.Documents.Add(ref oTemplate, ref missing, ref missing, ref missing);
doc.Activate();
doc.ActiveWindow.View.ShowFieldCodes = true;
Word.Books bms = doc.Books; //建立D目录
strsvepath =Server.MapPath(".").ToString() + "\\doc\\"+struseid+"\\";
if (Directory.Exists(strsvepath))
{
}
else
{
Directory.CreateDirectory(strsvepath);
}
strsvepath = Server.MapPath(".").ToString() + "\\pdf\\" + struseid + "\\";
if (Directory.Exists(strsvepath))
{
}
else
{
Directory.CreateDirectory(strsvepath);
} //添写内容
inputdata(bms, struseid);
oTemplate = Server.MapPath(".") + "\\doc\\"+struseid+ "\\" + struseid +".doc"; doc.SaveAs(ref oTemplate, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
App.Quit(ref missing, ref missing, ref missing);
}
/// 将数据添入指定WORD表区域,注意列数量保持和WORD一样不然出错。
/// </summary>
/// <param name="wordtab">标签位置表对像</param>
/// <param name="dts">要添加的数据源</param>
/// <param name="rowscont">原行数量</param>
/// <param name="tablehei">行高</param>
/// <param name="x">开始添加的行</param>
/// <param name="y">开始添加的列</param>
private void inputtable(Word.Table wordtab,DataTable dts,float tablehei,int rowscont, int x,int y)
{
//Word.Table newTable = bm.Range.Tables[1];
//bm.Range.Tables[1].Rows.Height;
//粘贴
//bm.Range.Paste();
//添加一行
if (dts.Rows.Count > 0)
{
object ff = 1;
//得出控制高度
tablehei = tablehei * rowscont;
//bm.Application.ActiveWindow.View.ShowFieldCodes = true;
float rowhei = 0; if (dts.Rows.Count > rowscont)
{
ff = dts.Rows.Count - rowscont;
App.Selection.InsertRows(ref ff);
//获得总高度,以保持高度不变
tablehei = App.Selection.Cells.Height * rowscont;
rowhei = tablehei / dts.Rows.Count;
}
for (int b = 0; b < dts.Columns.Count; b++)
{
for (int i = 0; i < dts.Rows.Count; i++)
{
if (rowhei != 0)
{
wordtab.Cell(i + x, b + y).Height = rowhei; }
wordtab.Cell(i + x, b + y).Range.Text = dts.Rows[i][b].ToString();
}
}
} }
你的QQ号是多少?我能加你咨询吗?