• 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 plaincopy
     
    1. using Microsoft.Office.Core;  
    2. using Microsoft.Office.Interop;  
    3. using Microsoft.Office.Interop.Word;  

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

    [html] view plaincopy
     
    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 plaincopy
     
    1. oDoc.Close(ref missing, ref missing, ref missing);  
    2. oWord.Quit(ref missing, ref missing, ref missing);  

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

    [html] view plaincopy
     
    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}个表: ", 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  a  
    92.                         //tableMessage += " ";  
    93.                     }  
    94.                     rw.Cells.Add(tc);  
    95.                 }  
    96.                 //tableMessage += " ";  
    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 plaincopy
     
      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.    }  
  • 相关阅读:
    iOS开发工具
    Runtime 自动化归档
    iOS事件拦截及应用
    NoSuchFieldError
    微信开放平台创建android应用时怎么获取应用签名
    高德地图添加marker及反地理编码获取POI
    快速创建导航栏
    Android Studio 工具栏添加常用按钮
    undefined is not an object (evaluating 'RNFetchBlob.DocumentDir')
    React-Native集成到已有项目中的总结
  • 原文地址:https://www.cnblogs.com/Alex80/p/5076012.html
Copyright © 2020-2023  润新知