首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么读取WORD文档内容 并存入数据库

2013-07-01 
如何读取WORD文档内容 并存入数据库图为word内容。请大神给个读取方法。本人初学。。啥都不懂。。。自己尝试的老

如何读取WORD文档内容 并存入数据库
怎么读取WORD文档内容 并存入数据库
图为word内容。
请大神给个读取方法。本人初学。。啥都不懂。。。
自己尝试的老是报错 获取不到内容,
oDoc.Content.Tables[1];
oDoc.Tables.Count..;等语句怎么写都报错。。
怎么读取WORD文档内容 并存入数据库

c#?WORD
[解决办法]
你要保存word,不用读取内容,就把word以数据流的方式存入二进制字段就行了,google “c# 数据库 图片”,道理和保存图片一样

[解决办法]
我好早之前做一个将数据库中的数据导入word,也就是生成一个带表格的word文件
大概代码:

     /// <summary>
        /// 新建表格
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="oral"></param>
        private static void NewTable(Document doc,Oral_subject oral)
        {
            if(doc != null)
            {
                object defaultV = System.Reflection.Missing.Value;
                object what = WdUnits.wdLine;
                object count = 1;

                if (doc.Tables.Count == 0)
                {
                    object bName="Items";
                    doc.Bookmarks.get_Item(ref bName).Select();
                }
                else
                {
                    doc.Tables[doc.Tables.Count].Select();
                    //what = WdUnits.wdCharacter;
                    //doc.Application.Selection.MoveRight(ref what, ref count, ref defaultV);
                    //what = WdUnits.wdLine;
                    doc.Application.Selection.MoveDown(ref what, ref count, ref defaultV);


                }
              
                doc.Application.Selection.TypeParagraph();
                doc.Application.Selection.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
                doc.Application.Selection.ParagraphFormat.LineSpacing = 17;
                doc.Application.Selection.Font.Size = 14;
                doc.Application.Selection.Font.Bold = 1;
                doc.Application.Selection.TypeText(items[doc.Tables.Count] + " " + OralLibraryManager.GetPartitionByPID(oral.Partition_ID).Partition_name);


                doc.Application.Selection.TypeParagraph();
                doc.Application.Selection.Font.Bold = 0;
                doc.Application.Selection.Font.Size = 13;
                doc.Application.Selection.TypeText("说明:");

                doc.Application.Selection.TypeParagraph();
                doc.Application.Selection.TypeText("  1、○项类别试题为必考题目;");
                doc.Application.Selection.TypeParagraph();
                doc.Application.Selection.TypeText("  2、△项类别试题为选考题目;");
                doc.Application.Selection.TypeParagraph();
                doc.Application.Selection.TypeText("  3、考官记录只需在试题类别○或△上打√或×来记录考生的答题情况。");
                doc.Application.Selection.TypeParagraph();

                Table tb = doc.Tables.Add(doc.Application.Selection.Range, 1, 3, ref defaultV, ref defaultV);  //插入表格
                tb.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
                tb.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
                
                tb.Rows[1].Cells[1].Range.Text = "考试题目";  //表头
                tb.Rows[1].Cells[2].Range.Text = "类别";
                tb.Rows[1].Cells[1].Range.Font.Size = 14;


                tb.Rows[1].Cells[2].Range.Font.Size = 14;
                tb.Rows[1].Cells[1].Range.Font.Bold = 0;
                tb.Rows[1].Cells[2].Range.Font.Bold = 0;
                tb.Rows[1].Cells[1].Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
                tb.Rows[1].Cells[1].Range.ParagraphFormat.LineSpacing = 17;
                tb.Rows[1].Cells[2].Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly;
                tb.Rows[1].Cells[2].Range.ParagraphFormat.LineSpacing = 17;

                tb.Rows[1].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                tb.Rows[1].Cells[2].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

                tb.Columns[1].Width = 340;
                tb.Columns[2].Width = 40;
                tb.Columns[3].Width = 40;

            }
        }


      /// <summary>
        /// 插入总结
        /// </summary>
        /// <param name="ht"></param>
        /// <param name="doc"></param>
        /// <param name="list"></param>
        private static void InsertStatistic(Hashtable ht, Document doc,List<Oral_subject> list)
        {
            if (doc != null)
            {
                object oMissing = System.Reflection.Missing.Value;
                object bName = "Statistic";
                doc.Bookmarks.get_Item(ref bName).Select();
                doc.Application.Selection.TypeParagraph();

                Table tb = doc.Tables.Add(doc.Application.Selection.Range, 1, 3, ref oMissing, ref oMissing);  //插入“总结”表
                tb.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;


                tb.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;

                tb.Rows[1].Cells[1].Range.Text = "考试项目";  //表头
                tb.Rows[1].Cells[2].Range.Text = "○数目";
                tb.Rows[1].Cells[3].Range.Text = "△数目";

                tb.Rows[1].Cells[1].Range.Font.Size = 14;
                tb.Rows[1].Cells[2].Range.Font.Size = 14;
                tb.Rows[1].Cells[3].Range.Font.Size = 14;

                tb.Rows[1].Cells[1].Range.Font.Bold = 1;
                tb.Rows[1].Cells[2].Range.Font.Bold = 1;
                tb.Rows[1].Cells[3].Range.Font.Bold = 1;

                tb.Rows[1].Cells[1].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                tb.Rows[1].Cells[2].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                tb.Rows[1].Cells[3].Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

                tb.Columns[1].Width = 300;
                tb.Columns[2].Width = 60;
                tb.Columns[3].Width = 60;

                //

                if (ht != null)  //所有题目表
                {
                    object s = 1;
                    Row row;
                    foreach (DictionaryEntry de in ht)
                    {
                        int pos = int.Parse(de.Key.ToString());
                        int b = 0;
                        int x = 0;



                        GetTypeCount(ref b, ref x, doc.Tables[pos]);

                        //Row row = tb.Rows.Add(ref oMissing);
                        
                        tb.Rows[1].Select();
                        doc.Application.Selection.InsertRowsBelow(ref s);
                        row = tb.Rows[2];

                        row.Cells[1].Range.Text = items[pos - 1] + " " + de.Value.ToString();
                        row.Cells[2].Range.Text = b.ToString();
                        row.Cells[3].Range.Text = x.ToString();

                        row.Cells[1].Range.Font.Bold = 0;
                        row.Cells[2].Range.Font.Bold = 0;
                        row.Cells[3].Range.Font.Bold = 0;

                        row.Cells[1].Range.Font.Size = 13;
                        row.Cells[2].Range.Font.Size = 13;
                        row.Cells[3].Range.Font.Size = 13;

                        row.Cells[1].Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpace1pt5;
                        row.Cells[2].Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpace1pt5;
                        row.Cells[3].Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpace1pt5;


                    }


                    tb.Rows[tb.Rows.Count].Select();
                    doc.Application.Selection.InsertRowsBelow(ref s);


                    row = tb.Rows[tb.Rows.Count];
                    int B = 0;
                    int X = 0;

                    foreach (Oral_subject oral in list)
                    {
                        if (oral.Osubject_type == 0)
                        {
                            B++;
                        }
                        else
                        {
                            X++;
                        }
                    }

                    row.Cells[1].Range.Text = "合计数目";
                    row.Cells[2].Range.Text = B.ToString();
                    row.Cells[3].Range.Text = X.ToString();

                    row.Cells[1].Range.Font.Bold = 1;
                    row.Cells[2].Range.Font.Bold = 0;
                    row.Cells[3].Range.Font.Bold = 0;
                    row.Cells[1].Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpace1pt5;
                    row.Cells[2].Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpace1pt5;
                    row.Cells[3].Range.ParagraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpace1pt5;


                }

            }


        }


我就截取了其中两个设计到表格的方法  你凑合着看看吧  其余的太多 就不贴了  具体啥意思 我也搞忘记了
[解决办法]
段落、行、表格、书签、单签选中文本、页、页眉页脚 等等都有对应的类型
[解决办法]
妹妹裤裆夹黄莲--苦逼!
[解决办法]
http://www.189works.com/article-58086-1.html 这个比较简单的

热点排行