word 文档的保存数据及读取,类似图片,数据库设置图片格式(image)

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace labzmzl
{
    public partial class zmzl_add : Form
    {
        public zmzl_add()
        {
            InitializeComponent();
        }

        private void bt_upload_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "word文档(*.doc;*.docx)|*.doc;*.docx";
            if (opf.ShowDialog() == DialogResult.OK)
            {
                if (opf.FileName != "")
                {
                    this.textBox2.Text = opf.FileName;
                }
            }
        }
        private string zlid;
        private void bt_save_Click(object sender, EventArgs e)
        {
            //zlid自动生成
            string sqlzlid = "select max(zlid) from lab_zmzl";
            SqlCommand cmd = new SqlCommand(sqlzlid, alluse.Class1.conn1);
            string id = Convert.ToString(cmd.ExecuteScalar());
            if (id == "")
            {
                zlid = "1";
            }
            else
            {
                int zl = Int32.Parse(id) + 1;
                zlid = zl.ToString();
            }
            //
            string zlsm = this.textBox1.Text.Trim().ToString();
            string wordstr = this.textBox2.Text;
            if (wordstr.Trim() == "") { wordstr = Application.StartupPath + "//modal//word.docx"; }

            string strsql = "insert into lab_zmzl(zlid,zlsm,zlwd) values('" + zlid + "','" + zlsm + "',@word)";
            SqlCommand mycmd = new SqlCommand(strsql, alluse.Class1.conn1);
            FileStream fs = File.OpenRead(wordstr);
            byte[] byte1 = new byte[fs.Length];
            fs.Read(byte1, 0, (int)fs.Length);
            fs.Close();
            mycmd.Parameters.Add("@word", SqlDbType.Image).Value = byte1;
            int n = mycmd.ExecuteNonQuery();
            if (n > 0)
            {
                Class1.tt = new string[] { zlid, zlsm };
                Class1.hasdo = "yes";
                this.Close();
            }
            else
            {
                MessageBox.Show("添加失败!");
            }
        }

        private void bt_clear_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            this.textBox2.Text = "";
        }
    }
}

 

 

以上是添加,修改类似。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace labzmzl
{
    public partial class zmzl_change : Form
    {
        private string zlzm_zlid;
        public zmzl_change(String zlid)
        {
            InitializeComponent();
            zlzm_zlid = zlid;
        }

       
        private void zmzl_change_Load(object sender, EventArgs e)
        {
            string sql = "select zlsm from lab_zmzl where zlid='" + zlzm_zlid + "'";
            SqlDataAdapter sda = new SqlDataAdapter(sql, alluse.Class1.conn1);
            DataSet ds = new DataSet();
            sda.Fill(ds, "lab_zmzl");
            this.textBox1.Text = ds.Tables["lab_zmzl"].Rows[0]["zlsm"].ToString();
        }
       
        private void bt_upload_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "word文档(*.doc;*.docx)|*.doc;*.docx";
            if (opf.ShowDialog() == DialogResult.OK)
            {
                if (opf.FileName != "")
                {
                    this.textBox2.Text = opf.FileName;
                }
            }
        }

        private void bt_clear_Click(object sender, EventArgs e)
        {
            this.textBox2.Text = "";
        }

        private void bt_change_Click(object sender, EventArgs e)
        {
            string zlsm = this.textBox1.Text.Trim().ToString();
            string strsql = "update lab_zmzl set zlsm='" + zlsm + "'";// where zlid='" + zlzm_zlid + "'
            string wordstr = this.textBox2.Text;
            if (wordstr.Trim() != "") { strsql += ",zlwd=@word"; }
            strsql += " where zlid='" + zlzm_zlid + "'";
            SqlCommand mycmd = new SqlCommand(strsql, alluse.Class1.conn1);
            if (wordstr.Trim() != "")
            {
                FileStream fs1 = File.OpenRead(wordstr);
                byte[] bytes1 = new byte[fs1.Length];
                fs1.Read(bytes1, 0, (int)fs1.Length);
                fs1.Close();
                mycmd.Parameters.Add("@word", SqlDbType.Image).Value = bytes1;
            }

            int n = mycmd.ExecuteNonQuery();
            if (n > 0)
            {
                Class1.tt = new string[] { zlzm_zlid, zlsm };
                Class1.hasdo = "yes";
                this.Close();
            }
            else
            {
                MessageBox.Show("修改失败!");
            }
        }

    }
}
以上是修改代码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java中的POI库读取Word文档并将其存储数据库中,可以按照以下步骤进行操作: 1. 添加POI库的依赖 在Maven项目中,可以在pom.xml文件中添加如下依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 读取Word文档 可以使用POI库中的XWPFDocument类读取Word文档。下面是一个简单的示例代码: ```java File file = new File("path/to/word/document.docx"); FileInputStream fis = new FileInputStream(file); XWPFDocument document = new XWPFDocument(fis); ``` 3. 解析Word文档并获取需要存储数据 可以使用POI库提供的API来解析Word文档中的内容,如获取段落、表格、图片等。根据需要存储数据类型,可以选择不同的API进行解析。下面是一个示例代码,用于获取Word文档中的所有段落: ```java List<String> paragraphs = new ArrayList<>(); List<XWPFParagraph> paragraphList = document.getParagraphs(); for (XWPFParagraph paragraph : paragraphList) { String text = paragraph.getText(); paragraphs.add(text); } ``` 4. 将数据存储数据库中 根据需要存储数据类型,可以选择不同的数据库操作API进行存储。以下是一个示例代码,用于将获取到的段落存储到MySQL数据库中: ```java String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "root"; String password = "mypassword"; Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO paragraphs (text) VALUES (?)"); for (String paragraph : paragraphs) { pstmt.setString(1, paragraph); pstmt.executeUpdate(); } ``` 注意:以上代码只是一个示例,实际应用中需要根据具体需求进行修改。同时,为了保证程序的健壮性,需要添加异常处理代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值