C# 查找和替换 excel 单元格中的换行符

C# Find and Replace Line Breaks in excel cell

我需要通过 SSIS 将 Excel 文件导入到 sql 服务器。有一种情况。 excel 中的两列相互关联。

我可以在 excel 中执行 find/replace 以将换行符替换为某个分隔符(比如 &&)。有什么办法可以用 c# 代码替换吗? 谢谢

我制作了以下电子表格以匹配您的数据并添加了一个 ID 列:

现在回到SSIS,我添加了以下数据流:

打开脚本组件并转到输入和输出并定义您的列:

返回脚本并单击编辑。

粘贴以下代码以读取电子表格并解析为您的输出:

public override void CreateNewOutputRows()
    {
        string fileName = @"D:\imports\survey.xlsx";
        string SheetName = "Bananas";  
        string cstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";



   using (System.Data.OleDb.OleDbConnection xlConn = new System.Data.OleDb.OleDbConnection(cstr))
    {
       xlConn.Open();
       System.Data.OleDb.OleDbCommand xlCmd = xlConn.CreateCommand();
       xlCmd.CommandText = string.Format("Select * from [{0}$]", SheetName); 
       xlCmd.CommandType = CommandType.Text;
       using (System.Data.OleDb.OleDbDataReader rdr = xlCmd.ExecuteReader())
            while(rdr.Read())
            {
                int id = Convert.ToInt32((decimal.Parse(rdr[0].ToString())));
                string[] keys = rdr.GetString(1).Split('\n');
                string[] values = rdr.GetString(2).Split('\n');

                if (keys.Length > 0)
                {
                    for(int i = 0;i<keys.Length;i++)
                    {
                        Output0Buffer.AddRow();
                        Output0Buffer.ID = id;
                        Output0Buffer.key = keys[i];
                        Output0Buffer.Pair = values[i];
                    }
                }


            }
    }
   }

最后输出: