将数据库中的文档合并为一个文档 [英] Merge documents from the database into one document

查看:99
本文介绍了将数据库中的文档合并为一个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,它采用附件文件(图像和Word文档)格式,并且保存在数据库中的每个文件都有一个优先级编号,

我需要一种方法来获取这些文件从Priority订购的数据库中,然后将它们合并到一个文件中,请帮忙做什么?



我尝试了什么:



I have a web application , that takes Attachments File (images and Word Documents) formats ,and each file that saved in the database has a priority number,
I need a way that takes these Files from the database ordered by Priority , and then merge them into one Files ,Please any help to do that ?

What I have tried:

public void load()
   {
       connection.Open();
       string strQuery2 = "select Data,Priority from Attcehments where CourseNum='" + course_number + "' and Semester='" + semester + "' ORDER BY Priority ASC;";

       SqlCommand cmd = new SqlCommand(strQuery2, connection);

       cmd.ExecuteNonQuery();

       DataTable dt = GetData(cmd);

       if (dt != null)
       {

           download(dt);

       }
   }




private DataTable GetData(SqlCommand cmd)
   {

       DataTable dt = new DataTable();

       String strConnString = System.Configuration.ConfigurationManager

       .ConnectionStrings["connect"].ConnectionString;

       SqlConnection con = new SqlConnection(strConnString);

       SqlDataAdapter sda = new SqlDataAdapter();

       cmd.CommandType = CommandType.Text;

       cmd.Connection = con;

       try
       {

           con.Open();

           sda.SelectCommand = cmd;

           sda.Fill(dt);

           return dt;

       }

       catch
       {

           return null;

       }

       finally
       {

           con.Close();

           sda.Dispose();

           con.Dispose();

       }

   }//data table

   private void download(DataTable dt)
   {
      string name = dt.Rows[0]["priority"].ToString();
       Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
       // System.Text.UnicodeEncoding.UTF8.GetBytes(dt.Rows[0]["Data"].ToString());
       using (FileStream fs = new FileStream(@"C:\\Users\\Rawan Mansour\\Desktop\\"+name+".docx", FileMode.Create,FileAccess.ReadWrite))
       {
           fs.Write(bytes, 0, bytes.Length);
           fs.Close();
       }

       MessageBox.Show("File saved successfully ");




   }

推荐答案

As已经在评论中指出,你将不得不编写代码来支持每种文件类型。对于Office文件类型,您可以使用OpenXML SDK,并且可以使用该SDK将图像放入Word文档中。



或者您可以使用pdf工具,如iTextSharp,并创建一个pdf。如果您的需求支持pdf,那可能会更容易。
As has been pointed out in the comments, you'll have to write code to support each of the file types. For Office file types you can use the OpenXML SDK and images can be dropped into Word documents by using that SDK.

Or you can use a pdf tool, like iTextSharp, and create a pdf. That might be easier, if your requirements support pdf.


如前所述,OpenXML是正确的方法。但是,它很难学习并且容易出错,特别是对于新手而言。所以我的建议是使用第三方库。在您的场景中,最终文档将完全在后端准备,然后写入Response对象,然后将其下载到客户端的浏览器。



既然你需要混合Word文档和图像我建议您使用图像的占位符创建word模板。然后,您将通过合并模板和图像在运行时为每个图像创建Word文档。这些带有图像的Word文档将与其他常规Word文档合并为最终的Word文档。



这里是一个片段,展示了如何将多个Word文档连接成一个。
As it has been pointed earlier OpenXML is the right approach. However, it is quite difficult to learn and it's error prone, especially for a novice. So my recommendation would be to use 3rd party library. In your scenario final document would be prepared entirely on the back end and then written to the Response object, which would download it to the browser on the client.

Since you need to mix Word documents and images I recommend that you create word template with a placeholder for the image. You would then create a Word document at runtime for each image by merging the template and the image. These Word documents with images would then be merged with other regular Word documents into a final Word document.

Here is the snippet that shows how to concatenate several Word documents into one.


这篇关于将数据库中的文档合并为一个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆