效果:
1.
2.
3.
數據庫:
思路:
上傳:先獲取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 }