点滴积累【C#】---C#实现上传word将路径保存到数据库,文件保存到服务器。并且按照名称读取服务器的word
2013-12-23 14:42
996 查看
效果:
1.
2.
3.
数据库:
思路:
上传:先获取word物理地址,然后根据文件的类型判断,然后再保存到相应的文件夹下,再把路径插入到数据库中。
读取:首先根据输入的文件名字在数据库中查找出来文件的路径,然后再根据路径把文件读取出来。
代码:
说明:需要导入COM库:Microsoft word 11.0 Object Library.
1.
2.
3.
数据库:
思路:
上传:先获取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文件。
- 点滴积累【C#】---C#实现上传照片到物理路径,并且将地址保存到数据库,
- C#实现上传照片到物理路径,并且将地址保存到数据库的小例子
- C#实现上传照片到物理路径,并且将地址保存到数据库的小例子
- 点滴积累【C#】---对上传文件的路径进行加密,以免将路径暴露在浏览器上,避免一些安全隐患!
- PageOffice+C#+SqlServer+Aspose.Word+Aspose.Cells实现上传word模板,通过标签保存填写的数据到数据库中
- java文件上传!针对各种文件上传demo实列 文件上传以及数据库保存文件名以及路径名称:
- c#客户端图片上传到服务器端文件并且把路径保存到SQL数据库
- 网站的文件的上传,并将相对路径保存到数据库的代码实现。
- [转]C#将文件保存到数据库中或者从数据库中读取文件
- C#将文件保存到数据库中或者从数据库中读取文件
- (转)ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器
- c# 如何实现保存文件到数据库
- 定时备份SQL SERVER的数据库并且把备份文件复制到另外一台服务器保存
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 通过Socket方式上传文件后服务器迟迟不返回文件保存路径
- struts2+ckeditor+ckfinder实现文件的上传、从数据库获取数据、修改并提交到数据库中保存
- php上传图片到指定位置路径保存到数据库的具体实现
- [置顶] js前端预览上传图片到七牛服务器,保存图片路径到数据库的方法
- php上传图片到指定位置路径保存到数据库的具体实现