关于C# 导出word文档问题?请求帮助。。。

cssf8866 2010-03-30 03:10:56
word文档导出按钮 事件如下
private void but_Table_Click(object sender, EventArgs e)
{

object Nothing = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
//创建Word文档
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//Microsoft.Office.Interop.Word.Document wordDoc =new Microsoft.Office.Interop.Word.Document();

wordApp.Visible = true;

//设置文档宽度
wordApp.Selection.PageSetup.LeftMargin = wordApp.CentimetersToPoints(float.Parse("2"));
wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled = 11;
wordApp.Selection.PageSetup.RightMargin = wordApp.CentimetersToPoints(float.Parse("2"));

Object start = Type.Missing;
Object end = Type.Missing;

PictureBox pp = new PictureBox(); //新建一个PictureBox控件
int p1 = 0;
for (int i = 0; i < MyDS_Grid.Tables[0].Rows.Count; i++)
{
try
{
byte[] pic = (byte[])(MyDS_Grid.Tables[0].Rows[i][23]); //将数据库中的图片转换成二进制流
MemoryStream ms = new MemoryStream(pic); //将字节数组存入到二进制流中
pp.Image = Image.FromStream(ms); //二进制流Image控件中显示
pp.Image.Save(@"C:\22.bmp"); //将图片存入到指定的路径
}
catch
{
p1 = 1;
}
object rng = Type.Missing;
string strInfo = "职工基本信息表" + "(" + MyDS_Grid.Tables[0].Rows[i][1].ToString() + ")";
start = 0;
end = 0;
wordDoc.Range(ref start, ref end).InsertBefore(strInfo); //插入文本
wordDoc.Range(ref start, ref end).Font.Name = "Verdana"; //设置字体
wordDoc.Range(ref start, ref end).Font.Size = 20; //设置字体大小
wordDoc.Range(ref start, ref end).ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; //设置字体局中

start = strInfo.Length;
end = strInfo.Length;
wordDoc.Range(ref start, ref end).InsertParagraphAfter();//插入回车

object missingValue = Type.Missing;
object location = strInfo.Length; //如果location超过已有字符的长度将会出错。一定要比"明细表"串多一个字符
Microsoft.Office.Interop.Word.Range rng2 = wordDoc.Range(ref location, ref location);

Microsoft.Office.Interop.Word.Table tab= wordDoc.Tables.Add(rng2, 14, 6, ref missingValue, ref missingValue);
tab.Rows.HeightRule = Microsoft.Office.Interop.Word.WdRowHeightRule.wdRowHeightAtLeast;
tab.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));
tab.Range.Font.Size = 10;
tab.Range.Font.Name = "宋体";

//设置表格样式
tab.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
tab.Borders.InsideLineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth050pt;
tab.Borders.InsideColor= Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐

//第5行显示
tab.Cell(1, 5).Merge(tab.Cell(5, 6));
。。。
//第14行显示
tab.Cell(14, 2).Merge(tab.Cell(14, 6));

//第1行赋值
tab.Cell(1, 1).Range.Text = "职工编号:";
tab.Cell(1, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][0].ToString();
tab.Cell(1, 3).Range.Text = "职工姓名:";
tab.Cell(1, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][1].ToString();

//插入图片

if (p1 == 0)
{
string FileName = @"C:\22.bmp";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor =tab.Cell(1, 5).Range; //指定图片插入的区域
//将图片插入到单元格中
tab.Cell(1, 5).Range.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
}
p1 = 0;
。。。
//第5行赋值
tab.Cell(5, 1).Range.Text = "政治面貌:";
tab.Cell(5, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][8].ToString();
tab.Cell(5, 3).Range.Text = "单位工作时间:";
try
{
tab.Cell(5, 4).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[0][10]).ToShortDateString());
}
catch {tab.Cell(5, 4).Range.Text = ""; }
//第6行赋值
tab.Cell(6, 1).Range.Text = "籍贯:";
tab.Cell(6, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][24].ToString();
tab.Cell(6, 3).Range.Text = MyDS_Grid.Tables[0].Rows[i][25].ToString();
tab.Cell(6, 4).Range.Text = "身份证:";
tab.Cell(6, 5).Range.Text = MyDS_Grid.Tables[0].Rows[i][9].ToString();
//第7行赋值
tab.Cell(7, 1).Range.Text = "工龄:";
tab.Cell(7, 2).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][11]);
tab.Cell(7, 3).Range.Text = "职工类别:";
tab.Cell(7, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][12].ToString();
tab.Cell(7, 5).Range.Text = "职务类别:";
tab.Cell(7, 6).Range.Text = MyDS_Grid.Tables[0].Rows[i][13].ToString();
//第8行赋值
tab.Cell(8, 1).Range.Text = "工资类别:";
tab.Cell(8, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][14].ToString();
tab.Cell(8, 3).Range.Text = "部门类别:";
tab.Cell(8, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][15].ToString();
tab.Cell(8, 5).Range.Text = "职称类别:";
tab.Cell(8, 6).Range.Text = MyDS_Grid.Tables[0].Rows[i][16].ToString();
//第9行赋值
tab.Cell(9, 1).Range.Text = "月工资:";
tab.Cell(9, 2).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][26]);
tab.Cell(9, 3).Range.Text = "银行帐号:";
tab.Cell(9, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][27].ToString();
//第10行赋值
tab.Cell(10, 1).Range.Text = "合同起始日期:";
try
{
tab.Cell(10, 2).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][28]).ToShortDateString());
}
catch {tab.Cell(10, 2).Range.Text = ""; }
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][28]);
tab.Cell(10, 3).Range.Text = "合同结束日期:";
try
{
tab.Cell(10, 4).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][29]).ToShortDateString());
}
catch {tab.Cell(10, 4).Range.Text = ""; }
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][29]);
tab.Cell(10, 5).Range.Text = "合同年限:";
tab.Cell(10, 6).Range.Text = Convert.ToString(MyDS_Grid.Tables[0].Rows[i][30]);
//第11行赋值
tab.Cell(11, 1).Range.Text = "电话:";
tab.Cell(11, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][17].ToString();
tab.Cell(11, 3).Range.Text = "手机:";
tab.Cell(11, 4).Range.Text = MyDS_Grid.Tables[0].Rows[i][18].ToString();
tab.Cell(11, 5).Range.Text = "毕业时间:";
try
{
tab.Cell(11, 6).Range.Text = Convert.ToString(Convert.ToDateTime(MyDS_Grid.Tables[0].Rows[i][21]).ToShortDateString());
}
catch {tab.Cell(11, 6).Range.Text = ""; }
//Convert.ToString(MyDS_Grid.Tables[0].Rows[i][21]);
//第12行赋值
tab.Cell(12, 1).Range.Text = "毕业学校:";
tab.Cell(12, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][19].ToString();
//第13行赋值
tab.Cell(13, 1).Range.Text = "主修专业:";
tab.Cell(13, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][20].ToString();
//第14行赋值
tab.Cell(14, 1).Range.Text = "家庭地址:";
tab.Cell(14, 2).Range.Text = MyDS_Grid.Tables[0].Rows[i][22].ToString();

wordDoc.Range(ref start, ref end).InsertParagraphAfter();//插入回车
wordDoc.Range(ref start, ref end).ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; //设置字体局中
}
我用的是OFFICE2003 添加了引用word组件和office组件

运行时 触发按钮时
运行到下边这行代码提示错误
Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
错误如下:

无法将类型为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对

象强制转换为接口类型“Microsoft.Office.Interop.Word._Application”。此

操作失败的原因是对 IID 为“{00020970-0000-0000-C000-000000000046}”的接

口的 COM 组件调用 QueryInterface 因以下错误而失败: 加载类型库/DLL 时出

错。 (异常来自 HRESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))。
...全文
608 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pingshizhi 2010-12-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 gramaster 的回复:]
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
改成Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Appli……
[/Quote]
这个是正确的,很多书上都是按照你说的写的,但实际上是错的。
gramaster 2010-04-07
  • 打赏
  • 举报
回复
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
改成Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
xiaojing7 2010-04-07
  • 打赏
  • 举报
回复
在dcom配置里面设置权限控制

运行-->dcomcnfg--> 找到Word/Excel文档。设置里面权限(标示和安全里面)为当前用户的权限
cssf8866 2010-03-31
  • 打赏
  • 举报
回复
晕 谁能告诉我 这是为什么啊 我感觉是OFFICE 的问题 代码应该是没有问题的啊
lanfengsese 2010-03-30
  • 打赏
  • 举报
回复
这个有点复杂,记得网上好像有专门讲这个的
cssf8866 2010-03-30
  • 打赏
  • 举报
回复
我是个新手,书上的程序 正在学习 不知道那里是重点 所以让全部代码给贴出来啦 希望高手能帮助啊
daxiao474 2010-03-30
  • 打赏
  • 举报
回复
这是来求助的还是来炫耀的哦 请问老大

111,094

社区成员

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

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

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