简体   繁体   English

使用 C# 在同一位置获取和保存特定的 Excel 文件

[英]Get and Save particular Excel file in same location using C#

I am using Asp.Net MVC application, Visual Studio 2013, SQL Server Data base我正在使用 Asp.Net MVC 应用程序、Visual Studio 2013、SQL Server 数据库

There is a particular location in my system's local drive ie, c:/Files/Exim_Files/ , where a lot of excel(.xlsx) files are sitting.我系统的本地驱动器中有一个特定位置,即 c:/Files/Exim_Files/ ,其中有很多 excel(.xlsx) 文件。 I want to get the particular file from that location and save it programmatically (without Save pop up) in same location with same/different name.我想从该位置获取特定文件并以相同/不同名称以编程方式将其保存(不弹出保存)在同一位置。 while saving the excel data should not be lost, file should be as it is, just I need to save/SaveAs it again.保存excel数据时不应该丢失,文件应该保持原样,只是我需要再次保存/另存为。

How can I achieve this requirement?我怎样才能达到这个要求?

Note that I am using Virtual Machine and inside Virtual Machine: Microsoft Office is not installed.请注意,我正在使用虚拟机并在虚拟机内部:未安装 Microsoft Office。 So the code will have to work without Microsoft Office installation in the machine.因此,代码必须在机器中没有安装 Microsoft Office 的情况下才能工作。 I can only use Microsoft Office in my host machine.我只能在我的主机上使用 Microsoft Office。

Edit编辑

In below code, I am using Aspose.Cells to save the Excel file from 1 location to another.在下面的代码中,我使用 Aspose.Cells 将 Excel 文件从一个位置保存到另一个位置。

I am getting the particular File from sharedLocation in array "l_strFileUploadPath" and then checking, if the file that I am getting from user exists in shared location, then I want to save/SaveAs that file into different location (defined in 'string str') along with entire data (say I want to import the data as well while Saving the Excel in different location).我从数组“l_strFileUploadPath”中的 sharedLocation 获取特定文件,然后检查我从用户获取的文件是否存在于共享位置,然后我想将该文件保存/另存为不同的位置(在“字符串 str”中定义) ) 以及整个数据(比如我想在将 Excel 保存在不同位置的同时导入数据)。

The issue I am facing is that, the file that is getting saved in C: drive, is not saving the data which is present inside the Excel.我面临的问题是,保存在 C: 驱动器中的文件没有保存 Excel 中存在的数据。 It seems it is creating a new excel file in c: drive with same name (x-TECHNICAL_DIT_BUDV01_RV124_R01_2015_Test.xlsx) having 2 sheets.似乎是在 c: 驱动器中创建一个新的 excel 文件,同名 (x-TECHNICAL_DIT_BUDV01_RV124_R01_2015_Test.xlsx) 有 2 张纸。 1 is 'sheet 1' and another is 'Evaluation Warning' sheet. 1 是“表 1”,另一个是“评估警告”表。

How can I remove the 'Evaluation Warning' sheet and what is the method of saving the exact file (along with data) from shared drive to c: drive, as per my code.根据我的代码,如何删除“评估警告”表以及将确切文件(以及数据)从共享驱动器保存到 c: 驱动器的方法是什么。

This is the first time using Aspose.Cells to get and Save/SaveAs the file from 1 location to another.这是第一次使用 Aspose.Cells 将文件从一个位置获取并保存/另存为另一个位置。

protected void getFileAndSave()
{
    string[] l_strFileUploadPath = Directory.GetFiles("//181.184.11.435/share//Temp/New folder");

    foreach (var filename in l_strFileUploadPath)
    {
            string fileName = Path.GetFileName(filename);
            string p_filename = "x-TECHNICAL_DIT_Test.xlsx"; //this is the file I am getting from user

            if (fileName == p_filename)
            {
                //-- Using Aspose.Cells
                    Workbook wb = new Workbook();

                    Worksheet worksheet = wb.Worksheets["Sheet1"];
                    worksheet.Name = "Technical Data";

                    //Save workbook with export cell as true
                    OoxmlSaveOptions opts = new OoxmlSaveOptions();
                    opts.ExportCellName = true;
                    wb.Save(str + file, opts);

            }
    }
}

It seems for me that you just want to copy the file.对我来说,您似乎只想复制文件。

Take a look at File.Copy()看看File.Copy()

File.Copy(@"c:\excel.xsl", @"c:\other\excel.xsl");

EDIT for comment:编辑评论:

I think it will be hard to change app.xml metadata without just manually looking what changes are made there during save/saveAs and then trying to understand how to copy this behavior and change it programmatically inside that file.我认为很难更改 app.xml 元数据,而不仅仅是手动查看在保存/另存为期间所做的更改,然后尝试了解如何复制此行为并在该文件中以编程方式更改它。

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

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