导出word文档

工人21 2014-08-29 01:43:28
点击button后,导出word文档。
运行至:
try
{
this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
ref myNothing, ref myNothing, ref myNothing);
}

报错,求解

源代码:
protected void Button1_Click(object sender, EventArgs e)
{
CreateAWord();
InsertText("ceshiceshiceshi ", 10, Microsoft.Office.Interop.Word.WdColor.wdColorBlack, 0, Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter);
NewLine();
InsertText("啦啦啦啦啦啦 ", 10, Microsoft.Office.Interop.Word.WdColor.wdColorBlack, 0, Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter);
NewLine();
SaveWord("测试报告");
}

/// <SUMMARY></SUMMARY>
/// word 应用对象
private Microsoft.Office.Interop.Word.Application _wordApplication;

/// <SUMMARY></SUMMARY>
/// word 文件对象
private Microsoft.Office.Interop.Word.Document _wordDocument;

/// <SUMMARY></SUMMARY>
/// 创建文档
///
public void CreateAWord()
{
//实例化word应用对象
this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
Object myNothing = System.Reflection.Missing.Value;

this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
}

/// <SUMMARY></SUMMARY>
/// 添加页眉
///
/// <PARAM name="pPageHeader" />
public void SetPageHeader(string pPageHeader)
{
//添加页眉
this._wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
//设置中间对齐
this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
//跳出页眉设置
this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
}

/// <SUMMARY></SUMMARY>
/// 插入文字
///
/// <PARAM name="pText" />文本信息
/// <PARAM name="pFontSize" />字体大小
/// <PARAM name="pFontColor" />字体颜色
/// <PARAM name="pFontBold" />字体粗体
/// <PARAM name="ptextAlignment" />方向
public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)
{
//设置字体样式以及方向
this._wordApplication.Application.Selection.Font.Size = pFontSize;
this._wordApplication.Application.Selection.Font.Bold = pFontBold;
this._wordApplication.Application.Selection.Font.Color = pFontColor;
this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
this._wordApplication.Application.Selection.TypeText(pText);
}

/// <SUMMARY></SUMMARY>
/// 换行
///
public void NewLine()
{
//换行
this._wordApplication.Application.Selection.TypeParagraph();
}

/// <SUMMARY></SUMMARY>
/// 插入一个图片
///
/// <PARAM name="pPictureFileName" />
public void InsertPicture(string pPictureFileName)
{
object myNothing = System.Reflection.Missing.Value;
//图片居中显示
this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
}

/// <SUMMARY></SUMMARY>
/// 保存文件
///
/// <PARAM name="pFileName" />保存的文件名
public void SaveWord(string pFileName)
{
object myNothing = System.Reflection.Missing.Value;
object myFileName = pFileName;
object myWordFormatDocument = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
object myLockd = false;
object myPassword = "";
object myAddto = true;
try
{
this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
ref myNothing, ref myNothing, ref myNothing);
}
catch
{
throw new Exception("导出word文档失败!");
}
}
...全文
136 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
代码太多。我愿意看至少40%文字分析的问题。
yuanmashijie 2014-08-31
  • 打赏
  • 举报
回复
贴代码太多了。。楼主可以试一下docx http://www.hellocsharp.com/code/16.aspx
by_封爱 版主 2014-08-30
  • 打赏
  • 举报
回复
你认为有人会看.... 比如google it aspose.words...来的更快...
jiandan_2536925237 2014-08-30
  • 打赏
  • 举报
回复
将Word文档数据导入到指定数据库中 //执行导入数据 private void btn_Begin_Click(object sender, EventArgs e) { btn_Begin.Enabled = false; Thread P_th = new Thread(//创建线程 () => //使用Lambda表达式 { while (true)//开始无限循环 { this.Invoke(//在窗体线程中执行 (MethodInvoker)(() =>//使用Lambda表达式 { foreach (object P_O in lbox_Task.Items) { Time P_Time = (Time)P_O;//将对象转换为Time类型 if (P_Time.Hours.ToString() == //判断时间是否相等 DateTime.Now.Hour.ToString() && P_Time.Minutes.ToString() == DateTime.Now.Minute.ToString() && P_Time.Seconds.ToString() == DateTime.Now.Second.ToString()) { if (P_Time.Execute)//判断任务是否已经执行 { P_Time.Execute = false;//设置任务为已执行 InsertData();//开始插入数据 } } } if ("0" == DateTime.Now.Hour.ToString() &&//是否重置任务 "0" == DateTime.Now.Minute.ToString() && "0" == DateTime.Now.Second.ToString()) { foreach (object P_1 in lbox_Task.Items) { ((Time)P_1).Execute = true; } }//CodeGo.net/ })); Thread.Sleep(1000);//线程挂起1秒钟 } }); P_th.IsBackground = true;//设置线程为后台线程 P_th.Start();//线程开始执行 } // 向SQL数据库插入数据方法 private void InsertData() { try { G_wa.ActiveDocument.Save();//保存文档 object P_Save = false;//创建object对象 ((Word._Application)G_wa.Application).Quit(//退出应用程序 ref P_Save, ref G_missing, ref G_missing); } catch (Exception ex) { Console.WriteLine(ex.Message); } btn_Begin.Enabled = false;//停用插入数据按钮 G_wa = new Microsoft.Office.Interop.Word.Application();//创建应用程序对象 object P_Path = Directory.GetCurrentDirectory() + @"\Word.doc"; Word.Document P_Document = G_wa.Documents.Open(//打开Word文档 ref P_Path, ref G_missing, ref G_missing, ref G_missing , ref G_missing, ref G_missing, ref G_missing, ref G_missing , ref G_missing, ref G_missing, ref G_missing, ref G_missing , ref G_missing, ref G_missing, ref G_missing, ref G_missing); try { Word.Range P_Range =//得到文档范围 P_Document.Range(ref G_missing, ref G_missing); Word.Table P_Table = P_Range.Tables[1];//得到文档内的表格对象 List<InstanceClass> P_List_InstanceClass = //建立集合对象 new List<InstanceClass>(); for (int i = 2; i < 7; i++) { try { if (P_Table.Cell(i, 1).Range.Text != "\r\a" &&//判断表格内是否已经添加信息 P_Table.Cell(i, 2).Range.Text != "\r\a" && P_Table.Cell(i, 3).Range.Text != "\r\a" && P_Table.Cell(i, 4).Range.Text != "\r\a") { P_List_InstanceClass.Add(//向数据集合中添加数据 new InstanceClass() { Name = P_Table.Cell(i, 1).Range.Text.Replace("\r\a", ""), Chinese = float.Parse(P_Table.Cell(i, 2).Range.Text.Replace("\r\a", "")), Math = float.Parse(P_Table.Cell(i, 3).Range.Text.Replace("\r\a", "")), English = float.Parse(P_Table.Cell(i, 4).Range.Text.Replace("\r\a", "")) }); } } catch (Exception ex) { MessageBox.Show(string.Format(//提示异常信息 "Word表格中第{0}行数据不正确,插入失败!" + ex.Message, (i - 1).ToString()), "错误!"); } } new DataTier(txt_Server.Text, txt_DataBase.Text, txt_UserName.Text,//向SQL数据库中插入数据 txt_PassWord.Text).InsertMessage(P_List_InstanceClass); object P_Save = false;//创建object对象 ((Word._Application)G_wa.Application).Quit(//退出应用程序 ref P_Save, ref G_missing, ref G_missing); this.Invoke(//窗体线程执行 (MethodInvoker)(() =>//使用Lambda表达式 { dgv_Message.DataSource = //绑定数据库中的数据 new DataTier(txt_Server.Text, txt_DataBase.Text, txt_UserName.Text, txt_PassWord.Text).GetMessage(); MessageBox.Show("向SQL中插入数据成功!", "提示!"); })); } catch (Exception ex) { object P_Save = false;//创建object对象 ((Word._Application)G_wa.Application).Quit(//退出应用程序 ref P_Save, ref G_missing, ref G_missing); throw new Exception(ex.Message);//将异常抛向上一层 } }
liuxiuming 2014-08-29
  • 打赏
  • 举报
回复
报什么错误?
银霜覆秋枫 2014-08-29
  • 打赏
  • 举报
回复
太长了 ,看不下去。。。

110,549

社区成员

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

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

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