简体   繁体   中英

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

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. 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.

How can I achieve this requirement?

Note that I am using Virtual Machine and inside Virtual Machine: Microsoft Office is not installed. So the code will have to work without Microsoft Office installation in the machine. I can only use Microsoft Office in my host machine.

Edit

In below code, I am using Aspose.Cells to save the Excel file from 1 location to another.

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).

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. 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. 1 is 'sheet 1' and another is 'Evaluation Warning' sheet.

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.

This is the first time using Aspose.Cells to get and Save/SaveAs the file from 1 location to another.

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(@"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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