简体   繁体   中英

Creating and Saving an Excel File

I have the following code that creates a new Excel file in my C# code behind. When I attempt to save the file I would like the user to select the location of the save.

In Method #1, I can save the file my using the workbook SaveCopyAs without prompting the user for a location. This saves one file to the C:\\Temp directory.

Method #2 will save the file in my Users\\Documents folder, then prompt the user to select the location and save a second copy. How can I eliminate the first copy from saving in the Users\\Documents folder?

Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;

try
{
    //Start Excel and get Application object.
    oXL = new Excel.Application();
    oXL.Visible = false;

    //Get a new workbook.
    oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
    oSheet = (Excel._Worksheet)oWB.ActiveSheet;

    // *****
    oSheet.Cells[2, 6] = "Ship To:";
    oSheet.get_Range("F2", "F2").Font.Bold = true;

    oSheet.Cells[2, 7] = sShipToName;
    oSheet.Cells[3, 7] = sAddress;
    oSheet.Cells[4, 7] = sCityStateZip;
    oSheet.Cells[5, 7] = sContactName;
    oSheet.Cells[6, 7] = sContactPhone;

    oSheet.Cells[9, 1] = "Shipment No:";
    oSheet.get_Range("A9", "A9").Font.Bold = true;
    oSheet.Cells[9, 2] = sJobNumber;

    oSheet.Cells[9, 6] = "Courier:";
    oSheet.get_Range("F9", "F9").Font.Bold = true;
    oSheet.Cells[9, 7] = sCarrierName;

    oSheet.Cells[11, 1] = "Requested Delivery Date:";
    oSheet.get_Range("A11", "A11").Font.Bold = true;
    oSheet.Cells[11, 2] = sRequestDeliveryDate;

    oSheet.Cells[11, 6] = "Courier Acct No:";
    oSheet.get_Range("F11", "F11").Font.Bold = true;
    oSheet.Cells[11, 7] = sCarrierAcctNum;
    // *****

    Method #1
    //oWB.SaveCopyAs(@"C:\Temp\" + sJobNumber +".xls");

    Method #2
    oXL.SaveWorkspace(sJobNumber + ".xls");
}
catch (Exception theException)
{
    String errorMessage;
    errorMessage = "Error: ";
    errorMessage = String.Concat(errorMessage, theException.Message);
    errorMessage = String.Concat(errorMessage, " Line: ");
    errorMessage = String.Concat(errorMessage, theException.Source);
}

您可以使用 savefiledialog 并让用户选择他们的位置,然后您可以在调用 oWB.SaveCopyAs(userselectedlocation) 时使用该位置

Use a SaveFileDialog class to get the desire path from the user:

http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

Why aren't you using SaveFileDialog ? See How to: Save Files Using the SaveFileDialog Component

--EDIT--

If its asp.net app, then this discussion might help to produce a save file dialog.

You can try ZetExcel for excellent results. Just try it.

You can try this. I tried, and it worked for me

oWB.SaveAs(@"C:\Temp\" + "test" + ".xlsx");

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