日期:2014-05-17  浏览次数:20623 次

.NET怎么读取WORD文档(包括HTML源代码)然后保存到数据库中
.NET怎么读取WORD文档(包括HTML源代码)然后保存到数据库中
网上搜索过来的都是把WORD文档上传后直接转为HTML文件的,我想的结果是想把WORD文档保留原有样式的代码一起保存到数据库中
PS:效率要高一点
谢谢各位达人

------解决方案--------------------
C# code
 try
            {
                int totalFile = 0;
                if (this.textBox1.Text.Trim() == "")
                {
                    MessageBox.Show("请输入HTML文件路径!");
                }
                else if (comboBox1.Text == "请选择")
                {
                    MessageBox.Show("请选择文件类型!");
                }
                else
                {
                    string dirPath = this.textBox1.Text.Trim();
                    if (!dirPath.Substring(dirPath.Length - 1).Contains("\\"))
                    {
                        dirPath = dirPath + "\\";
                    }
                    StreamWriter sw = null; ;
                    DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
                    FileInfo[] files = dirInfo.GetFiles();
                    foreach (FileInfo fileinfo in files)
                    {
                        if (fileinfo.Extension.Equals(comboBox1.Text))//遍历所有htm文件
                        {
                            totalFile = totalFile + 1;
                            WebRequest myWebRequest = WebRequest.Create(dirPath + fileinfo.Name);
                            WebResponse myWebResponse = myWebRequest.GetResponse();
                            Stream myStream = myWebResponse.GetResponseStream();
                            Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
                            StreamReader myStreamReader = new StreamReader(myStream, encode);
                            string strhtml = myStreamReader.ReadToEnd();
                            myWebResponse.Close();
                            string stroutput = strhtml;
                            //替换
                            stroutput = stroutput.Replace(textBox2.Text.Trim(), textBox4.Text.Trim());
                            string fileName = fileinfo.ToString();
                            //创建文件夹
                            if (!Directory.Exists(dirPath + "/NewFile"))
                                Directory.CreateDirectory(dirPath + "/NewFile");
                            //生成静态文件  
                            try
                            {
                                sw = new StreamWriter(dirPath + "NewFile/" + fileName, false, Encoding.GetEncoding("gb2312"));
                                sw.Write(stroutput);
                                sw.Flush();
                            }
                            catch (Exception ex)
                            {
                                sw.Close();
                                throw ex;
                            }
                            finally
                            {
                                sw.Close();
                            }
                        }

                        label5.Text = textBox1.Text + "\\NewFile";
                        label7.Text = "共替换" + totalFile + "处";
                    }
                    sw.Close();
                    MessageBox.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show("操作失败:" + ee.Message);
            }

------解决方案--------------------
把word文档当二进制流存进数据库,绝对原汁原味