C#读取word表格到DataTable

  //word 中表数据格式定义
    public class DocTables
    {
        public string FileName { get; set; }
        public string TableName { get; set; }
        public System.Data.DataTable TableContent { get; set; }
    }


/根据word文件名找到内部所有的表并返回,patternText 为标示目录表特有标记,如 '目录主表数据定义' 或 '主表数据定义'等

        public static List<DocTables> GetTablesFromWordDoc(string filePath, string patternText = "主表数据定义")
        {
            List<DocTables> lstTableWithNames = new List<DocTables>();
            object oFileName = filePath;// @"D:\svn_repos\geps6.2\Docs\15.安全管理\07.安全管理活动\安全活动记录34.docx";
            object oReadOnly = true;
            object oMissing = System.Reflection.Missing.Value;


            Microsoft.Office.Interop.Word._Application oWord = null;
            Microsoft.Office.Interop.Word._Document oDoc = null;
            Object nothing = System.Reflection.Missing.Value;
            oWord = new Microsoft.Office.Interop.Word.Application();
            try
            {
                oWord.Visible = false;
                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);
              
                for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
                {
                    DocTables docTable = new DocTables();
                    Microsoft.Office.Interop.Word.Table nowTable = oDoc.Tables[tablePos];
                    //3 代表sentence,4 代表paragraph,5 代表line
                    var tableName = nowTable.Range.Previous(4, 1).Text;
                    //不满足匹配字符,跳过该表
                    if (!tableName.Contains(patternText)) continue;
                    docTable.FileName = filePath;
                    docTable.TableName = tableName.Trim(new char[] { '\n', '\r' });
                    //string tableMessage = string.Format("第{0}/{1}个表:\n", tablePos, oDoc.Tables.Count);
                    System.Data.DataTable dt = new System.Data.DataTable();
                    for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
                    {
                        DataRow drNew = dt.NewRow();
                        int columnIndex = 0;
                        foreach (Cell cellObj in nowTable.Rows[rowPos].Cells)
                        {
                            columnIndex++;
                            drNew[columnIndex] = cellObj.Range.Text.Remove(cellObj.Range.Text.Length - 2, 2);//remove \r\a


                        }
                        dt.Rows.Add(drNew);
                    }
                    lstTableWithNames.Add(docTable);
                }
                object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                oWord.Quit(ref saveOption, ref nothing, ref nothing);
                return lstTableWithNames;
            }
            catch
            {
                //
            }
            finally
            {
                if (oWord != null)
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
            }
            return null;


        }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值