每 255 个字符分割长字符串
我想将 qc
列每 255 个字符拆分为多个字符串,然后在将它们插入 Excel 时将它们连接在一起。我已经尝试了我能想到的一切,我知道这很愚蠢,但我想我会问,但我该怎么做呢?这是我的插入代码。谢谢!
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();
}
I want to split the qc
column every 255 characters into multiple strings and then concat them back together when I Insert them into Excel. I've tried about everything I can think of, I know this is dumb, but I thought I would ask, but how would I go about doing this? This is my insert code. Thanks!
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串. 子字符串(0, 255)。做一个循环并定位。拆分句子。将其保存到数组中并每 255 个字符循环一次,直到字符串位置到达字符串末尾。
环形。每 255 分割一次。记住字符串中的位置。
String.Substring(0, 255). Do a loop and position. split the sentence. save it to an array and keep looping every 255 characters until the string position reaches the string end.
Loop. split every 255. remember the position in the string.
这个 LINQ 并不漂亮,但它会将您指定的任何字符串
str
分割成多个部分,每个部分的长度为len
(并且,在最后一部分,较少)。这可能稍微更容易理解:
This LINQ is not pretty, but it will split any string,
str
, that you specify into a number of parts, each of lengthlen
(and, in the case of the last part, less).This may be slightly more legible:
这个 linq 不是那么漂亮,但我仍然喜欢它......
This linq is not that pretty, but I still like it...