每 255 个字符分割长字符串

发布于 2025-01-03 23:05:31 字数 4378 浏览 1 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千仐 2025-01-10 23:05:31

字符串. 子字符串(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.

绾颜 2025-01-10 23:05:31

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

This LINQ is not pretty, but it will split any string, str, that you specify into a number of parts, each of length len (and, in the case of the last part, less).

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

This may be slightly more legible:

string[] parts =
    Enumerable.Range(0, (str.Length - 1) / len + 1)
              .Select(i => str.Substring(i * len, Math.Min(len, str.Length - i * len)))
              .ToArray();
坏尐絯 2025-01-10 23:05:31

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

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

This linq is not that pretty, but I still like it...

int len = 255;
for (int i = 0; i < big.Length; i += len )
{
    string clip = new string(big.Skip(i).Take(len).ToArray());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文