2010-04-29 9 views
2

다음 C# 코드에서 새 Excel 파일을 만드는 코드가 있습니다. 파일을 저장하려고 할 때 사용자가 저장 위치를 ​​선택하기를 원합니다.Excel 파일 만들기 및 저장

방법 # 1에서 사용자에게 위치를 묻지 않고 SaveCopyAs 통합 문서를 사용하여 파일을 저장할 수 있습니다. 이렇게하면 하나의 파일이 C : \ Temp 디렉토리에 저장됩니다.

방법 2는 파일을 내 Users \ Documents 폴더에 저장 한 다음 사용자에게 위치를 선택하고 두 번째 복사본을 저장하라는 메시지를 표시합니다. 첫 번째 복사본을 Users \ Documents 폴더에 저장하지 않으려면 어떻게합니까? 유 oWB.SaveCopyAs (userselectedlocation)를 호출 할 때

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); 
} 
+1

Kris, http://stackoverflow.com/faq의 FAQ를 읽어보십시오. 태그 작동 방식이 아닙니다. –

+0

이 귀중한 정보에 감사드립니다. – Kris

답변

3

당신은

3

를 사용하여 A의 SaveFileDialog 클래스가에서 욕망의 경로를 얻기 위해 다음 U 해당 위치를 사용할 수 있습니다, 자신의 위치를 ​​선택의 SaveFileDialog를 사용하여 사용자를 가질 수 있습니다 사용자 :

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

+0

이것은 Windows 양식에서만 작동합니까? 웹 응용 프로그램에서 대화 상자로 저장하려고합니다. – Kris

+0

@Kris : 귀하의 게시물에 언급 된 곳은 어디에도 없습니다. 난뿐만 아니라 귀하의 웹 애플 리케이션 태그 (예 : asp.net)를 추가해야한다고 생각합니다. –

+0

@KMan : 죄송합니다. 조각을 깜빡했습니다. :) – Kris

관련 문제