2012-12-10 2 views
0

내 Excel 통합 문서는 일부 위치에 저장됩니다. 기본적으로 버튼을 클릭하면 내보내기가 gridview에서 Excel로 사용됩니다. Excel에서 데이터 읽기 및 업데이트하기

모두 잘 작동

,

내 질문은 "내가 먼저 파일을 조회 할, 그리고 않는 경우, 이미 존재하고, 난 내 gridview에와있어 데이터로 업데이트 존재하는 새로운 데이터가있는 경우 . "

내 함수는 이전 데이터를 덮어 쓰는 많은 양의 새로운 데이터를 생성합니다.

감사

+0

과정은 – Boomer

+0

... 훨씬 더 비용이 많이 드는 파일을 열고 내부의 데이터를 확인하고, 당신이 무엇을 비교하기 때문에 이전 파일을 덮어 이상의 이유 난을 데이터입니다 업데이트 내가 수동으로 만든 엑셀 파일을 가지고 있기 때문에, 내가 필요로하는 것은 gridview에서 값을 가져 와서 엑셀 필드에 저장하는 것입니다 .... 그 이유는 ... – Kulkarni

답변

0

여기

string filePath = Request.PhysicalApplicationPath + "filename.xls"; 
FileInfo imageFile = new FileInfo(filePath); 
bool fileExists = imageFile.Exists; 
Label1.Text = "File exits?: " + fileExists.ToString(); 

을 다음과 독서에 대한 코드와 엑셀 여기

var myConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\Language_Batch1_OneClick.xls';Extended Properties=Excel 8.0;"); ; 
    var myCommand = new OleDbCommand(); 
    var upCommand = new OleDbCommand(); 
    int i = 0; 
    try 
    { 

     string sql = null; 
     myConnection.Open(); 
     myCommand.Connection = myConnection; 
     sql = "select ANSWER_CODE,Punjabi from [Batch_Lang_1$]"; 
     myCommand.CommandText = sql; 
     var dataReader = myCommand.ExecuteReader(); 

     while (dataReader.Read()) 
     { 
      var langText = Convert.ToBase64String(Encoding.UTF8.GetBytes(dataReader["Punjabi"].ToString())); 
      if (langText.Length >= 1000) 
      { 
       continue; 
      } 
      var ansCode = dataReader["ANSWER_CODE"].ToString(); 
      sql = "update [Batch_Lang_1$] set Punjabi= '" + langText + "' where ANSWER_CODE='" + ansCode + "'"; 
      upCommand.Connection = myConnection; 
      upCommand.CommandText = sql; 
      upCommand.ExecuteNonQuery(); 
      i++; 
     } 

파일 업데이트 할 수있는 링크와 같이 먼저 파일을 확인할 수 있습니다 여기에는 엑셀 파일을 읽고 업데이트하는 코드가 있습니다. 당신이 요청하는대로
C# Convert MS Excel column to utf-8 and then to base64