如何打开嵌入式资源word文档?

时间:2015-10-16 06:56:29

标签: c# ms-word embedded-resource

我的项目中有一个嵌入式word模板文档,我将其添加为资源(Resources.resx - >添加资源 - >添加现有文件),现在我想打开它像这样

Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document document = application.Documents.Open(Properties.Resources.MyDoc);

但不幸的是,Microsoft.Office.Interop.Word.Application不能处理字节数组,我无法将MyDoc设置为它。

1 个答案:

答案 0 :(得分:2)

Word只能打开文件系统中存在的文件,它不能完全从内存中运行。

做这样的事情:

File.Delete( fileName );

然后,当您检测到Word已关闭时,请删除该文件:

Byte[]

可能是一个想法(出于性能原因)将Word文档作为嵌入式资源而不是resx文件中的Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream resourceStream = thisExe.GetManifestResourceStream("MyDoc.docx"); // copy the stream to a new FileStream, then open Word as-before 数组嵌入,如下所示:

    btnCalculate = new JButton("Calculate");
    btnCalculate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            inGui = new Scanner (System.in);
            double firstRun = getAverageOfRun(1);
            double secondRun = getAverageOfRun(2);
            double best;

            if (firstRun > secondRun) {
                best = firstRun;
            } else {
                best = secondRun;
            }
            textFieldRun1Score.setText(Double.toString(best));
            }
        private double getAverageOfRun (int runNumber) {
            double total, avg;
            int num1, num2, num3, num4, num5, num6;
            List<Integer> list = new ArrayList<Integer>();

            num1 = Integer.parseInt(textFieldRun1Score1.getText());

            System.out.print(""+runNumber +": ");
            list.add(textFieldRun1Score1.getText());
            list.add(textFieldRun1Score2.getText());
            list.add(textFieldRun1Score3.getText());
            list.add(textFieldRun1Score4.getText());
            list.add(textFieldRun1Score5.getText());
            list.add(textFieldRun1Score6.getText());

            Collections.sort(list);

            total = list.get(1) + list.get(2) + list.get(3) + list.get(4);

            avg = total / 4;

            textFieldBestScore.setText(Double.toString(avg));
            return avg;

        }