每255个字符拆分长字符串

时间:2012-02-08 21:18:43

标签: c# oledb

我希望将每255个字符的qc列拆分成多个字符串,然后在将它们插入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();
}

3 个答案:

答案 0 :(得分:2)

String.Substring(0,255)。做一个循环和位置。分开这句话。将它保存到数组并保持每255个字符循环,直到字符串位置到达字符串结尾。

循环。每隔255分裂一次。记住字符串中的位置。

答案 1 :(得分:0)

这个LINQ并不漂亮,但它会将您指定的任何字符串str拆分为多个部分,每个部分的长度为len(在最后一部分的情况下,更小)。

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();

这可能稍微清晰一点:

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 :(得分:0)

这个linq不是那么漂亮,但我还是喜欢它......

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