问题是这样的。有一些Word文档。内容是一些表格。比如一个Word文档中会有一些说明性文字,然后接着一个表格。然后又是文字。接着又是表格。
现在想通过程序,将文档中的表格信息读取出来,然后建立成数据库。问题是,如何读取word文档的信息出来。
上网搜了一些资料。还是没有太多的章法。希望给位高手能给点建议和思路。
现在想通过程序,将文档中的表格信息读取出来,然后建立成数据库。问题是,如何读取word文档的信息出来。
上网搜了一些资料。还是没有太多的章法。希望给位高手能给点建议和思路。
解决方案 »
- 如何用js实现点击文字展开和收缩treeview
- sql server 2008 数据库查询问题。用'a,b,c,d'格式查询出的值再变成'e,f,g,h'的格式
- C# 摄像头录像问题
- 用户控件的使用
- 刪除3天前的文件
- 求助
- 为啥我的postmessage 到记事本的按键信息无效, 哪位大大帮我分析一下?
- asp.net操作mdb数据库
- LogRecord,Clerk,Com+,Transaction的问题?大给分啊.少了再给~~我还有1400多分.全给~
- 帮忙啊``~都来看~~.net的问题
- VB可以中调用C#的窗体不??调用C#的DLL及调用C#中的类的方法已经实现
- 有誰用過ResourceManager.Getstring()函數取出對應語言的資源文件 resx中的資源
{
System.Data.DataTable dt = new System.Data.DataTable();
//获取选定的ID号
string personId =listBox1.Text;
//字符型转化为整型
int x = Int32.Parse(personId);
//连接数据库获取结果集
PersonInfo pi = new PersonInfo();
dt = pi.getPersonInfo(x); //浏览文件夹保存文件
string savePath = "";
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
savePath = folderBrowserDialog1.SelectedPath;
}
object filename = savePath + "/" + "项目任务书"; object missing = System.Reflection.Missing.Value;
/* \endofdoc is a predefined book */
object endOfDoc = "\\endofdoc";
//Word应用程序对象
Word._Application wordApp;
//Word文档对象
Word._Document wordDoc;
wordApp = new Word.Application();
//wordApp.Caption = "My New Caption";
wordApp.Visible = true;
wordDoc = wordApp.Documents.Add(ref missing, ref missing,ref missing, ref missing); //使用默认打印机打印文档
/*
wordDoc.PrintOut( 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,ref missing,ref missing,ref missing);
*/ //添加页眉
if(wordApp.ActiveWindow.ActivePane.View.Type == Word.WdViewType.wdNormalView || wordApp.ActiveWindow.ActivePane.View.Type == Word.WdViewType.wdOutlineView)
{
wordApp.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView;
}
wordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
//wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("Header"); string sHeader = "XXXXXX股份有限公司";
wordApp.Selection.HeaderFooter.LinkToPrevious = false;
wordApp.Selection.HeaderFooter.Range.Text = sHeader;
wordApp.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument; //Insert a paragraph at the beginning of the document.
Word.Paragraph para1;
para1 = wordDoc.Content.Paragraphs.Add(ref missing);
para1.Range.Font.Bold = 1;//字体加粗
//para1.Range.Text = " 职工调查表";//title
para1.Range.Font.Size = 12;
para1.Range.Font.Name = "Arial";
para1.Range.Text = " 项目任务书";//title
//para1.Format.SpaceAfter = 7.3f; //7.3 pt spacing after paragraph.
para1.Range.InsertParagraphAfter(); //Insert another paragraph.
Word.Paragraph para2;
//利用了book确定text的插入位置
object oRng = wordDoc.Books.Item(ref endOfDoc).Range; //Insert a picture
wordDoc.InlineShapes.AddPicture("C:\\logo_color.gif",ref missing,ref missing,ref oRng); para2 = wordDoc.Content.Paragraphs.Add(ref oRng);
para2.Range.Text = "Id号: "+dt.Rows[0]["Id"].ToString()+" 总消费: "+dt.Rows[0]["all_money"].ToString()+" ";
para2.Range.Font.Bold = 0;
//para2.Format.SpaceAfter = 24;
para2.Range.InsertParagraphAfter();
//Insert a 3 x 5 table, fill it with data, and make the first row
Word.Table table;
Word.Range wrdRng = wordDoc.Books.Item(ref endOfDoc).Range;
table = wordDoc.Tables.Add(wrdRng, 8, 6, ref missing, ref missing);
table.Range.ParagraphFormat.SpaceAfter = 4;
/*
int r, c;
string strText;
//生成8行6列的表
for(r = 1; r <= 8; r++)
{
for(c = 1; c <= 6; c++)
{
strText = "r" + r + "c" + c;
//给表的每个格赋值
table.Cell(r, c).Range.Text = strText;
}
}
*/
//wordDoc.Paragraphs.First.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; table.Cell(1,1).Range.Text="性别";
table.Cell(1,2).Range.Text=dt.Rows[0]["sex"].ToString();
table.Cell(1,3).Range.Text="年龄";
table.Cell(1,4).Range.Text=dt.Rows[0]["age"].ToString();
table.Cell(1,5).Range.Text= "婚否";
table.Cell(1,6).Range.Text=dt.Rows[0]["marriage"].ToString();
table.Cell(2,1).Range.Text= "出生地";
table.Cell(2,2).Range.Text=dt.Rows[0]["brith_province"].ToString()+" 省 "+dt.Rows[0]["brith_city"].ToString()+" 市(县)";
table.Cell(1,1).Range.Font.Bold = 1;//设置粗体
table.Cell(1,1).Range.Font.Color = Word.WdColor.wdColorBlue;//设置表头字体颜色
table.Cell(2,1).Shading.Texture = Word.WdTextureIndex.wdTexture25Percent;//设置阴影 //Add some text after the table.
Word.Paragraph para3;
oRng = wordDoc.Books.Item(ref endOfDoc).Range;
para3 = wordDoc.Content.Paragraphs.Add(ref oRng);
para3.Range.InsertParagraphBefore();
para3.Range.Text = "注:\n 1. 此表由项目管理部门填写,主管开发的副总经理审批,发给开发部门的项目组。\n 2. 功能描述可参考有关需求文档。\n 3. 此页不足记录时,可以有附页,附页格式自定,总页数包括所有附页。";
para3.Format.SpaceAfter = 24;
para3.Range.InsertParagraphAfter();
//限制word文档不能复制、粘贴功能,只能被查看
if(wordApp.ActiveDocument.ProtectionType==Word.WdProtectionType.wdNoProtection)
{
wordApp.ActiveDocument.Protect(Word.WdProtectionType.wdAllowOnlyComments ,ref missing ,ref missing);
} /*关闭文档而不保存*/
//object SaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
//wordDoc.Close( ref SaveChanges, ref missing, ref missing); /*设置表第一行的属性*/
//table.Rows.First.Range.Font.Italic = 1;
//table.Rows.First.Range.Font.Color = Word.WdColor.wdColorBlue;//设置表头字体颜色
//table.Rows.First.Range.Font.Bold = 1;//设置粗体
//table.Rows.First.Shading.Texture = Word.WdTextureIndex.wdTexture25Percent;//设置阴影 //保存文档
wordDoc.SaveAs(ref filename,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);
//关闭文档
wordDoc.Close(ref missing, ref missing, ref missing);
//推出文档
wordApp.Quit(ref missing, ref missing, ref missing);
}
很希望看过或用过的朋友提出你宝贵的意见
我的email: [email protected]
=======================================================
using System;
using System.Data;
using System.Reflection;
using System.IO;
using Microsoft.Office.Core;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;namespace compass.Common
{
/// <summary>
/// 作者:emanlee
/// 功能描述:对Word进行操作
/// 创建时间:2006-04-7
/// 说明:在工程中需要添加 Word 11.0对象库的引用(Office 2000为Word 9.0,Office XP为Word 10.0);
/// 方法:添加引用-com-microsoft word 11.0 object library;
/// 需要在Dcom中配置Wordl应用程序的权限;
/// 服务器需要安装Office2003
/// help:Visual Studio Tools for Office 文档—〉使用 Office 对象模型实现应用程序自动化 —〉Word 任务
/// 目录:office开发:开发工具和语言:visual studio文档:Visual Studio Tools for Office 文档—〉
/// 使用 Office 对象模型实现应用程序自动化 —〉Word 任务
/// </summary>
public class WordLib
{ #region Variables
private Word.Application WordApp = null;
private string WordOpenFileName = ""; //操作Word的路径
private string WordSaveFileName = ""; //保存Word的路径
object missing = System.Type.Missing;
#endregion #region Properties public string OpenFileName
{
get
{
return WordOpenFileName;
}
set
{
WordOpenFileName = value;
}
}
public string SaveFileName
{
get
{
return WordSaveFileName;
}
set
{
WordSaveFileName = value;
}
}
#endregion //
//--------------------------------------------------------------------------------------------------------
/// <summary>
/// 构造函数;
/// </summary>
public WordLib()
{
}
public bool NewWordApp()
{
if (WordApp != null) CloseWordApp();
WordApp = new Word.ApplicationClass();
WordApp.Visible = false;
return true;
} public bool NewWordFile()
{
//创建基于 Normal.dot 的新文档
object missing = System.Type.Missing;
//object missing = System.Type.Missing;
WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
return true;
}
public bool NewWordFile(object template)
{
//使用自定义模板
//首先检查模板文件是否存在
if (template.ToString() == "")
{
throw new Exception("请选择文件!"); }
if (!File.Exists(template.ToString()))
{
throw new Exception(template + "该文件不存在!");//该异常如何处理,由什么处理???? }
//object template = @"C:\Test\SampleTemplate.dot";
object missing = System.Type.Missing;
WordApp.Documents.Add(ref template, ref missing, ref missing, ref missing);
return true;
} public bool SaveWordFile(object fileName)
{
//保存与项目关联的文档
Word.DocumentClass doc = WordApp.Documents.get_Item(ref fileName) as Word.DocumentClass;
doc.Save();
return true;
}
public bool SaveActiveWordFile()
{
//保存活动文档
WordApp.ActiveDocument.Save();
return true;
} public bool SaveWordFileAS(object fileName)
{
//使用 SaveAs 保存文档
//object fileName = @"C:\Test\NewDocument.doc"; WordApp.ActiveDocument.SaveAs(ref fileName,
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);
return true;
}
public bool CloseWordFile(object fileName)
{
//在不提示用户的前提下关闭文档并保存所做的更改
//fileName = "NewDocument.doc";
object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
Word.DocumentClass doc = WordApp.Documents.get_Item(ref fileName) as Word.DocumentClass;
object missing = System.Type.Missing;
doc.Close(ref doNotSaveChanges, ref missing, ref missing);
return true;
} //--------------------------------------------------------------------------------------------------------
/// <summary>
/// 关闭Word文件,释放对象;最后一定要调用此函数,否则会引起异常
/// </summary>
/// <param></param>
public void CloseWordApp()
{
try
{
if (WordApp != null)
{
WordApp = null;
}
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
public void Quit()
{
object missing = System.Type.Missing;
WordApp.Application.Quit(ref missing, ref missing, ref missing);
} public void Save()
{
WordApp.ActiveDocument.Save();
}
public void SaveAs(string strFileName)
{
object missing = System.Type.Missing;
object fileName = strFileName;
WordApp.ActiveDocument.SaveAs(ref fileName, 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);
} public void SaveAsHtml(string strFileName)
{
object missing = System.Type.Missing;
object fileName = strFileName;
object Format = (int)Word.WdSaveFormat.wdFormatHTML;
WordApp.ActiveDocument.SaveAs(ref fileName, ref Format, 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);
WordApp.ActiveDocument.Close(ref missing, ref missing, ref missing); }
public void CopyAll()
{
WordApp.Selection.WholeStory();
WordApp.Selection.Copy(); }
{ WordApp.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault); }
public void Clear()
{
//删除整个文档的内容
object Unit = (int)Word.WdUnits.wdCharacter;
object Count = 1;
WordApp.Selection.WholeStory();
WordApp.Selection.Delete(ref Unit, ref Count);
} public void InsertText(string strText)
{
WordApp.Selection.TypeText(strText);
}
public void InsertLineBreak()
{
WordApp.Selection.TypeParagraph();
}
public void InsertLineBreak(int nline)
{
for (int i = 0; i < nline; i++)
WordApp.Selection.TypeParagraph();
} public void SetAlignment(string strType)
{
switch (strType)
{
case "Center":
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
break;
case "Left":
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
break;
case "Right":
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
break;
case "Justify":
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
break;
}
}
public void SetFont(string strType)
{
switch (strType)
{
case "Bold":
WordApp.Selection.Font.Bold = 1;
break;
case "Italic":
WordApp.Selection.Font.Italic = 1;
break;
case "Underlined":
WordApp.Selection.Font.Subscript = 0;
break;
} }
public void SetFont()
{
WordApp.Selection.Font.Bold = 0;
WordApp.Selection.Font.Italic = 0;
WordApp.Selection.Font.Subscript = 0; }
public void SetFontName(string strType)
{
WordApp.Selection.Font.Name = strType; }
public void SetFontSize(int nSize)
{
WordApp.Selection.Font.Size = nSize; }
public void InsertPagebreak()
{ object pBreak = (int)Word.WdBreakType.wdPageBreak;
WordApp.Selection.InsertBreak(ref pBreak);
} public void GotoBookMark(string strBookMarkName)
{ object missing = System.Type.Missing;
object Book = (int)Word.WdGoToItem.wdGoToBook;
object NameBookMark = strBookMarkName;
WordApp.Selection.GoTo(ref Book, ref missing, ref missing, ref NameBookMark);
}
public void GoToTheEnd()
{ object missing = System.Type.Missing;
object unit;
unit = Word.WdUnits.wdStory;
WordApp.Selection.EndKey(ref unit, ref missing); }
public void GoToTheBeginning()
{ object missing = System.Type.Missing;
object unit;
unit = Word.WdUnits.wdStory;
WordApp.Selection.HomeKey(ref unit, ref missing); }
public void GoToTheTable(int ntable)
{
object missing = System.Type.Missing;
object what;
what = Word.WdUnits.wdTable;
object which;
which = Word.WdGoToDirection.wdGoToFirst;
object count;
count = 1;
WordApp.Selection.GoTo(ref what, ref which, ref count, ref missing);
WordApp.Selection.Find.ClearFormatting();
WordApp.Selection.Text = "";
}
public void GoToRightCell()
{ object missing = System.Type.Missing;
object direction;
direction = Word.WdUnits.wdCell;
WordApp.Selection.MoveRight(ref direction, ref missing, ref missing);
}
public void GoToLeftCell()
{ object missing = System.Type.Missing;
object direction;
direction = Word.WdUnits.wdCell;
WordApp.Selection.MoveLeft(ref direction, ref missing, ref missing);
}
public void GoToDownCell()
{ object missing = System.Type.Missing;
object direction;
direction = Word.WdUnits.wdLine;
WordApp.Selection.MoveDown(ref direction, ref missing, ref missing);
}
public void GoToUpCell()
{ object missing = System.Type.Missing;
object direction;
direction = Word.WdUnits.wdLine;
WordApp.Selection.MoveUp(ref direction, ref missing, ref missing);
}
public void InsertPageNumber(string strType, bool bHeader)
{
object missing = System.Type.Missing;
object alignment;
object bFirstPage = false;
object bF = true;
switch (strType)
{
case "Center":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
//WordApp.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
break;
case "Right":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
//WordApp.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
break;
case "Left":
alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
WordApp.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
break;
}
} //-------------------------------------------------------------------------------------------------------- private void ReleaseAllRef(Object obj)
{//ReleaseComObject()方法可以使RCW减少一个对COM组件的引用,并返回减少一个引用后RCW对COM组件的剩余引用数量。
//我们用一个循环,就可以让RCW将所有对COM组件的引用全部去掉。
try
{
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) > 1) ;
}
finally
{
obj = null;
}
} //public Word.Book GetBook(object bookName)
//{
////WordApp.ActiveDocument.Books //} public void UpdateBook(Word.Book book, string newText)
{
object rng = book.Range;
string bookName = book.Name;
book.Range.Text = newText;
WordApp.ActiveDocument.Books.Add(bookName, ref rng);
}
{
object name = bookName;
Word.Range rng = WordApp.ActiveDocument.Books.get_Item(ref name).Range;
rng.Text = newText;
object range = rng;
WordApp.ActiveDocument.Books.Add(bookName, ref range);
} public void UpdateTableContent(int tableID, int lineID, int columnID, string context)
{//更改某表的某个单元格的内容
Word.Table tbl = WordApp.ActiveDocument.Tables[tableID];
tbl.Cell(lineID, columnID).Range.Text = context;
} //将图片添加到文档中当前选定的位置
//调用 InlineShapes 对象的 AddPicture 方法,并传入文件名。
public void InsertPicture(string bookName, string pictureFileName)
{
object name = bookName;
Word.Range rng = WordApp.ActiveDocument.Books.get_Item(ref name).Range;
rng.InlineShapes.AddPicture(pictureFileName,
ref missing, ref missing, ref missing);
}
public void InsertPicture(string pictureFileName)
{
WordApp.Selection.InlineShapes.AddPicture(pictureFileName,
ref missing, ref missing, ref missing);
// rng.InlineShapes.AddPicture(pictureFileName,
//ref missing, ref missing, ref missing);
} public void DeleteBook(string bookName)
{
//删除书签
//object name = bookName;
//Word.Range rng = WordApp.ActiveDocument.Books.get_Item(ref name).Range;
//object wd = Word.WdUnits.wdCharacter;
//rng.Delete(wd, 1);
} public void DeleteText(string text)
{
//删除查找到的文本
object findText = text;
object replaceWith = "";
object replaceAll = Word.WdReplace.wdReplaceAll;
this.WordApp.Selection.Find.ClearFormatting();
this.WordApp.Selection.Find.Replacement.ClearFormatting();
this.WordApp.Selection.Find.Replacement.Text = "";
this.WordApp.Selection.Find.Execute(ref findText,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref replaceWith, ref replaceAll, ref missing, ref missing,
ref missing, ref missing); object unit = (int)Word.WdUnits.wdCharacter;
object count = 1;
this.WordApp.Selection.Delete(ref unit, ref count);
}
public int CountPage()
{
//统计页数
Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
int num = this.WordApp.ActiveDocument.ComputeStatistics(stat, ref missing);
return num;
} }
}
很少看到Word分析的实例。只有Word读取的。相关帮助又找不到所以很郁闷
object oReadOnly = true;
object oMissing = System.Reflection.Missing.Value;Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Open(ref oFileName, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);//MessageBox.Show(oDoc.Tables.Count.ToString());
for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
{
Word.Table nowTable = oDoc.Tables.Item(tablePos);
string tableMessage = string.Format("第{0}/{1}个表:\n", tablePos, oDoc.Tables.Count); for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
{
for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
{
tableMessage += nowTable.Cell(rowPos, columPos).Range.Text;
tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove \r\a
tableMessage += "\t";
} tableMessage += "\n";
} MessageBox.Show(tableMessage);
}TestDoc.doc是有着几个n*m的表信息的文档,如下:
The first table:
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
The second table:
Aa Ab Ac Ad
Ba Bb Bc Bd
The third table:
一一 一二 一三 一四 一五
二一 二二 二三 二四 二五现在就是提取出表对表进行分析,对我来说足够了。如果还有更多需要。我按楼上给的地址找到了这篇帮助,如下:
http://msdn2.microsoft.com/zh-CN/library/78whx7s6.aspx给我最大的启示就是:用com用C#就是自虐,还是vb结合的更好一些