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

    效果:

    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 }
  • 相关阅读:
    UOJ179
    Codeforces603E
    洛谷P4219
    Vijos1655
    JS对象 四舍五入round() round() 方法可把一个数字四舍五入为最接近的整数。 语法: Math.round(x)
    JS对象 向下取整floor() floor() 方法可对一个数进行向下取整。 语法: Math.floor(x)
    JS对象 向上取整ceil() ceil() 方法可对一个数进行向上取整。 语法: Math.ceil(x) 注意:它返回的是大于或等于x,并且与x最接近的整数。
    JS对象 神奇的Math对象,提供对数据的数学计算。注意:Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法。这是它与Date,String对象的区别
    JS对象 提取指定数目的字符substr() substr() 方法从字符串中提取从 startPos位置开始的指定数目的字符串。
    JS对象 substring() 方法用于提取字符串中介于两个指定下标之间的字符。
  • 原文地址:https://www.cnblogs.com/xinchun/p/3487413.html
Copyright © 2011-2022 走看看