简体   繁体   English

如何使用Server.MapPath for Excel文件

[英]How to use Server.MapPath for Excel File

Using Asp.Net MVC. 使用Asp.Net MVC。

I have one Excel file which has default styles, borders etc and it's name is MyExcel.xlsx. 我有一个具有默认样式,边框等的Excel文件,它的名称是MyExcel.xlsx。 It opens "C:\\Users\\myusername\\Documents" from my local. 它从本地打开“ C:\\ Users \\ myusername \\ Documents”。

Here is my C# code for it: 这是我的C#代码:

            Application excel = new Application();
            excel.Visible = true;
            Workbook wb = excel.Workbooks.Open("MyExcel.xlsx");
            Worksheet sh = wb.Sheets[1];

I uploaded my file to my Server 2012 like C:\\MyExcel.xlsx. 我将文件像C:\\ MyExcel.xlsx一样上传到了Server 2012。 So i want to open server side MyExcel.xlsx file. 所以我想打开服务器端MyExcel.xlsx文件。

Server.MapPath("C:\MyExcel.xlsx");

But i get "Unexpected M" error. 但是我收到“意外的M”错误。 How can i fix this code? 我该如何解决此代码?

>         string myServerPath = Server.MapPath("C:\MyExcel.xlsx");
>         Application excel = new Application();
>         excel.Visible = true;
>         Workbook wb = excel.Workbooks.Open(myServerPath);
>         Worksheet sh = wb.Sheets[1];

尝试这样放置:

Server.MapPath(@"C:\MyExcel.xlsx");
Server.MapPath("C:\\MyExcel.xlsx");

要么

Server.MapPath(@"C:\MyExcel.xlsx");

Here is the short code to demonstrate how to use Server.MapPath. 这是演示如何使用Server.MapPath的简短代码。

Before starting, please make sure that you have a folder ready in your Solution Explorer to save your images/files. 开始之前,请确保在解决方案资源管理器中已准备好一个文件夹来保存图像/文件。

string img=""
string filePath = Server.MapPath("~/UploadFile/") + Path.GetFileName(FileUpload2.PostedFile.FileName);
if (!File.Exists(filePath))
{
  FileUpload2.SaveAs(img);
}

If you run this code, you will find that your "UploadFile" folder in your project will have the actual file uploaded. 如果运行此代码,您将发现项目中的“ UploadFile”文件夹将上传实际文件。

Hope this helps. 希望这可以帮助。

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

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