以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  如何读取word、excel文件的页数呢?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=175749)

--  作者:cnsjroom
--  发布时间:2022/3/17 16:04:00
--  如何读取word、excel文件的页数呢?

如何读取word、excel文件的页数呢?

如题  麻烦老师们指导下  谢谢!


--  作者:有点蓝
--  发布时间:2022/3/17 16:06:00
--  
word:http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&Id=138473

execl:http://foxtable.com/bbs/dispbbs.asp?boardid=2&Id=144600

--  作者:cnsjroom
--  发布时间:2022/3/17 16:35:00
--  回复:(有点蓝)word:http://www.foxtable.com/bbs/...

老师帮忙看看 

我想在读取word页码的同时 读取wod的内容  下述代码提示错误:

 


图片点击可在新窗口打开查看此主题相关图片如下:11.png
图片点击可在新窗口打开查看
代码如下:

        If Values(1).Contains("doc") Or Values(1).Contains("docx") Then
            Dim app As New MSWord.Application
            try
                Dim fileName = fl
                app.Documents.Open(fileName)
                Dim count = app.Documents(fileName).Characters.Count
                Dim rng As MSWord.Range = app.Documents(fileName).Range(Start:=0, End:=count)
                r("文件内容")=rng.Text
                wjnr.text=rng.Text
                \'msgbox(rng.Text)
                rng.Select()   \'全选
                rng.Copy()   \'拷贝
                \'msgbox(y)
            catch ex As exception
                msgbox(ex.message)
            finally
                app.Quit
            End try
            \'msgbox(ClipBoard.GetText)
           
            Dim app1 As New MSWord.Application
            try
                Dim fileName = fl
                Dim doc = app.Documents.Open(fileName)
                Dim y = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, Nothing)
                \' msgbox(y)
                r("文件页数") = y
                wjys.text=y
            catch ex As exception
                msgbox(ex.message)
            finally
                app1.Quit
            End try
           
        Else
            r("文件内容")=wjnr.text
            r("文件页数") = wjys.text
        End If


--  作者:有点蓝
--  发布时间:2022/3/17 16:43:00
--  
    Dim Doc = app.Documents.Open(fileName)
    Dim count = doc.Characters.Count
    Dim rng As MSWord.Range = Doc.Range(Start:=0, End:=count)
r("文件内容")=rng.Text
r("文件页数") = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, Nothing)

--  作者:cnsjroom
--  发布时间:2022/3/17 17:03:00
--  回复:(有点蓝)    Dim Doc = app.Docume...

老师   在成功读取word内容和页数之后  我要将当前文件同步上传到二进制列中

 

运行提示   未将对象引用设置到对象的实例【如果是pdf的时候  就可以无误地上传并能够进行二进制文件的下载与预览】

麻烦老师看看   我这样的操作是不是顺序有误!还是代码需要哪里写不对了!

当前代码如下:

    Dim dlg As New OpenFileDialog
    dlg.Filter = "图形|*.bmp;*.jpg;*.gif;*.png|文件|*.txt;*.doc;*.docx;*.xls;*.xlsx;*.pdf;*.rar;*.zip;*.wps"
    If dlg.ShowDialog =DialogResult.OK Then
        Dim fl As String = dlg.FileName
        Dim bb As String =FileSys.GetName(fl)
        Dim Values() As String
        Values = bb.split(".")
        Dim ss1 As String
        Dim ss2 As Integer
        If Values(1).Contains("pdf") Then
            Dim reader As New iTextSharp.text.pdf.PdfReader(fl)
            Dim n As Integer = reader.NumberOfPages
            Dim str As String = ""
            For i As Integer = 1 To n
                Dim strategy As object = New iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy()
                Dim currentText As String = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(Reader, i, strategy)
                currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.[Default], Encoding.UTF8, Encoding.[Default].GetBytes(currentText)))
                str &= currentText
               
            Next
            output.show(str)
           
            reader.Close()
            ss1=str
            wjnr.text=str
        Else  If Values(1).Contains("doc") Or Values(1).Contains("docx") Then
            Dim app1 As New MSWord.Application
            try
                Dim fileName = fl
                Dim Doc = app1.Documents.Open(fileName)
                Dim count = doc.Characters.Count
                Dim rng As MSWord.Range = Doc.Range(Start:=0, End:=count)
                ss1=rng.Text
                ss2 = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, Nothing)
                wjnr.text=ss1
                wjys.text=ss2
            catch ex As exception
                msgbox(ex.message)
            finally
                app1.Quit
            End try
        End If
       
        Dim r As Row = Tables(e.Form.Name & "_table1").AddNew()
        r("文件类型")=Values(1)
        r("文件名") = wjm.text
        r("文件类别") = wjlb.text
        r("文件归类") = wjgl.text
        r("uuid")=vars("uuid")
        r("文件时间") = wjsj.text
        r("文件页数") = wjys.text
        r("单位名称")=dwmc.text
        r("人员名称")=rymc.text
        r("身份证号")=sfzh.text
        r("所属室组")=sssz.text
        r("文件内容")=ss1
        r("文件页数") = ss2
        r.Save \'必须先保存
        r.DataRow.SQLInsertFile("附件",fl) \'插入文件
        r.DataRow.SQLSetValue("CRC值", CRCCheckFile(fl)) \'保存crc检验值
        MessageBox.Show("附件上传完毕!","温馨提示")
        \'Dim ylbt As WinForm.Button = e.Form.Controls("预览")
        \'ylbt.PerformClick
    End If


--  作者:有点蓝
--  发布时间:2022/3/17 17:08:00
--  
提示看是那一句代码出错
--  作者:cnsjroom
--  发布时间:2022/3/17 17:20:00
--  回复:(有点蓝)提示看是那一句代码出错

经弹出与测试pdf  excel文件均可正常上传

 

word测试时候   会提示这个  有没有办法取消呢?默认都不保存!


图片点击可在新窗口打开查看此主题相关图片如下:11.png
图片点击可在新窗口打开查看

然后代码会一直运行到末尾   1.2.3都弹出  甚至提示上传完毕!


图片点击可在新窗口打开查看此主题相关图片如下:22.png
图片点击可在新窗口打开查看

当前代码如下:

Dim dlg As New OpenFileDialog
dlg.Filter = "图形|*.bmp;*.jpg;*.gif;*.png|文件|*.txt;*.doc;*.docx;*.xls;*.xlsx;*.pdf;*.rar;*.zip;*.wps"
If dlg.ShowDialog =DialogResult.OK Then
    Dim fl As String = dlg.FileName
    Dim bb As String =FileSys.GetName(fl)
    Dim Values() As String
    Values = bb.split(".")
    Dim ss1 As String
    Dim ss2 As Integer
    If Values(1).Contains("pdf") Then
        Dim reader As New iTextSharp.text.pdf.PdfReader(fl)
        Dim n As Integer = reader.NumberOfPages
        Dim str As String = ""
        For i As Integer = 1 To n
            Dim strategy As object = New iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy()
            Dim currentText As String = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(Reader, i, strategy)
            currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.[Default], Encoding.UTF8, Encoding.[Default].GetBytes(currentText)))
            str &= currentText
           
        Next
        output.show(str)
       
        reader.Close()
        ss1=str
       
    Else  If Values(1).Contains("doc") Or Values(1).Contains("docx") Then
        Dim app1 As New MSWord.Application
        try
            Dim fileName = fl
            Dim Doc = app1.Documents.Open(fileName)
            Dim count = doc.Characters.Count
            Dim rng As MSWord.Range = Doc.Range(Start:=0, End:=count)
            ss1=rng.Text
            ss2 = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, Nothing)
            output.show(ss2)
            app1.Visible = False
            
        catch ex As exception
            msgbox(ex.message)
        finally
            app1.Quit
        End try
    End If
   
    Dim r As Row = Tables("附件").AddNew()
    r("文件类型")=Values(1)
   
    r("文件内容")=ss1
    r("文件页数") = ss2
    r.Save \'必须先保存
MessageBox.Show(1)
    r.DataRow.SQLInsertFile("附件",dlg.FileName) \'插入文件    这个地方提示  为 未将对象引用设置到对象的实例
MessageBox.Show(2)
    r.DataRow.SQLSetValue("CRC值", CRCCheckFile(dlg.FileName)) \'保存crc检验值
MessageBox.Show(3)
    MessageBox.Show("附件上传完毕!","温馨提示")

    \'Dim ylbt As WinForm.Button = e.Form.Controls("预览")
    \'ylbt.PerformClick
End If


--  作者:有点蓝
--  发布时间:2022/3/17 17:23:00
--  
这个地方选其它文件可以正常保存?

到命令窗口直接测试下面代码有没有问题?

 Dim r As Row = Tables("附件").AddNew()
    r.Save \'必须先保存
MessageBox.Show(1)
    r.DataRow.SQLInsertFile("附件","手工填入文件路径") \'插入文件    这个地方提示  为 未将对象引用设置到对象的实例
MessageBox.Show(2)
[此贴子已经被作者于2022/3/17 17:23:49编辑过]

--  作者:cnsjroom
--  发布时间:2022/3/17 17:29:00
--  回复:(有点蓝)这个地方选其它文件可以正常保存?到...

直接写文件位置  就没有问题

也不弹出是否保存文件的提示


--  作者:有点蓝
--  发布时间:2022/3/17 17:37:00
--  
Else  If Values(1).Contains("doc") Or Values(1).Contains("docx") Then
        Dim app1 As New MSWord.Application
        try
            Dim fileName = fl
            Dim Doc = app1.Documents.Open(fileName)
            Dim count = doc.Characters.Count
            Dim rng As MSWord.Range = Doc.Range(Start:=0, End:=count)
            ss1=rng.Text
            ss2 = doc.ComputeStatistics(Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages, Nothing)
            output.show(ss2)
            app1.Visible = False
            doc.saved = True
        catch ex As exception
            msgbox(ex.message)
        finally
            app1.Quit
        End try
    End If