2012-02-08 4 views
0

qc 열을 255 자마다 여러 문자열로 분할 한 다음 Excel에 삽입 할 때 함께 다시 연결하려고합니다. 나는 내가 생각할 수있는 모든 것을 시도했다. 나는 이것이 멍청하다는 것을 안다.하지만 나는 물을 것이라고 생각했다.하지만 어떻게 할 것인가? 내 삽입 코드입니다. 감사!255 자마다 긴 문자열 분할

string lFilename = "myExcel.xls"; 
string lDistributorFolder = Server.MapPath(".") + "\\Portals\\0\\Distributors\\" + _currentUser.UserID.ToString() + "\\"; 
string lTemplateFolder = System.Configuration.ConfigurationManager.AppSettings["CPCeCommerceTemplates"]; 
System.IO.Directory.CreateDirectory(lDistributorFolder); 

File.Copy(lTemplateFolder + lFilename, lDistributorFolder + lFilename, true); 
string lConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lDistributorFolder + "\\" + lFilename + ";Extended Properties=\"Excel 8.0;HDR=YES;\""; 
DbProviderFactory lFactory = DbProviderFactories.GetFactory("System.Data.OleDb"); 
int lSequence = 0; 
using (DbConnection lConnection = lFactory.CreateConnection()) 
{ 
    lConnection.ConnectionString = lConnectionString; 
    lConnection.Open(); 

    foreach (DataRowView rowView in dv) 
    { 
     DataRow row = rowView.Row; 

     lSequence++; 

     using (DbCommand lCommand = lConnection.CreateCommand()) 
     { 

      lCommand.CommandText = "INSERT INTO [Sheet1$]"; 
      lCommand.CommandText += "([First Name],[Last Name],[Title],[Company],[Address],[Address 2],[City],[State],[Zip],[Country],[Work phone],[Email],[Website],[Stamp Time],[Campaign],[Source],[Business Unit],[Market Segment],[Notes],[Other Source Detail],[Description],[Sales Employee firstname],[Sales Employee last name],[Reason],[Status],[Category],[Priority]) "; 
      lCommand.CommandText += "VALUES("; 
      lCommand.CommandText += "\"" + row["name"].ToString().Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["lastname"].ToString().Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["title"].ToString().Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["company"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["address"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["address2"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["city"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["state"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["zip"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["country"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["workphone"].ToString() + "\","; 
      lCommand.CommandText += "\"" + row["email"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["website"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["stamptime"].ToString() + "\","; 
      lCommand.CommandText += "\"" + row["campaign"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["source"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + string.Empty + "\","; 
      lCommand.CommandText += "\"" + row["market"].ToString() + "\","; 
      lCommand.CommandText += "\"" + row["qc"].ToString().Replace("\"", "\"\"").Replace(" ", " ") + "\","; 
      lCommand.CommandText += "\"" + row["othersourcedetail"].ToString() + "\","; 
      lCommand.CommandText += "\"" + string.Empty + "\","; 
      lCommand.CommandText += "\"" + string.Empty + "\","; 
      lCommand.CommandText += "\"" + string.Empty + "\","; 
      lCommand.CommandText += "\"" + "Lead" + "\","; 
      lCommand.CommandText += "\"" + "Open" + "\","; 
      lCommand.CommandText += "\"" + "Lead" + "\","; 
      lCommand.CommandText += "\"" + "High" + "\""; 
      lCommand.CommandText += ")"; 
      lCommand.ExecuteNonQuery(); 
     } 
    } 

    lConnection.Close(); 
} 
+2

... 그것처럼 그냥 관련이없는 제안하지만 당신은 할 수 있습니다 :

int len = 4; string str = "abcdefghijklmnopqrstuvwxyz"; string[] parts = str.Select((chr, index) => new { chr, index }) .GroupBy(entry => entry.index/len) .Select(group => new string(group.Select(entry => entry.chr).ToArray())) .ToArray(); 

이 약간 더 읽을 수 있습니다 StringBuilder를 사용하여 많은 문자열 연결을 수행하므로 CommandText를 작성해야합니다. http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx –

+0

@DJKRAZE 여기에 관련된 연산자 과부하가 있다고 생각지 않습니까? – McKay

+0

반환하고 삽입하려는 원래 데이터가 쉼표로 구분됩니까? ..? 그렇다면 쿼리에서 데이터를 읽지 않고 각 줄을 기준으로 분할을 수행합니다.이 항목을 사전에로드하고 실제로 가져 오거나 Excel에 삽입 할 수있는 쉼표로 구분 된 파일을 만들 수도 있습니다 .. – MethodMan

답변

0

이 LINQ는 예쁘지 않지만 사용자가 지정한 길이가 len 인 부품 번호 (str)를 분할합니다 (마지막 부품의 경우 더 적음).

string[] parts = 
    Enumerable.Range(0, (str.Length - 1)/len + 1) 
       .Select(i => str.Substring(i * len, Math.Min(len, str.Length - i * len))) 
       .ToArray(); 
2

String.Substring (0, 255). 루프와 위치를 지정하십시오. 문장을 나눕니다. 배열에 저장하고 문자열 위치가 문자열 끝에 도달 할 때까지 255 문자마다 반복하여 반복하십시오.

루프. 255자를 나눕니다. 문자열의 위치를 ​​기억하십시오.

0

이 LINQ가 꽤 아니지만, 여전히

int len = 255; 
for (int i = 0; i < big.Length; i += len) 
{ 
    string clip = new string(big.Skip(i).Take(len).ToArray()); 
}