C# asp.net 操作Word的前提配置和简单的方法

操作的前提:

1.要保证机器本身要安装OFFICE.

有时安装了Office,但是不能找到Microsoft Word 11.0(或者更高的版本) Object Library。那可能是因为在安装office的时候没有选在。net可编程性支持

此时只需要重新打开office安装文件-》添加或删除功能-》下一步-》在Microsoft Word下点选。net可编程性支持

以Office 2003为例

2.用VS打开一个asp.net网站,右键单击Bin ->添加引用,在COM中选择Microsoft Word 11.0(或者更高的版本) Object Library。点击确定后会在Bin文件夹中出现

 

Microsoft.Office.Interop.Word.dll 

或者Interop.Word.dll 

如果没有Bin文件夹,就右键根目录,选择添加ASP.NET文件->Bin

至此前提配置就完成了

 

编程部分:

添加引用

 

[html]  view plain copy
 
  1. using Microsoft.Office.Core;  
  2. using Microsoft.Office.Interop;  
  3. using Microsoft.Office.Interop.Word;  

 

 

编程打开Word文件,和Word文件夹建立连接

 

[html]  view plain copy
 
  1. object oReadOnly = true;  
  2. object oMissing = System.Reflection.Missing.Value;  
  3. Word._Application oWord;  
  4. Word._Document oDoc;  
  5. oWord = new Word.Application();  
  6. oWord.Visible = false;//只是为了方便观察,为true时,会显示一个打开的Word  
  7. object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();  
  8. oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);  

关闭和Word的连接

 

 

[html]  view plain copy
 
  1. oDoc.Close(ref missing, ref missing, ref missing);  
  2. oWord.Quit(ref missing, ref missing, ref missing);  

对Word中的表格的数据读取,并在网页中简单显示

 

 

[html]  view plain copy
 
  1. protected void PageFucword()  
  2.     {  
  3.         //object oMissing = System.Reflection.Missing.Value;  
  4.         //Word._Application oWord;  
  5.         //Word._Document oDoc;  
  6.         //oWord = new Word.Application();  
  7.         //oWord.Visible = true;  
  8.         //  
  9.         //oDoc = oWord.Documents.Add(ref fileName, ref oMissing,ref oMissing, ref oMissing);  
  10.   
  11.         object oReadOnly = true;  
  12.         object oMissing = System.Reflection.Missing.Value;  
  13.         Word._Application oWord;  
  14.         Word._Document oDoc;  
  15.         oWord = new Word.Application();  
  16.         oWord.Visible = false;//只是为了方便观察  
  17.         object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();  
  18.         oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);  
  19.         //MessageBox.Show(oDoc.Tables.Count.ToString());  
  20.         for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)  
  21.         {  
  22.             Word.Table nowTable = oDoc.Tables[tablePos];  
  23.             string tableMessage = string.Format("第{0}/{1}个表:\n", tablePos, oDoc.Tables.Count);  
  24.             // int i = 0;  
  25.             // foreach (Word.InlineShape shape in nowTable.Cell(28, 1).Range.InlineShapes)  
  26.             //{  
  27.             //直接读取图片  
  28.             //if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)  
  29.             //{  
  30.             //    //获取Word中的图片  
  31.             //    byte[] img = (byte[])shape.Range.EnhMetaFileBits;  
  32.             //    Bitmap bmp = new Bitmap(new System.IO.MemoryStream(img));  
  33.             //    bmp.Save("c:\\pic" + i.ToString() + ".jpg ");  
  34.             //}  
  35.             //i++;  
  36.             //从剪切板上读取图片,还无法实现  
  37.             //if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)  
  38.             //{  
  39.             //    shape.Select();  
  40.             //    oWord.Selection.Copy();  
  41.             //    Image image = Clipboard.GetImage();  
  42.   
  43.             //    Bitmap bitmap = new Bitmap(image);  
  44.             //    bitmap.Save("c:\\pic " + i.ToString() + ".jpg ");  
  45.             //    i++;  
  46.             //}  
  47.             // }   
  48.             Table a = new Table();  
  49.             a.Attributes["border"] = "1";  
  50.             a.Attributes["width"] = "100%";  
  51.             for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)  
  52.             {  
  53.                 TableRow rw = new TableRow();  
  54.                 for (int columPos = 1; columPos <= nowTable.Rows[rowPos].Cells.Count; columPos++)  
  55.                 {  
  56.                     TableCell tc = new TableCell();  
  57.                     if (nowTable.Columns.Count > nowTable.Rows[rowPos].Cells.Count)  
  58.                     {  
  59.                         tc.Attributes["colspan"] = nowTable.Columns.Count.ToString();  
  60.                         tc.Attributes["align"] = "center";  
  61.                     }  
  62.                     string cell = nowTable.Cell(rowPos, columPos).Range.Text;  
  63.                     if (cell.Contains(""))  
  64.                     {  
  65.                         int i = 0;  
  66.                         foreach (Word.InlineShape shape in nowTable.Cell(rowPos, columPos).Range.InlineShapes)  
  67.                         {  
  68.                             //直接读取图片                             
  69.                             if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)  
  70.                             {  
  71.                                 System.Web.UI.WebControls.Image imgct = new System.Web.UI.WebControls.Image();  
  72.                                 //获取Word中的图片  
  73.                                 byte[] img = (byte[])shape.Range.EnhMetaFileBits;  
  74.                                 Bitmap bmp = new Bitmap(new System.IO.MemoryStream(img));  
  75.                                 string url = System.Web.HttpContext.Current.Server.MapPath("~/image/").ToString() + "pic" + i.ToString() + ".jpg ";//图片保存路径  
  76.                                 bmp.Save(url);  
  77.                                 imgct.ImageUrl = "~/image/" + "pic" + i.ToString() + ".jpg ";  
  78.                                 imgct.Attributes["width"] = "250px";  
  79.                                 tc.Controls.Add(imgct);  
  80.                                 tc.Wrap = true;  
  81.                             }  
  82.                             i++;  
  83.                         }  
  84.                     }  
  85.                     else  
  86.                     {  
  87.                         cell = cell.Remove(cell.Length - 2, 2);  
  88.                         if (cell == "") cell = "空";  
  89.                         tc.Text = cell;  
  90.                         //tableMessage += cell;  
  91.                         //tableMessage = tableMessage.Remove(tableMessage.Length - 2, 2);//remove \r\a  
  92.                         //tableMessage += "\t";  
  93.                     }  
  94.                     rw.Cells.Add(tc);  
  95.                 }  
  96.                 //tableMessage += "\n";  
  97.                 a.Rows.Add(rw);  
  98.             }  
  99.             //Label1.Text = tableMessage;  
  100.             Page.Controls.Add(a);  
  101.         }  
  102.     }  

 

 

通过标签Bookmark来读取数据

Bookmark bk;

bk.Range.Cells[1];//该值得到书签所在的单元格

 

[html]  view plain copy
 
    1. public bool FruitWordToSQL()  
    2.    {  
    3.        object oReadOnly = true;  
    4.        object oMissing = System.Reflection.Missing.Value;  
    5.        Word._Application oWord;  
    6.        Word._Document oDoc;  
    7.        oWord = new Word.Application();  
    8.        //oWord.Visible = false;//只是为了方便观察  
    9.        object fileName = System.Web.HttpContext.Current.Server.MapPath("~/Resultstemplate.doc").ToString();  
    10.        oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing);  
    11.        System.Collections.IEnumerator enu = oWord.ActiveDocument.Bookmarks.GetEnumerator();  
    12.        Dictionary<string, stringmap = new Dictionary<string, string>();//数据项(项名,项值)  
    13.        while (enu.MoveNext())  
    14.        {  
    15.            Word.Bookmark bk = (Word.Bookmark)enu.Current;  
    16.            string text = bk.Name.ToString();  
    17.            string value=bk.Range.Cells[1].Range.Text;//书签所在单元格的内容  
    18.            value = value.Remove(value.Length - 2);//去除单元格后的标记(/r/n)  
    19.            map.Add(text,value);  
    20.        }  
    21.        //对word的操作结束,关闭对word的控制  
    22.        oDoc.Close(ref oMissing, ref oMissing, ref oMissing);  
    23.        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);  
    24.        //检验数据的格式及正确性  
    25.        KeyValuePair<bool,stringcheck=CheckData(map);  
    26.        if (check.Key == false)  
    27.        {  
    28.            //check.Value中所含的值为数据检验不符合标准的第一个数据的值  
    29.            return false;  
    30.        }  
    31.        //map中的数据插入到数据库的Fruit表中  
    32.        bool Inserted=InsertData(map);  
    33.        if (Inserted == false)//插入失败  
    34.        {   
    35.            //插入失败后的操作  
    36.            return false;  
    37.        }  
    38.        return true;  
    39.    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值