C#을

2015-02-06 3 views
-1

내가 텍스트 파일의 형식으로 DataGridView에 데이터를 수출하고, 을 사용하여 만든 나는 아래의 코드 여기C#을

 string dirLocationString = @"C:\Users\palanar\Desktop\result.txt"; 
     StreamWriter sW = new StreamWriter(dirLocationString); 
     string lines = ""; 
     lines = "DateTime" + "\t" + "TestStatus" + "\t" + "BatPackSN" + "\t" + "CellSN" 
       + "\t" + "BlockSN" + "\t" + "UI_ID" + "\t" + "UI_ParentID" + "\t" + "OperationNumber" 
       + "\t" + "OperationName" + "\t" + "EquipmentNumber" + "\t" + "EquipmentName" + "\t" + "WorkOrder" 
       + "\t" + "Assembly" + "\t" + "ProductName" + "\t" + "HandlingDuration" + "\t" + "OperationDuration" 
       + "\t" + "RepairID" + "\t" + "DefectID" + "\t" + "UitemLevelCode" + "\t" + "UIEventLevelCode"; 
     sW.WriteLine(lines); 
     for (int row = 0; row < dataGridView2.Rows.Count - 1; row++) 
     { 
      string lines1 = ""; 
      for (int col = 0; col <= 19; col++) 
      { 
       lines1 += (string.IsNullOrEmpty(lines1) ? " " : "\t") + dataGridView2.Rows[row].Cells[col].Value.ToString(); 
      } 

      sW.WriteLine(lines1); 
     } 

데이터가 완벽하게 내 보낸 텍스트 파일의 형식으로 저장됩니다 시도 , 문제는 여기에 기본 위치를 할당했습니다. 대신 저장 대화 상자를 열어 위치를 요청해야합니다.

+0

창 또는 웹을 사용한다 ??? –

+0

@Ganesh_Devlekar, Windows 데스크톱 응용 프로그램. –

+1

사이드 노트 :'IDisposable'로 작업하면'using'으로 감싸고, 사용하는 경우에는 (StreamWriter sW = new StreamWriter ...) {...}' –

답변

4

당신은 saveFileDialogthis is a tutorial

예를 찾고 있습니다 :

SaveFileDialog sfd = new SaveFileDialog(); 

sfd.Filter = "Text file(*.txt)|*.txt"; 
sfd.FilterIndex = 1; 

if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } 
string dirLocationString = sfd.FileName; 
+0

감사합니다 :-) 내 문제를 해결했습니다 :-) –

+0

당신은 매우 환영합니다. – chouaib

1

당신은 SaveFileDialog

 SaveFileDialog sfd = new SaveFileDialog(); 

     if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      //your selected file location 
      string dirLocationString = sfd.FileName; 
     }