您的位置:首页 > 数据库

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

2013-12-23 14:42 996 查看
效果:

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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐