效果:

1.

.

.

数据库:

思路:

上传:先获取word物理地址,然后根据文件的类型判断,然后再保存到相应的文件夹下,再把路径插入到数据库中。

读取:首先根据输入的文件名字在数据库中查找出来文件的路径,然后再根据路径把文件读取出来。

代码:

说明:需要导入COM库:Microsoft word 11.0 Object Library.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using System.Configuration;
  9. using System.Data;
  10. using System.Data.SqlClient;
  11.  
  12. namespace InExcelOutExcel
  13. {
  14. public partial class UpWord : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18.  
  19. }
  20. string SQLString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();
  21. protected void UploadButton_Click(object sender, EventArgs e)
  22. {
  23. try
  24. {
  25. using (SqlConnection sqlcon = new SqlConnection(SQLString))
  26. {
  27. string FullName = FileUpload1.PostedFile.FileName;//获取word物理地址
  28. FileInfo fi = new FileInfo(FullName);
  29. string name = fi.Name;//获取word名称
  30. string type = fi.Extension;//获取word类型
  31. if (type == ".doc" || type == ".docx")
  32. {
  33. string SavePath = Server.MapPath("excel\\");//word保存到文件夹下
  34. this.FileUpload1.PostedFile.SaveAs(SavePath + "\\" + name);//保存路径
  35. string sql = "insert into image1(ImageName,ImageType,ImagePath) values('" + name + "','" + type + "','C:\\Users\\NewSpring\\Desktop\\Demo\\InExcelOutExcel\\InExcelOutExcel\\excel\\" + name + "')";
  36. SqlCommand cmd = new SqlCommand(sql, sqlcon);
  37. sqlcon.Open();
  38. cmd.ExecuteNonQuery();
  39. this.label1.Text = "上传成功";
  40. this.tb1.Text = fi.Name;
  41. }
  42. else
  43. {
  44. this.label1.Text = "请选择正确的格式word";
  45. }
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. Response.Write(ex.Message);
  51. }
  52. }
  53.  
  54. protected void lbtn_Click(object sender, EventArgs e)
  55. {
  56. try
  57. {
  58. using (SqlConnection sqlcon = new SqlConnection(SQLString))
  59. {
  60. string sql = "select ImagePath from image1 where ImageName='" + tb1.Text.ToString() + "'";
  61. SqlCommand cmd = new SqlCommand(sql, sqlcon);
  62. sqlcon.Open();
  63. cmd.CommandText = sql;
  64. SqlDataReader sdr = cmd.ExecuteReader();
  65. string ImagePath = "";
  66. if (sdr.Read())
  67. {
  68. ImagePath = sdr["ImagePath"].ToString();
  69. }
  70. //下面是读取文档代码
  71. object oMissing = System.Reflection.Missing.Value;
  72. Microsoft.Office.Interop.Word._Application oWord;
  73. Microsoft.Office.Interop.Word._Document oDoc;
  74. oWord = new Microsoft.Office.Interop.Word.Application();
  75. oWord.Visible = true;
  76. object fileName = ImagePath;
  77. oDoc = oWord.Documents.Open(ref fileName,
  78. ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  79. ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
  80. ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  81. // 创建新Word
  82. //object oMissing = System.Reflection.Missing.Value;
  83. //Word._Application oWord;
  84. //Word._Document oDoc;
  85. //oWord = new Word.Application();
  86. //oWord.Visible = true;
  87. //oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
  88. // ref oMissing, ref oMissing);
  89. // 导入模板
  90. //object oMissing = System.Reflection.Missing.Value;
  91. //Word._Application oWord;
  92. //Word._Document oDoc;
  93. //oWord = new Word.Application();
  94. //oWord.Visible = true;
  95. //object fileName = @"E:XXXCCXTest.doc";
  96. //oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
  97. // ref oMissing, ref oMissing);
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. Response.Write(ex.Message);
  103. }
  104. }
  105. }
  106. }

点滴积累【C#】---C#实现上传word将路径保存到数据库,文件保存到服务器。并且按照名称读取服务器的word的更多相关文章

  1. 点滴积累【C#】---对上传文件的路径进行加密,以免将路径暴露在浏览器上,避免一些安全隐患!

    效果: 描述: 本事例是为解决在上传或下载文件时避免将路径暴露在外.在上传时将路径进行加密保存到DataTable或数据库中,在下载是再读取DataTable中加密数据进行解密下载. 代码: [前台代 ...

  2. [转]ExtJs入门之filefield:文件上传的配置+结合Ajax完美实现文件上传的asp.net示例

    原文地址:http://www.stepday.com/topic/?459 作文一个ExtJs的入门汉子,学习起来的确是比较费劲的事情,不过如今在这样一个网络资源如此丰富的时代,依然不是那么难了的. ...

  3. (配置)CKEditor+CKFinder+php上传配置,根据年月命名创建文件夹来存放

    CKEditor+CKFinder+php上传配置 新版本的CKEditor只提供了基本的文本编辑功能,上传模块由另一个组件CKFinder.这里主要记录CKFinder上传的一些参数配置,能够成功上 ...

  4. 从0在windows上一次性上传本地整个项目(包含所有文件/文件夹)到 Github

    1.注册并登陆Github. 2.登陆进去之后的页面,点击这个“库”,这表示你在Github上上的代码仓库,我这里已经创建过一个了,所以数量是1 3.在仓库选项卡中,点击“新建”按钮添加一个项目. 4 ...

  5. 简单的Django实现图片上传,并存储进MySQL数据库 案例——小白

    目标:通过网页上传一张图片到Django后台,后台接收并存储进数据库 真是不容易!!这个案例的代码网上太乱,不适合我,自己摸索着写,终于成功了,记录一下,仅供自己参考,有的解释可能不对,自己明白就好, ...

  6. Angular14 利用Angular2实现文件上传的前端、利用springBoot实现文件上传的后台、跨域问题

    一.angular2实现文件上传前端 Angular2使用ng2-file-upload上传文件,Angular2中有两个比较好用的上传文件的第三方库,一个是ng2-file-upload,一个是ng ...

  7. thinkphp项目上传到github,为什么缺少很多文件

    thinkphp项目上传到github,为什么缺少很多文件 问题: 把tp5项目push到码云(类似github)上,为什么没有thinkphp这个核心库? 然后我看了下码云和github上,官方的t ...

  8. PHP 多图上传,图片批量上传插件,webuploader.js,百度文件上传插件

    PHP  多图上传,图片批量上传插件,webuploader.js,百度文件上传插件(案例教程) WebUploader作用:http://fex.baidu.com/webuploader/gett ...

  9. springboot 静态资源访问,和文件上传 ,以及路径问题

    springboot 静态资源访问: 这是springboot 默认的静态资源访问路径  访问顺序依次从前到后(http://localhost:8080/bb.jpg) spring.resourc ...

  10. 【Azure 应用服务】App Service 在使用GIt本地部署,上传代码的路径为/home/site/repository,而不是站点的根目录/home/site/wwwroot。 这个是因为什么?

    问题描述 App Service 在使用GIt本地部署,上传代码的路径为/home/site/repository,而不是站点的根目录/home/site/wwwroot. 这个是因为什么? 并且通过 ...

随机推荐

  1. mysql-proxy使用中的问题

    Auth: Jin 1.session问题 Date: 20140328问题描述:基于openx 的广告系统,将数据从单点,迁移到mmm集群,前端无法访问报错信息如下:MDB2 Error: Arra ...

  2. html在线美化网站

    Html在线美化压缩/转js - 在线工具 地址:http://tool.lu/html/ 1.美化效果: 2.转化字符串拼接

  3. 查看Ubuntu服务器的版本信息

    第一种: uname -a 第二种: cat/etc/issue 第三种: lsb_release -a 这个查看的信息更加详细 使用命令:cat /proc/version 查看 proc目录下记录 ...

  4. Android 卡顿优化 3 布局优化 工具 Hierarchy Viewer

    欲善其事, 先利其器. 分析布局, 就不得不用到Hierarchy Viewer了. 本文工具使用皆以GithubApp的详情界面RepoDetailActivity为例说明. 为了不影响阅读体验, ...

  5. [Linux] ubuntu 软件安装必须看的网址

    http://wiki.ubuntu.org.cn/index.php?title=Qref/Apps&variant=zh-hans 这里介绍了unbuntu常用软件及其安装,免得你百度来百 ...

  6. Asp.net Core CORS(跨域资源共享)实验

    环境:Asp.Net Core 2 1.问题 最近项目在调用远程UI时遇到点麻,在调用远程CSS文件时无法加载其中的字体文件.远程CSS文件对字体的定义: @font-face { font-fami ...

  7. python matplotlib.pyplot学习记录

    matplotlib是python中很强大的绘图工具,在机器学习中经常用到 首先是导入 import matplotlib.pyplot as plt plt中有很多方法,记录下常用的方法 plt.p ...

  8. TCP为何采用三次握手来建立连接,若采用二次握手可以吗?

    首先简单介绍一下TCP三次握手     在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_ ...

  9. 解决Oracle在Linux下Listener起不来,error 111错误

    近来发生一个问题有点头疼,在linux上的Oracle数据库突然无法访问 主要报错如下: 基于本人的走歪路经验,分享一下我的解决思路: 首先,最直观的一点,监听器起不来,是不是数据库本身就没起来 se ...

  10. 跟着辛星一起用CSS美化商品列表

    说实话,近期对CSS的关注还是蛮多的,不为别的,仅仅是由于自己喜欢,感觉写CSS就像画家绘画一样,使用热情和激情去探索,没有了那份功利心,反而感觉是一种享受.特别有成就感,好啦,今天就分享一期自己用C ...