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();
}
... 그것처럼 그냥 관련이없는 제안하지만 당신은 할 수 있습니다 :
이 약간 더 읽을 수 있습니다 StringBuilder를 사용하여 많은 문자열 연결을 수행하므로 CommandText를 작성해야합니다. http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx –
@DJKRAZE 여기에 관련된 연산자 과부하가 있다고 생각지 않습니까? – McKay
반환하고 삽입하려는 원래 데이터가 쉼표로 구분됩니까? ..? 그렇다면 쿼리에서 데이터를 읽지 않고 각 줄을 기준으로 분할을 수행합니다.이 항목을 사전에로드하고 실제로 가져 오거나 Excel에 삽입 할 수있는 쉼표로 구분 된 파일을 만들 수도 있습니다 .. –
MethodMan