简体   繁体   English

从asp.net/C#网页调用vb脚本

[英]Calling a vb script from a asp.net / C# webpage

I have written an ASP.NET web page with C# behind that runs an existing vb script. 我已经编写了一个ASP.NET网页,后面带有C#,可运行现有的vb脚本。

The idea is that the user uploads an Excel spreadsheet (.xls) using the web page, the C# does a few basic checks on the file (file type, file name etc) then saves the .xls to a network location. 这个想法是,用户使用网页上传Excel电子表格(.xls),C#对文件(文件类型,文件名等)进行一些基本检查,然后将.xls保存到网络位置。

The C# then passes the network path of the .xls to the vb script, which gets the required information from the .xls to create a .csv file. 然后,C#将.xls的网络路径传递到vb脚本,该脚本从.xls获取所需的信息以创建.csv文件。

Finally the .csv is passed into a stored procedure and uploaded to a database table. 最后,.csv传递到存储过程中并上载到数据库表。

The problem is that all this runs perfectly when I run the webpage locally on my machine. 问题是,当我在计算机上本地运行网页时,所有这些都可以完美运行。 However when I upload the page to the webserver it does not seem to execute the vb script; 但是,当我将页面上传到Web服务器时,它似乎没有执行vb脚本。 instead it just sits there waiting for the script to exit. 相反,它只是坐在那里等待脚本退出。

Some quick info: 一些快速信息:

  • Excel is installed on the web server Excel安装在Web服务器上
  • The website is set to execute scripts and executables 该网站设置为执行脚本和可执行文件
  • The script is currently set to 'run as' my personal domain login (this has to change) which has admin on the web server 该脚本当前设置为“运行为”我的个人域登录(必须更改),该登录在Web服务器上具有管理员权限
  • If I run the script on the webserver using the cmd prompt it works 如果我使用cmd提示符在Web服务器上运行脚本,则该脚本有效

I'd really appreciate any ideas on what might be going wrong... seriously, I'm pulling my hair out over this one and will consider any idea, no matter how crazy... but, and it's a big one, despite the fact that that there are many other ways of achieving the same result, I'm afraid that for a number of reasons this is what I have to work with :) 我真的很感激关于可能出什么问题的任何想法...认真地说,我正在竭尽全力,不管有多么疯狂,我都会考虑任何想法...但是,尽管有很大的想法,事实上,还有许多其他方法可以达到相同的结果,由于种种原因,我担心这就是我必须使用的方法:)

Edit 编辑

Here is how I call the script 这是我所谓的脚本

                try
                {
                    System.Security.SecureString password = new System.Security.SecureString();

                    string uspw = "mypassword";
                    foreach (char c in uspw)
                    {
                        password.AppendChar(c);
                    }

                    Process scriptProc = new Process();
                    scriptProc.StartInfo.FileName = @"cscript";
                    scriptProc.StartInfo.Arguments = scriptPath + " //Nologo " + uploadPath + xlsFileName;
                    scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
                    scriptProc.StartInfo.UserName = "myusername";
                    scriptProc.StartInfo.Password = password;
                    scriptProc.StartInfo.UseShellExecute = false;
                    scriptProc.Start();
                    scriptProc.WaitForExit();
                    scriptProc.Close();

                }
                catch...

All of the file paths are relative. 所有文件路径都是相对的。

The code seems to fail when the script is called. 调用脚本时,代码似乎失败。 You can clearly see the page waiting for the script to finish. 您可以清楚地看到等待脚本完成的页面。 However if you watch the task manager on the web server neither cscript or excel start to run. 但是,如果您在Web服务器上观看任务管理器,则cscript或excel都不会开始运行。

I've also stuck a message box right at the start of the script which does not get displayed 我还在脚本开始处粘贴了一个消息框,该消息框未显示

Edit 2 It turns out that cscript is running, I just needed to tick the 'All Users' check box in the task manager... I'm still none the wiser though! 编辑2事实证明cscript正在运行,我只需要在任务管理器中选中“所有用户”复选框即可。不过,我还是一个聪明人!

Thanks so much in advance 提前非常感谢

在没有看到代码的情况下,这是一个盲目的猜测-我建议您检查如何指定vb脚本的路径-确保您未使用绝对路径,并且该文件相对于C#页面位于同一位置机器上的服务器。

Sounds like you are using automation to control the Excel application itself? 听起来您正在使用自动化来控制Excel应用程序本身?

Some quick info: 一些快速信息:

  • Excel is installed on the web server Excel安装在Web服务器上

That is generally a bad idea, because the Excel application is not an application that is intended to be automated by a server. 这通常是一个坏主意,因为Excel应用程序不是旨在由服务器自动执行的应用程序。 Thing might hang because the application is waiting for user input in a dialog somewhere. 事情可能会挂起,因为应用程序正在等待用户在对话框中某处的输入。 And it's not scalable for handling operations from multiple users simultaneously. 而且,它不能同时处理多个用户的操作。

If the final goal is to extract the data from the excel file and put it in an sql server, I would rather suggest that you use the Jet OLEDB provider to retrieve the data from the excel file, either from your web application, and letting that feed the data into sql server, or let the sql server do it directly. 如果最终目标是从excel文件中提取数据并将其放置在sql服务器中,则我建议您使用Jet OLEDB提供程序从excel文件中检索数据,无论是从Web应用程序中进行,还是让它将数据输入sql服务器,或让sql服务器直接执行。 If there is a lot of data in the excel file, the latter might be the best choice 如果excel文件中有很多数据,则后者可能是最佳选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM