效果:

1.

.

.

数据库:

思路:

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

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

代码:

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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Data;
using System.Data.SqlClient; namespace InExcelOutExcel
{
public partial class UpWord : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
string SQLString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString();
protected void UploadButton_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection sqlcon = new SqlConnection(SQLString))
{
string FullName = FileUpload1.PostedFile.FileName;//获取word物理地址
FileInfo fi = new FileInfo(FullName);
string name = fi.Name;//获取word名称
string type = fi.Extension;//获取word类型
if (type == ".doc" || type == ".docx")
{
string SavePath = Server.MapPath("excel\\");//word保存到文件夹下
this.FileUpload1.PostedFile.SaveAs(SavePath + "\\" + name);//保存路径
string sql = "insert into image1(ImageName,ImageType,ImagePath) values('" + name + "','" + type + "','C:\\Users\\NewSpring\\Desktop\\Demo\\InExcelOutExcel\\InExcelOutExcel\\excel\\" + name + "')";
SqlCommand cmd = new SqlCommand(sql, sqlcon);
sqlcon.Open();
cmd.ExecuteNonQuery();
this.label1.Text = "上传成功";
this.tb1.Text = fi.Name;
}
else
{
this.label1.Text = "请选择正确的格式word";
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
} protected void lbtn_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection sqlcon = new SqlConnection(SQLString))
{
string sql = "select ImagePath from image1 where ImageName='" + tb1.Text.ToString() + "'";
SqlCommand cmd = new SqlCommand(sql, sqlcon);
sqlcon.Open();
cmd.CommandText = sql;
SqlDataReader sdr = cmd.ExecuteReader();
string ImagePath = "";
if (sdr.Read())
{
ImagePath = sdr["ImagePath"].ToString();
}
//下面是读取文档代码
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = true;
object fileName = ImagePath;
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// 创建新Word
//object oMissing = System.Reflection.Missing.Value;
//Word._Application oWord;
//Word._Document oDoc;
//oWord = new Word.Application();
//oWord.Visible = true;
//oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
// ref oMissing, ref oMissing);
// 导入模板
//object oMissing = System.Reflection.Missing.Value;
//Word._Application oWord;
//Word._Document oDoc;
//oWord = new Word.Application();
//oWord.Visible = true;
//object fileName = @"E:XXXCCXTest.doc";
//oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
// ref oMissing, ref oMissing);
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}

点滴积累【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. CentOS 6.9修改网卡名eth1为eth0

    vi /etc/udev/rules.d/70-persistent-net.rules 把NAME="eth1"修改为NAME="eth0",并将多余的记录删 ...

  2. 移动应用安全开发指南(Android)--Android组件和IPC

    概述 移动应用开发中,往往有跨进程通信的需求,方便地实现程序间的数据共享.Android提供了多种IPC通信的方式,给开发人员带来了便利,但如果选择或使用不当,就有可能发生各种各样的风险. 安全准则 ...

  3. C语言中的位域、字节序、比特序、大小端

    转:http://www.360doc.com/content/13/0624/10/496343_295125641.shtml 1.比特序 / 位序 /  bit numbering / bit  ...

  4. 《windows内核安全与驱动开发》ctrl2cap中的ObReferenceObjectByName疑问

    国内有关于windows内核驱动这块的书籍实在是甚少,不过好在<windows内核安全与驱动开发>这本书还算不错(内容方面),但是不得不说这本书在许多地方存在着一些细节上的问题.比如我今天 ...

  5. Linq 时间参数的一个坑

    背景:查询某个字段大于系统时间的数据 两种写法: 1.DataTime now=DateTime.Now; var result=dbContext.Table1.Created>now 2.  ...

  6. Lua-简洁、轻量、可扩展的脚本语言

    转自:http://rmingwang.com/The-Programming-Language-Lua.html 1. 一.Lua安装及常用库的安装 2. 1.1 Ubuntu $ sudo apt ...

  7. Kubernetes 1.7版本安装

      为什么搞完kubernetes 1.5又要装1.7, :( 是因为微服务架构istio的要求,而且直接用yum安装怎么都是1.5,所以只能通过下载包并且改配置文件的方式了,也好,花两天时间把整个过 ...

  8. C++中模块(Dll)对外暴露接口的方式

    总结下C++中模块(Dll)对外暴露接口的方式: (1)导出API函数的方式这种方式是Windows中调用DLL接口的最基本方式,GDI32.dll, User32.dll都是用这种方式对外暴露系统A ...

  9. reconstruct-original-digits-from-english(好)

    https://leetcode.com/problems/reconstruct-original-digits-from-english/ //https://discuss.leetcode.c ...

  10. nginx最大并发连接数的思考:worker_processes、worker_connections、worker_rlimit_nofile

    参考nginx官网:http://nginx.org/en/docs/ngx_core_module.html#worker_connections 从用户的角度,http 1.1协议下,由于浏览器默 ...