博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取 WORD 表格内容
阅读量:5119 次
发布时间:2019-06-13

本文共 1633 字,大约阅读时间需要 5 分钟。

 

public Dictionary<string, string> GetWordTableContent(object fullname, int tableIndex)

        {
            Microsoft.Office.Interop.Word.Application wordApp = new
              Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = new
                Microsoft.Office.Interop.Word.Document();
            object missing = System.Reflection.Missing.Value;

            Dictionary<string, string> tableItems = new Dictionary<string, string>();

            try

            {
                wordApp.Visible = false;

                doc = wordApp.Documents.Open(ref fullname, 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 (doc.Content.Tables.Count > 0)

                {
                    if (tableIndex <= 0 || tableIndex > doc.Content.Tables.Count)
                        throw new Exception("Incorrect tableIndex.");

                    Microsoft.Office.Interop.Word.Table table = doc.Content.Tables[tableIndex];

                    for (int i = 1; i <= table.Rows.Count; i++)

                    {
                        for (int c = 1; c <= table.Columns.Count; c++)
                        {
                            try
                            {
                                tableItems.Add(string.Format("{0},{1}", i, c), table.Cell(i, c).Range.Text ?? "");
                            }
                            catch (COMException comEx)
                            {
                                if (comEx.Message == "The requested member of the collection does not exist.")
                                    continue;
                                throw;
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                if (doc != null)
                    doc.Close(ref missing, ref missing, ref missing);
                if (wordApp != null)
                    wordApp.Quit(ref missing, ref missing, ref missing);
                throw;
            }
            finally
            {
                if (doc != null)
                    doc.Close(ref missing, ref missing, ref missing);
                if (wordApp != null)
                    wordApp.Quit(ref missing, ref missing, ref missing);
            }
            return tableItems;
        }

 

转载于:https://www.cnblogs.com/yipeng-yu/archive/2012/04/28/2475130.html

你可能感兴趣的文章
中国烧鹅系列:利用烧鹅自动执行SD卡上的自定义程序(含视频)
查看>>
Solaris11修改主机名
查看>>
latex for wordpress(一)
查看>>
如何在maven工程中加载oracle驱动
查看>>
Flask 系列之 SQLAlchemy
查看>>
aboutMe
查看>>
【Debug】IAR在线调试时报错,Warning: Stack pointer is setup to incorrect alignmentStack,芯片使用STM32F103ZET6...
查看>>
一句话说清分布式锁,进程锁,线程锁
查看>>
python常用函数
查看>>
FastDFS使用
查看>>
服务器解析请求的基本原理
查看>>
[HDU3683 Gomoku]
查看>>
【工具相关】iOS-Reveal的使用
查看>>
数据库3
查看>>
存储分类
查看>>
下一代操作系统与软件
查看>>
【iOS越狱开发】如何将应用打包成.ipa文件
查看>>
[NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)
查看>>
Yii2 Lesson - 03 Forms in Yii
查看>>
Python IO模型
查看>>