怎样才能从word 和 excel 中读取文件内容。
怎么设置编码。
我读取的都是乱码。

解决方案 »

  1.   

    你怎么读的参考下
    http://www.cnblogs.com/emanlee/archive/2007/05/31/766520.aspx
    http://book.chinaz.com/net/asp.net1/dot32.htm
      

  2.   

    要用微软专门提供的类this.openFileDialog1.Filter="excel文件(*.xls)|*.xls";   
      this.openFileDialog1.Title="打开excel文件";   
      this.openFileDialog1.ShowDialog();   
      path1=this.openFileDialog1.FileName;   
      //设置空值   
      object   objMissing=System.Reflection.Missing.Value;   
                            //打开excel文件   
        my=new   Excel.ApplicationClass();   
                          
      my.Visible=true;   
        
      //打开工作簿   
      Excel.Workbook   mybook=my.Workbooks.Open(path1.Trim(),objMissing,objMissing,   objMissing,     
      objMissing,   objMissing,   objMissing,     
      objMissing,   objMissing,   objMissing,     
      objMissing,   objMissing,   objMissing);   
        
      Excel.Worksheet   mysheet=(Excel.Worksheet)mybook.Worksheets.get_Item(1);   
        
      //设置第10行为红色   
      mysheet.get_Range((Excel.Range)mysheet.Cells[10,1],(Excel.Range)mysheet.Cells[10,200]).Select();   
      mysheet.get_Range((Excel.Range)mysheet.Cells[10,1],(Excel.Range)mysheet.Cells[10,200]).Interior.ColorIndex=3;   
      

  3.   

    我现在想从word读,可是中文读出来都是乱码。
    读.txt的文件就没事。
      

  4.   

    看看编码方式,如果方法正确就是编码方式的问题了,看看你word的编码方式,改下,或许就不是乱码了
      

  5.   

    word 用什么样的编码方式呢。
      

  6.   

    你估计是用FileStream+StreamReader来读的吧
    这样是不能读取.doc文档的
    只能用2楼的那种读取EXCEL的方法(读取WORD也是类似的Word.Application)
      

  7.   

    在WEB里可以这样读word..我最近也在做这个..你试试..添加以下对word等的引用..读的时候最好进程里的word进程都关闭,要不会提示说什么WORD已被锁定..
     public  static string Doc2Text(string docFileName)
            {
                try
                {
                    //实例化COM
                    Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                    object fileobj = docFileName;
                    object nullobj = System.Reflection.Missing.Value;
                    //打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
                    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj,
                        ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj
                        );
                    //取得doc文件中的文本
                    string outText = doc.Content.Text;
                    //关闭文件
                    doc.Close(ref nullobj, ref nullobj, ref nullobj);
                    //关闭COM
                    wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
                    //返回
                    return outText;
                }
                catch
                {
                    return null;
                }
            }
      

  8.   

    需要引用这个microsoft.office.interop.word.dll