0

我在 SQL Server 数据库中有一个模板 word 文件,我需要更改它的总和文本。我尝试先将文件下载到临时文件,然后将其加载到我的代码中。有没有一种方法可以将文件直接加载到我的代码中我使用这个(首先将文件保存到驱动器)之后我需要用数据库中的其他图像更改图像。

public Boolean save_agaza_file(string pattth)
    {
        Boolean result = false;
        Document wordDoc = new Document();
        Application wordApp = new Application();
        try
        {
            //OBJECT OF MISSING "NULL VALUE"

            Object oMissing = System.Reflection.Missing.Value;

            Object oTemplatePath = pattth;



            //wordDoc = new Document();

            wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
            DateTime day_start = DateTime.Now.AddDays(1);
            DateTime day_end = DateTime.Now.AddDays(2);
            foreach (Range rr in wordDoc.StoryRanges)
            {


            }
            foreach (Field myMergeField in wordDoc.Fields)
            {


                Range rngFieldCode = myMergeField.Code;

                String fieldText = rngFieldCode.Text;



                // ONLY GETTING THE MAILMERGE FIELDS

                if (fieldText.StartsWith(" MERGEFIELD"))
                {

                    // THE TEXT COMES IN THE FORMAT OF

                    // MERGEFIELD  MyFieldName  \\* MERGEFORMAT

                    // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

                    Int32 endMerge = fieldText.IndexOf("\\");

                    Int32 fieldNameLength = fieldText.Length - endMerge;

                    String fieldName = fieldText.Substring(11, endMerge - 11);

                    // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

                    fieldName = fieldName.Trim();

                    // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

                    // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

                    if (fieldName == "EmpName")
                    {

                        myMergeField.Select();

                        // wordApp.Selection.TypeText("محمد السيد زكى");
                        wordApp.Selection.TypeText(this.Reqested_emp.Name);

                    }
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.show(e.Message);
        }
    }

下载方法

string sql = @" select * from Agaza_template where template_id=" + agaza_id + ";";
                if (Form1.conn.State != ConnectionState.Open)
                {
                    Form1.conn.Open();
                    SqlCommand command = new SqlCommand(sql, Form1.conn);
                    SqlDataReader reader = command.ExecuteReader();
                    // reader.Read();

                    //if (reader.HasRows)

                    if (reader.Read())
                    {
                        byte[] b = (byte[])reader[2];

                        //  string s = Path.GetFileName(reader[1].ToString());
                        string s = reader[1].ToString();
                        string result = Path.GetTempPath();
                         file_temp_name = result + s;
                        FileStream fs = new FileStream(result + s, FileMode.Create);


                        fs.Write(b, 0, b.Length);
                        fs.Close();



                    }
                }


                Form1.conn.Close();

            }
4

0 回答 0