簡體   English   中英

使用 ASP.NET MVC C# 打開 Word/Excel 文件

[英]Open Word/Excel file using ASP.NET MVC C#

我有一個帶buttonView ,當單擊該button時,我使用Microsoft Word打開一個 word 文件:

JS

//CLICK EVENT
$('.openDocument').click(function () {
        $.ajax({
            url: "openFile",
            method: "GET",
            async: false,
            success: function (respuesta) {

            }
        });

    });

控制器

 [HttpGet]
   public void openFile()
   {
      Process.Start(@"C:\Users\user01\Downloads\Test_document.docx");
   }

我對此有兩個問題:

1)我在本地發布項目時,無法再次打開文件(只有在運行時),為什么發布時打不開? (任務管理器中僅顯示 Microsoft Word 圖標)

2) 已經發布了,我怎么做才能讓另一台電腦可以打開文件(在另一台電腦上顯示)而不是在服務器電腦上打開?

謝謝

試試這個問題 1

public static Process CreateProcess(string exePath, string arguments = null, bool isHidden = true, string workingDirectory = null)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo
            {
                FileName = exePath,
                Arguments = arguments,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                WorkingDirectory = workingDirectory ?? Path.GetDirectoryName(exePath)
            };

            if (isHidden)
                processStartInfo.CreateNoWindow = true;

            return new Process()
            {
                StartInfo = processStartInfo,
                EnableRaisingEvents = true
            };
        }

        public static void RunProcess(string exePath, string arguments = null, bool isHidden = true, string workingDirectory = null)
        {
            Process process = CreateProcess(exePath, arguments, isHidden, workingDirectory);
            if (process.Start())
            {
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
            }


        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM